Sto pensando di usare Hogan.js per il mio prossimo progetto. Stavo provando a sperimentarlo un po '. Sono solo bloccato e incapace di scoprire come utilizzare gli helper con Hogan.js. Ero abituato a usare Manubrio in precedenza. C'è un modo per avere una cosa simile su Hogan?Come avere helper in Hogan.js
6
A
risposta
3
Hogan.js stato sviluppato contro la suite di test baffi, in modo tutto ciò che vale per i modelli come specificato qui, è anche il caso per hogan.js.
Controllare il mustache manpage per una spiegazione approfondita delle funzionalità. Soprattutto la parte sulle espressioni lambda.
Quanto segue è un esempio di confronto dell'implementazione tra hogan.js e handlebars.js.
Template
{{#bold}}
Willy is awesome.
{{/bold}}
Hogan.js
{
"bold": function() {
return function(text, render) {
return "<b>" + render(text) + "</b>"
}
}
}
Handlebars.js
Handlebars.registerHelper('bold', function(options) {
return new Handlebars.SafeString(
'<b>' + options.fn(this) + '</b>'
);
});
uscita
<b>Willy is awesome.</b>
2
ho avuto un momento difficile con questo fino a quando ho trovato this Hogan issue on Lambdas
Non è necessario più passaggio di rendering per l'assistente.
Template
{{#foo}}
Lets put this text in a html tag.
{{/foo}}
Hogan.js
"foo": function() {
return function(text) {
return "<p>" + text + "</p>"
}
uscita
<p>Lets put this text in a html tag.</p>
Il mio problema era un po ' più difficile da quando ho avuto:
Template
{{#foo}}
{{bar}}
{{/foo}}
Così il text
essere passato per l'assistente era solo "{{bar}}"
Hogan.js
"foo": function() {
return function(text) {
// First get the rendered bar variable
var bar = Hogan.compile(text).render(this));
return "<p>" + bar + "</p>"
}
lambda di tutti è che si ottiene, per me è abbastanza. hai un problema specifico che non puoi risolvere con lambda's? –