0.2.3 • Published 4 years ago
template-filters v0.2.3
template-filters
Collection of generic filters to use in any template engine like Nunjucks, Liquid, Handlebars, ejs etc. Zero dependencies
Install
npm i template-filters --save-dev
Add filters to, for example, a 11ty static site:
eleventyConfig.addFilter('attributes', require('template-filters/attributes'));
className
To generate classNames attributes:
<div class="{{ ['foo', {bar: true}] | className }}">
Render to:
<div class="foo bar">
attributes
To generate html attributes:
<div {{ { hidden: true, class: ['foo', 'bar']} | attributes }}">
Render to:
<div hidden class="foo bar">
Use arguments to filter attributes:
<div {{ { hidden: true, name: 'foo', id: 'bar'} | attributes('hidden', 'id') | safe }}">
Render to:
<div hidden id="bar">
markdown
A wrapper of markdown-it to markdownify texts.
const markdownFilter = require('template-filters/markdown');
const MarkdownIt = require('markdown-it');
const markdown = markdownFilter(new MarkdownIt());
markdown('Hello **world**');
{{ 'Hello **world**' | markdown | safe }}
Render to:
<p>Hello <strong>world</strong></p>
Set true to render an inline version
<h1>{{ 'Hello **world**' | markdown(true) | safe }}</h1>
Render to:
<h1>Hello <strong>world</strong></h1>