How to include file in doT.js templating system

2.3k Views Asked by At

I use doT.js 1.0 for templating with requirejs. My goal is to load a file called "length.html" from another file called "freight.html".

The advanced sample states that you can load files with

{{#def.loadfile('/snippet.txt')}}

So I tried using {{#def.loadfile('/length.html')}} in freight.html. freight.html is in the same directory as length.html.

However, the javascript console throws an error:

Uncaught TypeError: Object #<Object> has no method 'loadfile' 

How can I get loadfile to work? Or is the documentation wrong and there is no loadfile function?

1

There are 1 best solutions below

1
Matt Way On

It appears as though loadfile needs to be created by you, as per your requirements. If you are using express, I would suggest looking at https://github.com/nerdo/dot-emc. This module wraps doT so that you can use res.render() to return your templates. It also provides a nifty include function to solve the problem you have above. So instead of loadfile you would use:

{{#def.include('length')}}

Just be aware that if you use this module, the default file extension is .def, which needs to be changed with options to use say .html