tj / ejs

Embedded JavaScript templates for node
4.47k stars 514 forks source link

include is either fundamentally broken or not sufficiently documented #68

Open MarkTraceur opened 11 years ago

MarkTraceur commented 11 years ago

Hi!

I'm using the include keyword in a base template to include whatever template I want to be on the page. It looks like so:

<!doctype html>
<html>
    <head>
        <title>MAWG - <%= pageName %></title>
    <link rel="stylesheet" href="/css/style.css" />
    <% if ( tplHead && tplHead !== '' ) { %>
            <%= tplHead %>
    <% } %>
    </head>
    <body>
        <% if ( tplBody && tplBody !== '' ) { %>
            <% include tplBody %>
    <% } %>
    </body>
</html>

Sadly, because the include keyword assumes that whatever happens after it is a string, not a variable, I get

Error: ENOENT, no such file or directory '/home/marktraceur/projects/mawg/src/templates/tplBody.ejs'

Clearly this is wrong. Inside of the <% %> block, there shouldn't be fudging of the JS syntax. Besides that, there should be a way to include arbitrary templates inside of others.

I would also settle for a way to extend templates, like in the Jinja2 system of Python fame. But that seems like a lot more work.

Cheers!

tj commented 11 years ago

include is compile-time

ForbesLindesay commented 11 years ago

Because the include has to be loaded synchronously, it must be loaded at compile time, which means that the value of tplBody is unknown. QEJS supports async rendering of partials (so it doesn't have to happen at compile time) and is otherwise almost identical to EJS. Alternatively I suggest you watch #69

tj commented 11 years ago

we dont even really need async partials, it's more efficient to cache anyway so a single initial sync hit doesn't matter much

Martin-Luther commented 11 years ago

Hello,

sorry to bother, but i'm facing the same problem that MarkTraceur mentioned. An asynchronous include would be really helpful in this case (layout + dynamic partial rendering).

Is it a feature in development or is there a way to write my own function ?

ralyodio commented 11 years ago

<% include head %>

doesn't seem to work with node-email-templates https://github.com/niftylettuce/node-email-templates

ForbesLindesay commented 11 years ago

I'm working on it, I've just been a bit busier than expected over the last few weeks. See: #69