Documentation Tutorials

Templating

js-web is using handlebars.js as the template engine.

How it works

When you have a htmlRoute(uri,file,callback), the js-web will take the object returned from the callback fuction and will parse it through the template engine with the giving file. This will make the properties on the object accessible from the html file by wrapping the object key {{objectkey}}.

route('/','html/index.html', _ => {
  return {username: 'simon'}
})
hello: {{username}}

Now, the html page will display "hello: simon"

Template functions

Foreach

{{#each users}}
{{point}} - {{name}}
{{/each}}

Unless

{{#unless userId}}
  Please login!
{{/unless}}

If

{{#if userId}}
    Welcome {{username}}
{{/if}}

Include

The this in the include is passing the context to the included file.

{{{include 'web/partials/menu.html'}}}