show a smarty variable with html content
Asked Answered
D

7

15

I have a smarty variable with html content in it like: $html="<strong>Content</strong><br/>etc etc" . I try to show it html-formatted. When showing it like {$html} only plain text appears without formatting. I try like: {$html|unescape} but then the tags are shown but not applied. Do you have any suggestions?

Defrost answered 8/5, 2013 at 20:26 Comment(0)
P
12

You should try this:

{$html|unescape:'html'}

Also check manual:

http://www.smarty.net/docs/en/language.modifier.unescape.tpl

Pansie answered 8/5, 2013 at 20:31 Comment(1)
Any idea how to do the same with smarty 2.x, where unescape doesn't exist?Sinusitis
L
47

Interestingly, none of the answers here work with Smarty 3.1.21 on CS-Cart 4.3.4. So, just to add another thought in that circumstance, use the nofilter on the $html string like so:

{$html nofilter}

Leprose answered 24/11, 2015 at 18:12 Comment(3)
I love you, i was looking the same solution for the same problem in cs-cart 4.4.3 during fetching RSS feeds and displaying Post description.Dogwood
Wow thank's al ot dude, it turns out this is the recommended filter to be used in Prestashop 1.7 !Psychobiology
i am finding solution of this problem for very long time, And it works...........Cunning
P
12

You should try this:

{$html|unescape:'html'}

Also check manual:

http://www.smarty.net/docs/en/language.modifier.unescape.tpl

Pansie answered 8/5, 2013 at 20:31 Comment(1)
Any idea how to do the same with smarty 2.x, where unescape doesn't exist?Sinusitis
F
10

You can try this:

{$html|unescape: "html" nofilter}
Fewness answered 28/7, 2017 at 10:45 Comment(2)
Add some explanation with answer for how this answer help OP in fixing current issueMango
nofilter is help to solve my issueGilford
S
4

Use {$html|unescape: "html" nofilter}

Based on the answer from Sim1-81 and ρяσѕρєя K. I want to explain why the following code works.

The unescape:"html" modifier helps to keep the special characters in place. For example, "&euro;". (Docs).

"nofilter" flag disables $escape_html, which essentially disables the variable being wrapped with htmlspecialchars() (Docs).

Their solution helped as my case was to display a templated block of HTML passed in from a variable.

Si answered 20/8, 2021 at 2:57 Comment(0)
A
0

Some versions of smarty unescape is not available. If this is the case, try using escape:'htmlentitydecode'.

{$html|escape:'htmlentitydecode'}
Aflcio answered 19/11, 2015 at 23:2 Comment(0)
P
0

For those who are using Smarty 2.x, the unescape method is not available, can try this instead;

{$html|html_entity_decode}
Pneuma answered 15/1, 2021 at 5:40 Comment(0)
M
-3

you can try :

php function symbol:

function html($str) {
    $arr = array(
        "&lt;"      => "<",
        "&gt;"      => ">",
        "&quot;"    => '"',
        "&amp;"     => "&",
        "&#92;"     => chr(92),
        "&#39"      => chr(39),
        "&#039;"    => chr(39)
    );
    return nl2br(strtr($str,$arr));
}

In smarty template call:

{html({$html})}

Or without php function only smarty:

{$html|unescape:'allhtml'}

Notice: if in tpl have use reset css you can try remove it and try again.

Mouflon answered 18/12, 2014 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.