문제

I start with DustJs in KrakenJs environment and i have some troubles with Dust helpers. In fact, i want to create a helper that can create for me a simple bootstrap button.

Here is my code :

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>');
};

And when i call this helper i have the render function for body instead of the final text for body (button content : "function body_3(chk,ctx){ctx=ctx.shiftBlocks(blocks);return chk.write("test");}")

I tried to user chunk.render but i have an error because my final html is not a function like body.

Do you have any idea ?

Regards, Guillaume

도움이 되었습니까?

해결책

The body is an unevaluated chunk which you need to evaluate before you can concatenate it with your strings.

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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top