Skip to content

Crush Templating Engine

Alexander Söderberg edited this page Nov 12, 2017 · 4 revisions

Templates

Kvantum comes with a custom templating engine, called "Crush". Provided it is enabled, it will be able to act on any text based view/file, before it is sent to the user.

Configure

In .kvantum/config/server.yml edit

templates:
  engine: !!com.github.intellectualsites.kvantum.api.config.CoreConfig$TemplatingEngine 'CRUSH'

Syntax

Variables

Variables are replaced in the text document before served. These are provided by views, but follow a general pattern: {{provider.variable}} where the provider is the service providing the variable. The variables can be complex, such as objects and collections/maps.

Filters/Modifiers

Filters can be applied to variables to change their behavior. The name is slightly confusing, as they act more like modifiers. The general pattern is {{provider.variable || FILTER}}. There are a couple of default filters:

  • javascript - Turns the outputted variable into a javascript variable. (Also works for arrays).
  • list - If the variable is of a collection type, or an array, it will be outputted using an un-ordered list (<ul>).
  • lowercase - Turns the variable into a lowercase string
  • uppercase - Turns the variable into an uppercase string

Comments

Comments are removed completely from the served document, and so may hold secret data. The pattern is: /* text */

Loops

You can loop through a variable if it's either a collection, or array, using a foreach loop. The pattern is: {#foreach provider.variable -> var} ... text... {var} {/foreach}

Include

You can include the content of other files into your file using {{include:file.extension}}

Meta

The engine has a built in engine, referred to as meta. Variables can be stored into this using {{: [key: value] :}} and then be referred to using {{meta.key}}

If/IfNot statements

Conditionals can be used to toggle output, either through meta variables or internally. The pattern is: {#if provider.variable}{/if} and (inverted) {#if !provider.variable}{/if}.

Macros

Macros are like functions, that are defined in templates. They are very useful when dealing with repetitive tasks.

A macro is defined by:

{#macro name param1 param2}
<p>{{param1}} said: {{param2}}!</p>
{/macro}

And is then called by:

#name ("value1" "value2")#

Example:

#name ("Trump" "China")#

would output

<p>Trump said: China!</p>

Variables can be inserted into macros: {{provider.name}}

Navigation

Templates

Configuration

Files can be found in .kvantum/config

Views

/commands

Development

Clone this wiki locally