You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to use bracket in your node project for building your email templates for example, you can use it directly. (For Express, it's recommended to use consolidate.js)
Helper methods can be passed during the compilation of the template. This is especially useful for passing a translation method for example.
Example
vartemplate=bracket.compile("Hello world: [[# translate('Hello world') ]]",{helpers: {translate: function(text){return'Bonjour le monde';}}});template();
→ ResultHello world: Bonjour le monde
Extras for node
When used in node, you can define a master layout and include partial files.
Layout
Layout declarations are done in yaml at the beginning of the template file.
index.brkt.html
---
master: master.brkt.html
title: Index page
---
<!-- Define blocks used in master.brkt.html -->
[[## body()
Hello from index.brkt.html
#]]
[[## body2()
Hello from index.brkt.html again
#]]
<!doctype html><htmllang="en"><head><title>Index page</title></head><body>
Hello from master.brkt.html <br/>
Hello from index.brkt.html <br/>
Hello from index.brkt.html again
</body></html>
The master file specified can be
relative to the current file ../_layout/master.brkt.html
relative to the views set in express app.set('views', path.resolve(__dirname, 'views'));
Pro tip
Custom variables can be defined in the yaml (like title in the example) and used in the master.
Default values in this format [[= layout.title || 'default title' ]]
Partials
You can include another file in your template file.
You can customize all the settings and regex used by bracket.
Default settings
constsettings={// Extract anything inside [[ ]] to be evaluated as jsevaluate: /\[\[([\s\S]+?(\}?)+)]]/g,// Extract anything in the form [[= model ]]interpolate: /\[\[=([\s\S]+?)]]/g,// Extract any block call [[# block1('arg') ]]block: /\[\[#\s*([\w]+)\(([\s\S]*?)\)\s*]]/g,// Extract any block definition [[## block1(arg) #]]blockDef: /\[\[##\s*([\w]+)\(([\s\w,]*)\)\s*[\n]([\s\S]*?)\s*#]]/g,// extract the argument values from a function call// e.g. { test1: '123', test2: 456, test3: true }, 'aaa', true, {}, ''argValues: /\s*({[\s\S]*?}|[^,]+)/g,// The params to pass to the template function// For multiple params, comma delimited e.g. 'model,model2,model3...'varname: 'model',};
Pro tip If you want to pass more than one model, change the varname to 'model, model2' and your template function will accept 2 models.