2019-10-11 admin


什么是hawkejs,Asynchronous Embedded JavaScript templates


Hawkejs logo Hawkejs

<div align=“center”>Mac/Linux Build Status Windows Build status Codecov Coverage report Known Vulnerabilities Dependency Status </div>

<div align=“center”>Latest version on npm Project license </div>

<div align=“center”>A Node.js templating engine, with server- and client-side rendering capabilities</div>

<div align=“center”><sub>Coded with <g-emoji class=“g-emoji” alias=“heart” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/2764.png”>❤️</g-emoji> by Jelle De Loecker.</sub></div>


$ 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-hawkejs block.

Simple assign

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

Assign with default content

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


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 head element.

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>
		<% foundation() %>
		<% assign('body') %>
		<% bottom() %>

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

Load styles and scripts.

In this example, website and first start downloading at the same time, while second loads after the first file 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']) %>


转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]