forked from VanceCole/macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebars-templates.js
53 lines (45 loc) · 1.67 KB
/
handlebars-templates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Example using handlebars templates in a module
*/
// Preload and/or register handlebars templates and/or partials
Hooks.once('init', () => {
loadTemplates([
'modules/mymodule/templates/my-template.html',
'modules/mymodule/templates/my-partial.html'
]);
});
// Pass values to handlebars template and get rendered result
let myGreeting = 'Hello';
let myUser = 'Steve';
const myHtml = await renderTemplate(`/path/to/my-handlebars.html`, { myGreeting, myUser });
// my-template.html
<div class="my-template">
<h1>{{ myGreeting }}, {{ myUser }}</h1>
{{> 'modules/mymodule/templates/my-partial.html' }}
</div>
// my-partial.html
<p>This is a partial, {{ myUser }}.</p>
// Result
<div class="my-template">
<h1>Hello, Steve</h1>
<p>This is a partial, Steve.</p>
</div>
/**
* Example getting and inject html from handlebars template
* to existing application
*/
// Whatever application hook, renderMeasuredTemplateConfig is just an example
Hooks.on('renderMeasuredTemplateConfig', async (app, html) => {
// Example value you want to pass to your handlebars template
const myVar = 'Example value to be passed to handlebars';
// Path to module templates
const tpl = '/modules/mymodule/templates/my-handlebars.html';
// Get the handlebars output
const myHtml = await renderTemplate(tpl, { myVar });
// Find form elements and inject their counterparts
// Slightly complicated example here to show how to find a form element when
// it only has a name and not a class or ID, but any jQuery selector works
const target = $(html).find('[name="fillColor"]').parent();
// Inject your handlebars template (can also use .before() of course)
target.after(myHtml);
});