Monklet Variables

by Chris Ullyott | Last edited: 1/6/2021

Watch Video Tutorial!

Sometimes you might want to use a Monklet to build a layout element, but need to use it repeatedly in various contexts. If only a few values in the Monklet need to be different from one page to another, Monklet Variables eliminate the need to create duplicates.

Monklet variables allow you to define a variable inside the Monklet code itself, where the getContent PHP API provides its value from the frontend. Since the variable receives its value before the Monklet itself is parsed, you can use this to override Monklet settings like "find_category" or "find_group", for example.

Usage

In the Monklet, instantiate the variable using double parentheses:


tag="article"
display="list"
find_group="((group))"
show="<h3>__titlelink__</h3>"

Without further changes, the API has no value to assign to group, so the line including "find_group" is ignored.

To dynamically provide a value to group, an extra parameter "monklet" is added to the API call in the page template. The value of this parameter should be a query string including the name and value of the Monklet variable.

<?php
$ministry = 'students';

getContent(
"page",
"display:detail",
"find:" . $_GET['nav'],
"show:__text__",
"monklet:group={$ministry}"
?>

This API call essentially changes the Monklet to be:


tag="article"
display="list"
find_group="students"
show="<h3>__titlelink__</h3>"

Details

  • Like the PHP API, any Monklet "show" including an undefined ((variable)) will be ignored, and that "show" line will be skipped. This is to avoid returning empty HTML tags.
  • To output the "show" line even when ((variable)) is undefined, you may use ((variable nokill="true")), also similar to the PHP API.
  • You can define multiple variables from the template in two ways. The first is to use one "monklet" parameter in the getContent and concatenate each key-value pair of monklet variables in a query string (the PHP function http_build_query can help with that). The second way is simply to add another "monklet" parameter to the getContent.