【问题标题】:DustJs - Helpers renderingDustJs - 助手渲染
【发布时间】:2014-03-10 05:53:35
【问题描述】:

我从 KrakenJs 环境中的 DustJs 开始,我在使用 Dust 助手时遇到了一些麻烦。 事实上,我想创建一个助手,可以为我创建一个简单的引导按钮。

这是我的代码:

var dust = require('dustjs-linkedin');

if (!dust.helpers)
    dust.helpers = {};

dust.helpers.bootstrapButton = function (chunk, context, bodies, params) {
    var body = bodies.block || '',
        options = params || {},
        btnStyle = options.style || 'default',
        btnClass = options.class || '',
        btnSize = options.size || '';

    btnStyle = 'btn btn-' + btnStyle;

    if (btnSize)
        btnSize = 'btn-' + btnSize;

    return chunk.write('<button class="' + btnClass + btnStyle + btnSize + '">' + body + '</button>');
};

当我调用这个助手时,我有 body 的渲染函数而不是 body 的最终文本(按钮内容:“function body_3(chk,ctx){ctx=ctx.shiftBlocks(blocks);return chk.write( "测试");}")

我尝试使用 chunk.render 但我有一个错误,因为我的最终 html 不是像 body 这样的函数。

你有什么想法吗?

问候, 纪尧姆

【问题讨论】:

    标签: dust.js kraken.js


    【解决方案1】:

    正文是一个未评估的块,您需要先对其进行评估,然后才能将其与字符串连接。

    var curChunk = chunk.data.join(); // Capture anything in chunk prior to this helper
    chunk.data = []; // Empty current chunk
    var body = bodies.block(chunk).data.join() || '', // Evaluate block and make a string of it
    .......
        return chunk.write(curChunk + '<button class="' + btnClass + btnStyle + btnSize + '">' + body + '</button>'); // Prefix output with any earlier chunk contents and then build your tag.
    

    【讨论】:

    • 注意你需要写bodies.block(chunk,context)而不是bodies.block(chunk),否则你会得到类似TypeError: Cannot read property 'get' of undefined的错误
    猜你喜欢
    • 2011-03-20
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2012-01-31
    相关资源
    最近更新 更多