简体   繁体   中英

How to include file in doT.js templating system

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM