Skip to content

Cookbook: MIK's custom Twig extensions

Mark Jordan edited this page Apr 28, 2017 · 4 revisions

MIK uses the Twig templating engine (version 1.x) in the InsertXmlFromTemplate metadata manipulator Templated metadata parser. It also provides a custom Twig filter, "TwigTruncate". This custom filter will truncate a string to a specific length while respecting word boundaries. One use case for this filter is to truncate object titles within MODS records, since titles longer than 255 characters are problematic in Islandora.

The filter takes one parameter, the length at which to truncate the string. TwigTruncate will truncate the string at the specified length or at the nearest preceding word boundary. An example of its use within a Twig template is:

{% if Title|length < 255 %}
  <titleInfo>
     <title>{{ Title }}</title>
  </titleInfo>
{% else %}
  <titleInfo>
     <title>{{ Title|TwigTruncate(249) }}  [...]</title>
  </titleInfo>
{% endif %}

Cookbook table of contents

Clone this wiki locally