Dust.js helpers
David Longworth 2016-11-25T14:41:55Z
408 words
2 min read

Template helpers

Web includes the common-dustjs-helpers library and provides a set of built-in helpers for use in your page and partial templates.

In addition to the built-in helpers you can add custom filters and helpers to your application. See Dust Template Helpers & Filters for more information.

Common Dust helpers

Follow the links below for detailed documentation of each helper.

DADI Web Helpers

Truncate

Returns the supplied ‘data’ parameter truncated using the supplied ‘length’ parameter

Usage

{@Truncate data="{body}" length="250" ellipsis="true"/}

Trim

Returns the supplied ‘data’ parameter trimmed of whitespace on both left and right sides

Usage

{@Trim data="{body}"/}

formatDate

Returns the supplied ‘data’ parameter formatted using the supplied ‘format’ parameter Pass a unix epoch time (expects milliseconds) in the ‘unix’ parameter. For seconds use ‘unix_sec’

Usage

{@formatDate data="{body}" [unix="{lastModifiedAt}"] format="YYYY-MM-DDTh:mm:ss+01:00"/}

markdown

Returns the markdown content formatted as HTML

soberMarkdown

Returns the markdown content formatted as HTML, but without <p> wrappers

forceRender

Returns the supplied ‘str’ parameter with any instances of {…} resolved to {vartoreplace}

Usage

{@forceRender str="{body}" value="{vartoreplace}" /}

iter

Iterates over items, much like using {#items}{/items}, but with the possiblity to loop over a subset, and in any direction

Usage

{@iter items=arrayOfItems from=0 to=12}
  run for each item, with the item as context
{/iter}

htmlstrip

Strips HTML from passed content. See https://github.com/zaro/node-htmlstrip-native for usage.

defaultParam

Default values for partials

numberCommas

Numbers with commas

plural

pluralise

htmlEncode

Encode html to json valid

url

Generate URLs based on routes by sending in page names & parameters

Usage

{@url page="pagename" param="val" otherparam=variableval/}

slugify

Use the Underscore.JS slugify() method to generate a URL friendly string

Usage

{@slugify}{title}{/slugify}

replace

Performs a global search and replace within a string. In the following example, we replace all periods in the message string with dashes.

Usage

{@replace str="{message}" search="." replace="-" /}

paginate

Paginate pages

Usage

{@paginate page=currentPageNumber totalPages=totalPageCount}
  <a href="{path}">{n}</a>
{:current}
  <a href="{path}">Current page {n}</a>
{:prev}
  <a href="{path}">Prev</a>
{:next}
  <a href="{path}">Next</a>
{/paginate}

findWhere

Get the first item matching the sent in params. Replaces iteration+eq combos.

For arrays of objects each object has its property at key checked for a match with the provided value, much like underscore’s findWhere

Usage

{@findWhere list=aList key="id" value=id}
{.}
{/findWhere}

You can also supply the list with props, which is a loosely parsed JSON object in a string. This makes it possible to combine multiple filters.

{@findWhere list=aList props="{attr: "{strvalue}", other: {numericalId}}"}
{.}
{/findWhere}

See something to improve? Edit in Github.