Full documentation is at eshengsky.github.io/saker/.
Saker is a template engine for Node.js and browsers, it is lightweight, powerfull and easy to use.
The greatest feature is, Saker minimizes the number of characters and keystrokes required in a file, and enables a fast, fluid coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. See Example for preview.
Live Demo
You can test drive Saker online here.
Installation
$ npm install saker
Example
This is a very simple and representative sample, it shows a name list when code is 1, and if the gender is female, the
data
{
"code" : 1,
"data": [{
"id" : 1,
"name" : "Sky",
"gender" : "male"
}, {
"id" : 2,
"name" : "Kathy",
"gender" : "female"
}]
}
template
@{ this.layout = null }
Name List
@if(code === 1) {
@{
data.forEach(function(person) {
-
@person.name
});
}
} else {
Sorry, no data!
}
result
The code looks clean and graceful, isn't it?
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
As you see above, Saker uses '@' as the mark symbol - 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'; @name } |
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') { Has data! } else { No data! } |
It's the special style code blocks, the example left is the same as: @{ if (code == '1') { Has data! } else { No data! } } 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 | Plain text goes here... | When you're inside a code block, you can output plain text by surrounding it with 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 @. |
Test
You can execute the following script in terminal to run test.
$ npm test
License
The MIT License (MIT)
Copyright (c) 2016 Sky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.