Getting Started
Installation
Saker is available via npm:
$ npm install --save saker
Hello Saker!
Now let's have out first demo: Hello Saker!
Open a terminal, and go to the directory which you've installed saker just now, type according to the following steps:
Step1: switch to node mode
$ node
Step2: require the Saker module
> var saker = require('saker')
Step3: compile template string 'Hello @name!' with data model: {name: 'Saker'}
> saker.compile('Hello @name!').call({layout: null},{name: 'Saker'})
If everything goes well, you can see the stdout as result:
'Hello Saker!'
For more examples, please see examples folder, it includes 3 examples each based on:
- the native Node.js server
- the Express framework
- the browser side
Syntax Cheat Sheet
As you see above, Saker uses '@' as the default mark symbol (support custom) - the codes after it represent server-side scripts. The following is the syntax cheat sheet, for more details you can see Syntax Reference.
Syntax | Example | Remarks |
---|---|---|
Implicit expression | @name | Simply prefix with the @ character to access a variable or a function. Be aware that the output will be automatically HTML encoded. |
Explicit expression | @('name: ' + name) | The explicit impression should be used when you want to do something that might otherwise confuse the parser. For instance, if you need to access a variable in the middle of a string or if you want to do calculations/modifications to the output. |
Unencoded expression | @this.raw(name) | The same as the implicit expression, but the output will not be HTML encoded. |
code blocks | @{ var name = 'Saker'; <span>@name</span> } |
A Saker code block starts with a combination of the @ character and the { character and ends with the } character. Inside of this, you're now writing server-side scripts. Amazing, you can mix HTML markup and the server-side scripts, Saker can distinguish them intelligently! |
Special code blocks | @if (code == '1') { <span>Has data!</span> } else { <span>No data!</span> } |
It's the special style code blocks, the example left is the same as: @{ if (code == '1') { <span>Has data!</span> } else { <span>No data!</span> } } Special code blocks support: @if...else if...else, @for..., @while..., @do...while..., @switch..., @try...catch...finally..., the 6 types. |
Plain text inside a code block | <text>Plain text goes here...</text> | When you're inside a code block, you can output plain text by surrounding it with <text> tags, the tag itself won't be included in the output to the browser. |
Server-side comment | @// This is inline comment @* Here's a Saker server-side multi-line comment It won't be rendered to the browser *@ |
If you need to, you can easily write Saker comments in your code. They are a great alternative to HTML comments, because the Saker comments won't be included in the output to the browser. |
Output @ | eshengsky@@163.com | Double @ will output the symbol @. |