From 1ac7039b3698e814d8e319faf8e2745d9bcd15c2 Mon Sep 17 00:00:00 2001 From: stellarpower Date: Sun, 29 Sep 2024 18:53:32 +0100 Subject: [PATCH] Add boolean parameter to mustache to control whether escapoing of psecial characters is performed or not. --- docs/Functions.md | 6 ++++-- lib/Interpreter/Functions/Template/Mustache.php | 11 +++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/Functions.md b/docs/Functions.md index 35691e4..675db86 100644 --- a/docs/Functions.md +++ b/docs/Functions.md @@ -614,9 +614,11 @@ This function has been removed. The dependency that makes this function work is You can continue using the function by manually installing the [`files_scripts_deprecated`](https://github.com/Raudius/files_scripts_deprecated) app, which bundles all the removed functions. ### mustache -`mustache(String template, [Table variables]={}): String` +`mustache(String template, [Table variables]={}, [Bool escape]=true): String` -Renders a [mustache](https://mustache.github.io) template. +Renders a [mustache](https://mustache.github.io) template. +If escape is false, no escaping of special characters (for e.g. HTML) will be performed. This is suitable for use with plaintext or other file formats where no special handling is desired. + Returns the resulting string. ## Util ### create_date_time diff --git a/lib/Interpreter/Functions/Template/Mustache.php b/lib/Interpreter/Functions/Template/Mustache.php index 6cdbf1f..44f0ea2 100644 --- a/lib/Interpreter/Functions/Template/Mustache.php +++ b/lib/Interpreter/Functions/Template/Mustache.php @@ -12,12 +12,19 @@ * Returns the resulting string. */ class Mustache extends RegistrableFunction { - public function run($template = '', $vars = []): string { + public function run($template = '', $vars = [], $escape = false): string { if (!$vars || !is_array($vars)) { $vars = []; } $vars = $this->normaliseArray($vars); - return (new Mustache_Engine(['entity_flags' => ENT_QUOTES]))->render($template, $vars); + $options = $escape ? [ + 'entity_flags' => ENT_QUOTES + ] : [ + 'escape' => function($value) { return $value; } + ]; + + return (new Mustache_Engine($options))->render($template, $vars); + } }