dust.js format text through a function
Problem
I've got a javascript Function which blur's text:
function blurlines(data) {
var dataSplit = data.split(" ");
var lastWord = dataSplit.pop();
var toBlur = '' + dataSplit.join(" ") + '';
// Blur entire sentace, show only last word
var output = '' + toBlur + lastWord + ' ';
return output;
}
I'm trying to get this to work with dust.js by trying something like:
{#storylines}
{/storylines}
Is there anyway to easily pass the {text} value through a JS function and then render the output?
If i run it in console it seems to work:
> blurlines("This is a test line")
> "
Problem courtesy of: Tam2
Solution
I do something similar, create a helper function in my global context:
var dustCtx = dust.makeBase({
blurText: function(chunk, context, bodies, params) {
var dataSplit = params.data.split(" ");
var lastWord = dataSplit.pop();
var toBlur = '' + dataSplit.join(" ") + '';
var output = '' + toBlur + lastWord + ' ';
return chunk.write(output);
}
});
Merge it with local context on render:
dust.render("template", dustCtx.push({storylines:...}), function(err, out) {});
And then call it like this:
{#storylines}
{#blurText data=text/}
{/storylines}
This approach might be handy for passing additional parameters if you wanted to control the blurring for example.
Solution courtesy of: Toby Skinner
Discussion
View additional discussion.