Saturday, May 17, 2014

Dancer / Template Toolkit : auto include javascript sources

Playing with Dancer and Angular I found out spliting up controllers in separate js files seems a clean thing to do... however I kept on missing js includes... let's automate this in your 'layout/main.tt' insert the following to include the js pieces automagically
....

[% PERL %]
my @files = ();
use File::Find;
File::Find::find({ wanted=>sub {   push @files, $File::Find::name if /^.*\.js\z/s; }}, 'public/javascripts', 'public/partials');
foreach my $js (@files) {
    $js =~ s/public\///g;
    print "\n";
}
[% END %]


...
..
Then make sure you configure the template engine in your 'config.yml' for TT.

template: "template_toolkit"
engines:
  template_toolkit:
    encoding:  'utf8'
    start_tag: '[%'
    end_tag:   '%]'
    WRAPPER: layouts/main.tt
    EVAL_PERL: '1'

Then my source gets generated like this (lines 35-43) :

No comments:

Post a Comment