MODX Community

Escaping MODX Tags [SOLVED]


I am making an website for ourselves with some documentation about certain Extra’s etc.
Now i want to be able to put MODX snippets in there for easy copy-pasting, but i dont really know how i should escape the tags…

I put the code in a MIGX tv and display them in the chunk like so

<p class="code-title">[[+title]]</p>
<pre><code class="language-html">[[+code:htmlent]]</code></pre>

The HTML tags get escaped but the MODX tags like [[snippet]] and [[+placeholder]] wont.
Escaping them manually with [\[ doesn’t work and just prints out the [\[formit? ... ]] making it not so easy to copy-paste.

I found a plugin called Escape MODx Tags but it doesn’t work ( I guess it’s for old modx/evo looking at the {{chunk}} tag):

// MODx Plugin: Escape MODx Tags
// Description: A simple system to escape MODx tags for display on a web page.
// Author: James Ehly 
// Website:
// System Event: OnWebPagePrerender
// Usage: [\+tvName+], [\[cachedSnippetName]], [\!snippetName!\],{\{chunkName}}

$find = array('[\\','\]','{\\');
$replace = array('[',']','{');
$modx->documentOutput = str_replace($find,$replace,$modx->documentOutput);

TLDR: How to escape MODX Tags?


Maybe you can replace [ with &lsqb; or &lbrack; in your chunk.

You could create a new custom output modifier to combine it with htmlent:

$input = htmlentities($input,ENT_QUOTES);
return str_replace('[','&lbrack;',$input);
1 Like

I didn’t know you could make custom output modifiers. That is a good idea!
I will test it out tomorrow and will let you know if it works.


Not sure, but I think the content of the placeholder and tags in it gets parsed before the outputfilter runs.
But it should be possible with a prepareSnippet, which runs on each row, before tags are parsed.

Nope, @halftrainedharry’s solution works perfect! Didnt know it was that easy to make an output modifier. modx gets easier the more I learn :smiley:

I use SyntaxHighlighter in combination with FixedPre. Syntaxhighligher gives you nice highlighting, a styled box for the code, and optional line numbers. The FixedPre plugin (which you can use by itself) will escape the MODX tags for you when you put them in FixedPre tags.

<fixedpre>[[Some MODX tag]]</fixedpre>

If you combine it with the SyntaxHighlighter plugin, the the FixedPre tags need to go in the inside:

[[SyntaxHighlighter? &brushes=`css,php`]]