I've got a javascript function which blur's text:
function blurlines(data) {
var dataSplit = data.split(" ");
var lastWord = dataSplit.pop();
var toBlur = '<span class="blur">' + dataSplit.join(" ") + '</span>';
// Blur entire sentace, show only last word
var output = '<li>' + toBlur + lastWord + '</li>';
return output;
}
I'm trying to get this to work with dust.js by trying something like:
{#storylines}
<script>
blurlines("{text}");
</script>
{/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")
> "<li><span class="blur">This is a test</span>line</li>"
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 = '<span class="blur">' + dataSplit.join(" ") + '</span>';
var output = '<li>' + toBlur + lastWord + '</li>';
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.
I created a dust.js filter to get it to work
bl: function(value){ var dataSplit = value.split(" ");
var lastWord = dataSplit.pop();
var toBlur = '<span class="blur">' + dataSplit.join(" ") + '</span>';
var output = toBlur + lastWord;
return output; }
and am passing the below in the template:
{#storylines}
<li>{text|bl|s}</li>
{/storylines}