【问题标题】:How to include headers, footers in dot.js templating engine?如何在 dot.js 模板引擎中包含页眉、页脚?
【发布时间】:2014-11-16 09:12:23
【问题描述】:

我认为我要做的所有(according to docs on git hub) 就是将{{#def.loadfile('/snippet.txt')}} 放入我的模板中,所以它看起来像这样:

    <!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>

但它似乎不起作用。我什至尝试导入withdoT.js,但仍然没有。 我得到的只是“内部服务器错误”,如果您能够使文件包含在 dot.js 上的工作,您能否分享您的知识。我喜欢这个漂亮的引擎,但它的文档没有那么漂亮。

【问题讨论】:

    标签: javascript node.js template-engine dot.js


    【解决方案1】:

    我不知道它是否适用于你,但是在 nodejs 上,使用 expressjs 和 express-dot.js, 我所做的是定义加载文件:

    在 app.js 中

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

    在 index.dot 中:

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

    基本上它将 loadfile 添加为一个函数,该函数采用路径并在 dotjs 的“def”对象中输出字符串。

    您当然可以根据您的特定需求进行调整和增强

    【讨论】:

      【解决方案2】:

      我很确定 doT 正在寻找从与当前文件相同的目录开始的包含。假设当前模板在视图中,我认为只需从路径中删除“/views”就可以了...

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 2016-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        相关资源
        最近更新 更多