简体   繁体   English

如何在doT.js模板系统中包含文件

[英]How to include file in doT.js templating system

I use doT.js 1.0 for templating with requirejs. 我使用doT.js 1.0与requirejs进行模板化。 My goal is to load a file called "length.html" from another file called "freight.html". 我的目标是从另一个名为“ freight.html”的文件加载一个名为“ length.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. 因此,我尝试{{#def.loadfile('/length.html')}}使用{{#def.loadfile('/length.html')}} freight.html is in the same directory as length.html. Freight.html与length.html在同一目录中。

However, the javascript console throws an error: 但是,javascript控制台引发错误:

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

How can I get loadfile to work? 如何使loadfile工作? 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. 看起来好像需要根据您的要求来创建loadfile If you are using express, I would suggest looking at https://github.com/nerdo/dot-emc . 如果您使用Express,我建议您查看https://github.com/nerdo/dot-emc This module wraps doT so that you can use res.render() to return your templates. 该模块包装了doT,以便您可以使用res.render()返回模板。 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 请注意,如果您使用此模块,则默认文件扩展名是.def ,需要使用例如.html选项进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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