How to include headers, footers in dot.js templating engine?

1.8k Views Asked by At

I thought all I have to do (according to docs on git hub) is to put {{#def.loadfile('/snippet.txt')}} into my template so it would look like this:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='utf-8'>
        <title>Data</title>
        <link href="/css/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        {{#def.loadfile('/views/includes/header.txt')}}
        <section>
            {{~it.arrOut:value:index}}
                {{=value}}!
            {{~}}
        </section>
    </body>
    </html>

but it does not seem to be working. I even tried to import withdoT.js, but still nothing. All I get is "Internal Server Error", If you were able to make file includes work on dot.js can you please share your knowledge. I like this nifty engine, but docs for it are not as nifty.

2

There are 2 best solutions below

0
user2900189 On

I don't know if it apply to you, but on nodejs, using expressjs and express-dot.js, what I did was to define loadfile:

in app.js

var doT = require('express-dot');
doT.setGlobals({
    loadfile:function(path){return fs.readFileSync(__dirname + path, 'utf8');}
});

in index.dot:

{{#def.loadfile('/views/_submit_form.html')}}

basically it adds loadfile as a function that take a path and output a string in the "def" object of dotjs.

You could certainly adapt and enhance this to your specific needs

0
Alex Zak On

I'm pretty sure doT is looking for includes starting from the same directory as the current file is. Assuming that the current template is inside views, I think that just removing "/views" from the path should do the trick...