Home>

If i include a style tag or javascript tag in the variable, I want to display it without escaping to view.

Applicable source code

In the case described below, HTML escaping can be avoided, but
<style>tag and CSS in it are not reflected.

&lt;div&gt;
    {$freeText nofilter}
&lt;/div&gt;

・ Substitute value

$freeText = '
&lt;a href = "/"&gt;Test data&lt;/a&gt;
&lt;style&gt;
    a {
        font-size: 18px;
        font-weight: bold;
    }
&lt;/style&gt;
&lt;script&gt;
    $(function () {
        alert ('test');
    });
&lt;/script&gt;
';

-The result of var_dump on the controller (value obtained from DB)

&amp;lt;a href = "/"&amp;gt;Test data&amp;lt;/a&amp;gt;
&amp;lt;style&amp;gt;
    a {
        font-size: 18px;
        font-weight: bold;
    }
&amp;lt;/style&amp;gt;
&amp;lt;script&amp;gt;
    $(function () {
        alert ('test');
    });
&amp;lt;/script&amp;gt;

・ Result of var_dump with tpl file

&amp;lt;a href = "/"&amp;gt;Test data&amp;lt;/a&amp;gt;
&amp;lt;style&amp;gt;
    a {
        font-size: 18px;
        font-weight: bold;
    }
&amp;lt;/style&amp;gt;
&amp;lt;script&amp;gt;
    $(function () {
        alert ('test');
    });
&amp;lt;/script&amp;gt;

・ Code displayed as HTML

&lt;a href = "/"&gt;Test data&lt;/a&gt;

I have tried it on the net, but I haven't implemented it yet.
Since we want to be able to directly display what was saved in HTML format on the DB,
How can I reflect the<style>tag and the CSS within it?

Supplemental information System requirements

PHP 7.2
smarty 3.1.3
zend framework1

Thank you for teaching me.

  • Answer # 1

    <div>
        {$freeText nofilter}
    </div>

    This means that, finally,

    <div>
    <a href = "/">Test data</a>
    <style>
        a {
            font-size: 18px;
            font-weight: bold;
        }
    </style>
    <script>
        $(function () {
            alert ('test');
        });
    </script>
    </div>

    I'd like to output like this,

    In the first place, CSS is

    <head>
    <style>
        a {
            font-size: 18px;
            font-weight: bold;
        }
    </style>
    </head>

    In this wayhead,

    <a href = "/">Test data</a>
    As in

    , there is a principle of creating a style attribute. But is there a reason why you have to break that rule and output?

    Because Smarty is a template engine that aims to separate HTML and PHP logic, it is problematic that the tag is included in the PHP variable and the purpose of the original template engine is ignored I think it is the root of

  • Answer # 2

    I don't know how far Zend is supported, but try unescape.