Pass through templates - Handy for Underscore.js templates

60 views
Skip to first unread message

Wes Cravens

unread,
Mar 2, 2012, 6:21:51 AM3/2/12
to mojol...@googlegroups.com
Underscore.js shares the same tag syntax for templates as the default
for ep. i.e. <%= tagfoo %>

I wanted a way to turn off processing for a block of an ep template...
but I could not find one. e.g.

...
<%= somefoo %>
...
<% ep_off %>
...
<%= my_underscore_tags %>
...
<% ep_on %>
...
back to normal

So instead I made a helper that would just pass through a template
untouched. I'm presenting my method here to get comments on it's sanity
and for others to adopt if it suits there needs also.

use Mojolicious::Lite;
use File::Slurp;

app->renderer->add_handler( '_' => sub {
my ( $r, $c, $output, $options ) = @_;

my $inline = $options->{ inline };
my $name = $r->template_name( $options );
my $content = $r->get_data_template( $options, $name );
my $path = $r->template_path( $options );

$$output = read_file($path); # File::Slurp used for example simplicity

return 1;
} );

Then in my ep templates:

%= include '_/myFoo', handler => '_'

Then I put my underscore.js template in 'templates/myFoo.html._' with
it's normal syntax.

It's was also handy putting:
au BufRead,BufNewFile *.html._ set filetype=html.epl

in my .vim/ftdetect/epl.vim file to take advantage of syntax
highlighting for the commonality in their syntax.

Feedback much appreciated,

Wes

Xoan Sampaiño

unread,
Apr 14, 2012, 4:05:57 PM4/14/12
to mojol...@googlegroups.com
On Friday, March 2, 2012 12:21:51 PM UTC+1, wcravens wrote:
Underscore.js shares the same tag syntax for templates as the default
for ep.  i.e. <%= tagfoo %>

I wanted a way to turn off processing for a block of an ep template...
but I could not find one.  e.g.

...
<%= somefoo %>
...
<% ep_off %>
...
<%= my_underscore_tags %>
...
<% ep_on %>
...
back to normal

I think this can be solved by using EP directly:

<%= somefoo %>
<%%= your_underscore_tags %>
<%= somebar %>

Reply all
Reply to author
Forward
0 new messages