Лекция 13
npm install handlebars
// hello.hbs
<div class="entry">
<h1>
{{title}}
</h1>
<div class="body">
{{body}}
</div>
</div>
const source = // читаем hello.hbs
const template = Handlebars.compile(source);
const result = template({
title: 'Hello World',
body: 'First handlebars template',
});
// записываем hello.html
// hello.html
<div class="entry">
<h1>
Hello World
</h1>
<div class="body">
First handlebars template
</div>
</div>
<h1>{{title}}</h1>
<h1>{{article.title}}</h1>
Идентификаторы не могут содержать:
Пробел ! " # % & ' ( ) * + , . / ;
< = > @ [ \ ] ^ ` { | } ~
Но всё таки могут:
{{articles.[10].[published at]}}
{{articles.[10].[#author]}}
{{foo}}
{{{foo}}}
<b>Hello</b>
<b>Hello</b>
<div class="entry">
{{#if author}}
<h1>{{author.firstName}} {{author.lastName}}</h1>
{{/if}}
</div>
<div class="entry">
{{#if author}}
<h1>{{author.firstName}} {{author.lastName}}</h1>
{{else}}
Author unknown
{{/if}}
</div>
<div class="entry">
{{#if author}}
...
{{else if authorLegacy}}
...
{{else}}
...
{{/if}}
</div>
<div class="entry">
{{#unless author}}
Author unknown
{{/unless}}
</div>
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
{
people: [
"Yehuda Katz",
"Alan Johnson",
"Charles Jolley"
]
}
{{#each paragraphs}}
<p>{{this}}</p>
{{else}}
<p class="empty">No content</p>
{{/each}}
{{#each array}}
{{@index}}: {{this}}
{{/each}}
{{#each array}}
{{@key}}: {{this}}
{{/each}}
{{#each array}}
{{#if @first}}
...
{{else if @last}}
...
{{else}}
...
{{/if}}
{{/each}}
{{#each array as |value key|}}
{{#each child as |childValue childKey|}}
{{key}} - {{childKey}}. {{childValue}}
{{/each}}
{{/each}}
{{#with author}}
<h2>By {{firstName}} {{lastName}}</h2>
{{/with}}
{{#with author}}
<p>{{name}}</p>
{{else}}
<p class="empty">No content</p>
{{/with}}
{{#with author as |myAuthor|}}
<h2>By {{myAuthor.firstName}} {{myAuthor.lastName}}</h2>
{{/with}}
{{#each bar}}
{{lookup ../foo @index}}
{{/each}}
{{log "Look at me!"}}
{{#each comments}}
<h2>Comment for {{../title}}</h2>
<div>{{body}}</div>
{{/each}}
{{#each comments}}
<h2>Comment for {{@root.title}}</h2>
<div>{{body}}</div>
{{/each}}
Handlebars.registerPartial(
'myPartial',
'{{name}}'
)
{{> myPartial }}
{{> (whichPartial) }}
{{> myPartial myOtherContext }}
{{> myPartial parameter=value }}
{{#> myPartial }}
Failover content
{{/myPartial}}
{{#> layout }}
My Content
{{/layout}}
Site Content
{{> @partial-block }}
{{#*inline "myPartial"}}
My Content
{{/inline}}
{{#each children}}
{{> myPartial}}
{{/each}}
Handlebars.registerHelper(
'fullName',
(person) =>
`${person.firstName} ${person.lastName}`,
);
<div class="post">
<h1>By {{fullName author}}</h1>
<div class="body">{{body}}</div>
<h1>Comments</h1>
{{#each comments}}
<h2>By {{fullName author}}</h2>
<div class="body">{{body}}</div>
{{/each}}
</div>
Handlebars.registerHelper('bold', function(options) {
return new Handlebars.SafeString(
'<div class="mybold";>'
+ options.fn(this)
+ '</div;>');
});
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{#bold}}{{body}}{{/bold}}
</div>
</div>
npm install hbs
app.set('view engine', 'hbs');
...
res.render('not-found', { title: '404 Not Found' });
Handlebars.registerHelper('eq', function (a, b) {
return a === b;
});
Handlebars.registerHelper('gt', function (a, b) {
return a > b;
});
...
{{#if (eq value1 value2)}}
// true
{{else}}
// false
{{/if}}
Handlebars.registerHelper('and', function () {
const operands = Array.prototype.slice.call(
arguments,
0,
arguments.length - 1
);
return operands.reduce((r, x) => r && x, true);
});
{{set 'video' (
$findby property.files (
$defer @helpers.$fieldMatches 'url' (
$defer
@helpers.$stringIncludes
'https://youtu.be'
'https://www.youtube.com'
)
)
)}}