Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

HTML with class in variable not working

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jonal
Smarty Rookie


Joined: 16 Apr 2021
Posts: 11

PostPosted: Fri Apr 16, 2021 12:29 pm    Post subject: HTML with class in variable not working Reply with quote

I'm pretty new to php and smarty so maybe I do something wrong.

I have this code:

Code:
{if $Data.headline_size == 'h2'}
            {$selectedHeadline = "<h2 class="test">Hello</h2>"}
            {else}
        {/if} 
<div>
     {$selectedHeadline}
</div>


But when I use it, smarty gives me an error. Or lets say Shopware (the CMS) gives me an error that smarty crashed or something like that. Not really an error I can work with.

I tested around and for some reason this
Code:
{if $Data.headline_size == 'h2'}
            {$selectedHeadline = "<h2>Hello</h2>"}
            {else}
        {/if} 
<div>
     {$selectedHeadline}
</div>

without the class in the h2 is working.
Why is it like this and what can I do to make it work?

Thank you very much!
Back to top
View user's profile Send private message
jonal
Smarty Rookie


Joined: 16 Apr 2021
Posts: 11

PostPosted: Fri Apr 16, 2021 12:37 pm    Post subject: Reply with quote

Ok I tested some more and found out that if I use single quotes ' instead of " it's working.
So
Code:

{if $Data.headline_size == 'h2'}
            {$selectedHeadline = "<h2 class='test'>Hello</h2>"}
            {else}
        {/if} 


Works. But I would be still interested if somebody knows why this happens. " is a normal quote for HTML syntax.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Apr 18, 2021 8:53 am    Post subject: Reply with quote

Because you are confusing Smarty and HTML.
Code:
{$selectedHeadline = "<h2 class="test">Hello</h2>"}
is a Smarty syntax and you must exape double quotes like in PHP:

Code:
{$selectedHeadline = "<h2 class=\"test\">Hello</h2>"}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP