Skip to content

render()

Render a template string or file partial.

Signature

render(templateStr, contextObj = {}, options = {})

Parameters

| Parameter | Type | Description |

|-----------|------|-------------|

| templateStr | string | Template string or file path with #partial suffix |

| contextObj | object | Variables to inject into the template |

| options | object | Options including views directories and custom settings |

Returns

string — The rendered HTML.

Examples

Render a template string

const { render } = require('miki-template');



const html = render('Hello {{ name }}!', { name: 'World' });

// Output: Hello World!
import { render } from 'miki-template';



const html = render('Hello {{ name }}!', { name: 'World' });

// Output: Hello World!

Render a partial from file

const { render } = require('miki-template');



const html = render('home#card', { title: 'Hello' }, { views: './views' });
import { render } from 'miki-template';



const html = render('home#card', { title: 'Hello' }, { views: './views' });

Render with options

const { render } = require('miki-template');



const html = render(template, context, {

  views: ['./views', './app/templates'],

  staticUrl: '/static',

  urlHelper: (name, ...args) => '/' + name + '/' + args.join('/')

});
import { render } from 'miki-template';



const html = render(template, context, {

  views: ['./views', './app/templates'],

  staticUrl: '/static',

  urlHelper: (name, ...args) => '/' + name + '/' + args.join('/')

});