Nested ejs templates can't access global helper

664 views
Skip to first unread message

wingy

unread,
Sep 25, 2012, 9:31:19 AM9/25/12
to expre...@googlegroups.com
I have nested ejs templates to keep everything DRY.

Here is an example response:

    res.render('layout', {
      title: 'Home',
      body: ejs.render(contactTemplate, {
        list: list
      })
    });

All of my templates needs a helper I have put in app.locals.

But that one seems only be accessible by templates rendered by res.render() and not by ejs.render().

How can I allow nested ejs templates but still being able to access app.locals in all of them?

Wingy

Filipe

unread,
Sep 25, 2012, 5:34:06 PM9/25/12
to Express
Hey, this is a very cool solution for not being able to use partials
with EJS, right?

Despite the scope problem, this solution works fine?

Johnny Luu

unread,
Sep 25, 2012, 9:43:15 PM9/25/12
to expre...@googlegroups.com
Yeah this works fine since you can have infinite nestings!

But I really need a global helper in all of my ejs templates.

Any idea?

Wingy


--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.


wingy

unread,
Sep 25, 2012, 10:07:17 PM9/25/12
to expre...@googlegroups.com
There is an excellent include function in ejs: https://github.com/visionmedia/ejs

I ended up doing:

    res.render('contact_template', {
      title: 'Home',
      list: list
    });

Inside contact_template.ejs I used:

    <% include head %>

to include the head section.

That made it pretty DRY and also now you can see the whole HTML from the partials!

Even better imo.

Wingy

Filipe

unread,
Oct 1, 2012, 6:09:25 PM10/1/12
to expre...@googlegroups.com
Awesome, thanks! :)

vinit agrawal

unread,
Feb 9, 2014, 10:48:02 AM2/9/14
to expre...@googlegroups.com, johnn...@yobistore.com
is the template data passed at res.render() available for the nested template included using <%- include head %>  ? for example you have nested templates which expect some data for rendering then is the data passed at res.render() available to it, since include in <%- include head %> only takes one arg which is the ejs template file name
Reply all
Reply to author
Forward
0 new messages