API Reference

Saker support native Node.js server, Express and browsers, but the api for them is not exactly the same. Node.jsmeans this support native Node.js server, Expressmeans this support Express, Browsermeans this support browsers.

saker.config(options)

Node.jsExpressBrowser

Set the configuration for Saker, this will overwrite the default value.

options : object
    The configuration set.

options.debug : boolean   Node.jsExpressBrowser
    Output debug info if this is true, default is false.

options.symbol : string   Node.jsExpressBrowser
    Server-side scripts mark symbol, default is '@'.

options.defaultLayout : string   Node.jsExpress
    The default layout file when you do not set it in a view, default is 'layout.html'.

options.partialViewDir : string   Node.jsExpress
    The default partial view directory, default is './views/partials/'.

returns : undefined

saker.getView(filePath, cb)

Node.jsExpress

Try to read a file, return it's content with utf8 encoded.

filePath : string
     The file path you want to read.

cb : function
     The callback function, the first argument is possible error, the second is file content.

returns : undefined

saker.compile(str)

Node.jsExpressBrowser

Compile a template string to a function which can be rendered multiple times with different locals.

str : string
     The source Saker template string to compile.

returns : function
     A function to generate the HTML from a data model object.

var saker = require('saker');

// Compile a function
var fn = saker.compile('<div>@name</div>');

// Define a data model
var model = {
    name: 'Saker'
}

// Render the function
var html = fn.call({layout: null}, model);

Also you can pass additional function to the returned function as callback:

fn.call({layout: null}, model, function(err, html){
    if(err){
        return console.error(err);
    }
    console.log(html);
})

Here wo don't want to render layout view, so wo call the returned function as scope {layout: null}, you can also write it in template like this:

var fn = saker.compile('@{this.layout = null;} <div>@name</div>');

saker.renderView(filePath, model, cb)

Node.jsExpress

Get the result HTML from template file and data model.

filePath : string
     The template file path.

model : object
     The data model, used as the locals object.

cb : function
     The callback function.

returns : undefined

saker.renderView('./index.html', {
    code : 1,
    name: 'Saker'
}, function (err, html) {
    if (err) {
        res.writeHead(500, {'Content-Type': 'text/html'});
        res.end(err.stack);
    } else {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(html);
    }
})

Saker is integrated with Express, so if you are using Express, you can also simply call res.render:

var model = {
    code : 1,
    name: 'Saker'
};
res.render('index', model);