hawkejs

2019-10-11

Asynchronous Embedded JavaScript templates

Hawkejs logoHawkejs

Mac/Linux Build Status

Windows Build status

Codecov Coverage report

Known Vulnerabilities

Dependency Status

Latest version on npm

Project license


A Node.js templating engine, with server- and client-side rendering capabilities
Coded with ❤️ by Jelle De Loecker.

Installation

$ npm install hawkejs

What helper functions are there?

start(name [, options]) & end(name [, options])

Define a new block.

The EJS code & HTML statements between these 2 functions will be executed immediately, but will not be printed out into the template yet.

They can be print out somewhere else using assign()

<% start('my-block') %>
    <p>This content will be stored into the <b>my-block</b> block</p>
<% end('my-block') %>

assign(name [, options]) [& assign_end(name [, options])]

Assign a block to this location.

When you use the assign_end() function, the content between these 2 functions will be used as default content, if no block was found.

Assignments are wrapped inside a x-hawkejsblock.

Simple assign

<div>
    <% assign('my-block') %>
</div>

Assign with default content

<div>
    <% assign('my-block') %>
        <p>This is temporary, default content</p>
    <% end_assign('my-block') %>
</div>

expands(names)

Indicate that the defined blocks in the current file should be assigned to any of the given layouts. Any HTML not inside a block is discarded.

If the layout is already rendered (on the client side), those blocks are replaced and rendering stops.

If it is not available, or this is a server-side render, that file is parsed and rendered.

Single name

<% expands('layouts/base') %>

<% start('body') %>
    <p>This content will be assigned to the <b>body</b> block inside the <b>layouts/base</b> template</p>
<% end('body') %>

Multiple names

You can supply multiple names. The first template to be found will be used. If none are found yet, the first one is rendered.

<% expands(['layouts/base', 'layouts/mod']) %>

<% start('body') %>
    <p>This content will be assigned to the <b>body</b> block inside the <b>layouts/base</b> or the <b>layouts/mod</b> template</p>
<% end('body') %>

foundation() and bottom()

The foundation()method needs to be put somewhere in the document headelement.

It puts in all the required data for client-side renders, events, styles and scripts.

bottom()is used to place in extra html markup, like modals.

<!DOCTYPE html>
<html>
    <head>
        <% foundation() %>
    </head>
    <body>
        <% assign('body') %>
        <% bottom() %>
    </body>
</html>

style(url [, options]) & script(url [, options])

Load styles and scripts.

In this example, websiteand firststart downloading at the same time, while secondloads after the firstfile is complete.

Styles are all downloaded in parallel.

// Load in the 'website.js' script file
<% script('website') %>

// Load in the 'first.js' script file and then the 'second.js' file serially
<% script(['first', 'second']) %>

Repository

https@github.com:skerit/hawkejs


上一篇:head-require
下一篇:谈谈html2canvas在项目中遇见的问题
相关教程
关注微信

扫码加入 JavaScript 社区

相关文章
暂无相关文章

首次访问,需要验证
微信扫码,关注即可
(仅需验证一次)

欢迎加入 JavaScript 社区

号内回复关键字:

回到顶部