DevelopmentTypo3

htmldecode and encode View Helpers for Fluid (Update)

By 8. 12. 2010November 9th, 2012No Comments

Fluid and Extbase are cool stuff! Really. Right now I create a new extension for a Namics customer and I am also working on a port of my podcast extension. All the basic stuff is already done; creating xml, rendering views for the podcasts including HTML5 Audio output etc. – if you like to offer help, leave me a comment.
One thing I found laking was, that objects assigned to the view are html encoded – this means, that if you assign the view a prerendered tag, lets say a captcha-image, it will show up as text and not as html tag. There is no easy way around it – that said, if you’re not up to abuse the helper by providing a wrong parseFuncTSPath argument. That’s why I have written two simple fluid view helpers that might help you with your own extension:

htmlDecode View Helper

class Tx_YourExtensionKey_Format_HtmlDecodeViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
    /**
     * Decodes a given string to HTML
     *
     * @param string $string the string to be decoded
     * @param string $quote_style The optional second quote_style parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT:
     * @param string $charset The URF-8 character set is used as default for the optional third charset. This defines the character set used in conversion.
     * @return string htmldecoded string
     * @author Noel Bossart 
     */
    public function render($code = NULL, $quote_style = ENT_COMPAT, $charset = 'UTF-8') {
        if ($code === NULL) {
            $code = $this->renderChildren();
        }
        return html_entity_decode($code, $quote_style, $charset);
    }
}

htmlEncode View Helper:

class Tx_YourExtensionKey_ViewHelpers_Format_HtmlEncodeViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

    /**
     * HTML encodes a given string
     *
     * @param string $string the string to be encoded
     * @param string $quote_style The optional second quote_style parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT:
     * @param string $charset The URF-8 character set is used as default for the optional third charset. This defines the character set used in conversion.
     * @return string HTML encoded string
     * @author Noel Bossart
     */
    public function render($code = NULL, $quote_style = ENT_COMPAT, $charset = 'UTF-8') {
        if ($code === NULL) {
            $code = $this->renderChildren();
        }
        return htmlspecialchars($code, $quote_style, $charset);
    }
}

You can download both view helpers and put the files into the following folder: EXT:/yourextension/Classes/ViewHelpers/Format/

Updated: Or you just use html_entity_decode() before you pass it to the view :)

NoëI

NoëI

I’m a full-stack developer living and working in Switzerland. I love to craft elegant and fast websites using powerfull tools that look great and are easy to use. My passion for good looking and even better working software lead me to try new things all the time – I guess, that makes me a classic digitalpixelnerd.