We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11149
    • 17 Posts
    Hello,

    I want to convert html tags (e.g. '<div>') into their HTML entities (e.g. '&lt;div&gt;') using the htmlspecialchars() function.
    This I want to do for a all text/code between the <code></code> tags in a recourse content field.

    I already did some research on the subject but could not find anything helpful.

    My limited understanding of modx tells me that a snippet is not helpful because I don't want to output a string. I want to intervene in the processing of the content of an inline-resource before it gets merged. Can anybody give me a tip how this could be done?

    summarizing:
    - how can I get and change the content of an inline recource?
    - where can I code the necessary php code before the inline-resource content is merged with the template?

    Any tip, link to a recource or correction would be much appreciated.

    ps. Big thanks to all the people who contributed to modx.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You would need to use a plugin. You could save some processing by doing this when saving the content in question rather than when generating the page.

      You could also look into using javascript to display the content of the desired HTML element. http://stackoverflow.com/questions/848527/jquery-escaping-html-for-preview


      [ed. note: sottwell last edited this post 14 years, 8 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 11149
        • 17 Posts
        Thanks for the advice. I don't want to convert the characters client side. So I have to use the plug-in.
        Here is the code I came up with so far:
        <?php
        &content = '';
        switch($modx->event->name) {
            case 'OnParseDocument':
                &content = &$modx->documentOutput;
                $content = htmlspecialchars($content);
                $content = str_replace(array("[","]","`"),array("[","]","`"),$content);
                $modx->documentOutput($content);
            break;
        }
        return 0;

        It is not working
        How can I get/access the output variable of the parsed document as string? And then I need to write the changed string back to the output variable. How could I do that?

        Again, any help would be much appreciated.