Add `|json` template modifier
authorTim Düsterhus <duesterhus@woltlab.com>
Thu, 20 Jan 2022 10:48:16 +0000 (11:48 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 20 Jan 2022 10:56:23 +0000 (11:56 +0100)
wcfsetup/install/files/lib/system/template/plugin/JsonModifierTemplatePlugin.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/template/plugin/JsonModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/JsonModifierTemplatePlugin.class.php
new file mode 100644 (file)
index 0000000..16e3652
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+namespace wcf\system\template\plugin;
+
+use wcf\system\template\TemplateEngine;
+use wcf\util\JSON;
+
+/**
+ * JSON encodes the given value.
+ *
+ * Usage:
+ *  { "title": {$foo->getTitle()|json} }
+ *
+ * Depending on the location you might need to either HTML-encode the resulting JSON string
+ * or not. Within a `<script>` tag, additional HTML-encoding usually is an error, as HTML is
+ * not interpreted within there, thus `{@$var|json}` with the additional `@` will need to be
+ * used.
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2022 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Template\Plugin
+ * @since 5.5
+ */
+class JsonModifierTemplatePlugin implements IModifierTemplatePlugin
+{
+    /**
+     * @inheritDoc
+     */
+    public function execute($tagArgs, TemplateEngine $tplObj)
+    {
+        return JSON::encode($tagArgs[0]);
+    }
+}