Skip to content

Libraries API

registerLibrary

Register a library — a named bundle of filters, tags, and helpers.

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



registerLibrary('mylib', {

  filters: {

    shout: (val) => String(val).toUpperCase() + '!'

  },

  tags: {

    hello: (tagContent, parser) => ({

      render: (context) => 'Hello!'

    })

  },

  helpers: {

    bold: (inner, context) => `<b>${inner}</b>`

  }

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



registerLibrary('mylib', {

  filters: {

    shout: (val) => String(val).toUpperCase() + '!'

  },

  tags: {

    hello: (tagContent, parser) => ({

      render: (context) => 'Hello!'

    })

  },

  helpers: {

    bold: (inner, context) => `<b>${inner}</b>`

  }

});

activateLibrary

Activate a library so its filters and tags become available.

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



activateLibrary('mylib');
import { activateLibrary } from 'miki-template';



activateLibrary('mylib');

Built-in libraries (humanize, cache, lorem, markdown, i18n) are auto-activated on import.

unregisterLibrary

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



// Unregister specific library

unregisterLibrary('mylib');



// Unregister all libraries

unregisterLibrary();
import { unregisterLibrary } from 'miki-template';



unregisterLibrary('mylib');

hasLibrary

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



if (hasLibrary('humanize')) {

  // library is registered

}
import { hasLibrary } from 'miki-template';



if (hasLibrary('humanize')) {

  // library is registered

}

getLibrary

Retrieve a library by name.

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



const lib = getLibrary('humanize');

console.log(Object.keys(lib.filters));
import { getLibrary } from 'miki-template';



const lib = getLibrary('humanize');

getLibraryNames

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



console.log(getLibraryNames());

// ['humanize', 'cache', 'lorem']
import { getLibraryNames } from 'miki-template';



console.log(getLibraryNames());

registerLibraryFromPath

Load a library from a JavaScript file on disk. The file must export { tags, filters, helpers }.

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



registerLibraryFromPath('mylib', './libs/mylib.js');
import { registerLibraryFromPath } from 'miki-template';



await registerLibraryFromPath('mylib', './libs/mylib.mjs');

Built-in Libraries

humanize

Provides natural formatting filters: intcomma, intword, ordinal, naturalday, naturaltime.

| Filter | Description |

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

| intcomma | 1234567 → 1,234,567 |

| intword | 1234567 → 1.2M |

| ordinal | 1 → 1st, 2 → 2nd |

| naturalday | Format dates as "today", "yesterday" |

| naturaltime | Format times as "just now", "2 hours ago" |

| ordinal | Convert numbers to ordinal (1st, 2nd, 3rd) |

cache

Provides {% cache timeout key %}...{% endcache %} tag for caching template fragments.

lorem

Provides {% lorem count random_words %} tag and lorem filter for placeholder text.

markdown

Provides markdown filter for Markdown→HTML conversion.

i18n

Provides {% trans %}, {% blocktrans %}, {% language %} tags and translation functions.

Next Steps