3

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?

JstnPwll
  • 8,585
  • 2
  • 33
  • 56
Manuel Hoffmann
  • 539
  • 1
  • 7
  • 23
  • Also, I found out that the advanced sample defines their own loadfile function elsewhere. But this seems to be for node.js because they use `var doT = require('doT');` Do I have to define my own function? – Manuel Hoffmann Mar 02 '14 at 16:53

1 Answers1

1

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

Matt Way
  • 32,319
  • 10
  • 79
  • 85