Szablon renderujący szablon jako tekst
Stworzyłem helpera w kierownicy, aby pomóc w logice, ale mój szablon przetwarza zwracany html jako tekst, a nie html.
Mam stronę wyników quizu, która jest renderowana po zakończeniu quizu:
<script id="quiz-result" type="text/x-handlebars-template">
{{#each rounds}}
{{round_end_result}}
{{/each}}
<div class="clear"></div>
</script>
Dla każdej rundy używam helpera, aby określić, który szablon wyrenderuje wynik rundy:
Handlebars.registerHelper("round_end_result", function() {
if (this.correct) {
var source = '';
if (this.guess == this.correct) {
console.log("correct guess");
var source = $("#round-end-correct").html();
} else {
var source = $("#round-end-wrong").html();
}
var template = Handlebars.compile(source);
var context = this;
var html = template(context);
console.log(html);
return html;
} else {
console.log("tie");
}
});
Oto szablon opisujący poprawną rundę (powiedzmy, że renderował #round-end-correct template):
<script id="round-end-correct" type="text/x-handlebars-template">
<div></div>
</script>
Oto co dostaje "rendered": {]}
<div></div>
Nie jako HTML, ale jako tekst. Jak sprawić, aby faktycznie renderował HTML jako HTML, a nie tekst?
2 answers
Zakładam, że unescaping w kierownicy działa tak samo jak w wąsach waniliowych.
W takim przypadku użyj potrójnych wąsów, aby unescape html,i, E: {{{unescapedhtml}}}
, Jak:
<script id="quiz-result" type="text/x-handlebars-template">
{{#each rounds}}
{{{round_end_result}}}
{{/each}}
<div class="clear"></div>
Dla ref zobacz: http://mustache.github.com/mustache.5.html
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-22 16:18:20
Odpowiedzi Geerta-Jana są poprawne, ale tylko w celach informacyjnych można również ustawić wynik na "Bezpieczny" bezpośrednio w helperze (kod z kierownicy.js wiki)
Handlebars.registerHelper('foo', function(text, url) {
text = Handlebars.Utils.escapeExpression(text);
url = Handlebars.Utils.escapeExpression(url);
var result = '<a href="' + url + '">' + text + '</a>';
return new Handlebars.SafeString(result);
});
Dzięki temu możesz używać zwykłej podwójnej kierownicy {{ }} i kierownica nie wymknie się Twojej ekspresji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-30 22:13:53