RandomEtc / ejs-locals

Express 3.x layout, partial and block template functions for the EJS template engine.
298 stars 63 forks source link

setting variables #14

Closed ralyodio closed 11 years ago

ralyodio commented 11 years ago

This is causing page not to be rendered, data is not found:

login.ejs <% layout(layout) %> <% var title = 'Login page' %>

layout.ejs <title><%= title %></title>

It works fine if I remove the 'var' in the partial.

RandomEtc commented 11 years ago

You can do this in login.ejs:

<% block('title', "Login page") %>

And in layout.ejs:

<title><%= blocks.title %></title>
ralyodio commented 11 years ago

Is there any advantage over just doing:

<% title = 'Login page' %> in the login.ejs

and then <%= title %> in the layout.ejs

It seems to work this way, but as soon as I introduced var, it broke everything.

RandomEtc commented 11 years ago

Honestly I don't know, I'm surprised that works at all. I think that it's creating a global variable. With luck the global only exists in the context of the template!

ralyodio commented 11 years ago

thanks, i'll use the block() just to be safe.

ralyodio commented 11 years ago

One more issue, can I use a filter with a block?

https://github.com/visionmedia/ejs/issues/73

RandomEtc commented 11 years ago

If you apply the filter when using the block content it should work fine.

RandomEtc commented 11 years ago

Something like <title><%=: blocks.title | capitalize %></title> should do it. I confess that I didn't test filters when I wrote this stuff but I have tested them since and they seem to work OK.

ralyodio commented 11 years ago

I can't do it like that -- I only have one place where the title uses a filter.