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

smarty fails when html contains a json array?

 
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
doesit
Smarty n00b


Joined: 05 Mar 2014
Posts: 1

PostPosted: Wed Mar 05, 2014 4:03 am    Post subject: smarty fails when html contains a json array? Reply with quote

Hi all,

In part of my smarty template I have the need to embed a json array to an attribute on a div. Below is my example code, and the error that occurs.

Is there a way to avoid wrapping every json array I have like this in a {literal} tag?

Thanks!


Quote:

<div data-state="{'key':'This is an example'}"></div>



Quote:
Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "D:\core\skin\templates\layout\page.tpl" on line 1 "<div data-state="{'key':'This is an example'}"></div>" - Unexpected ":", expected one of: "}" <-- thrown in D:\core\common\classes\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 1

Notice: Only variable references should be returned by reference in D:\core\common\classes\instance.class.inc on line 35

Notice: Only variable references should be returned by reference in D:\core\common\classes\instance.class.inc on line 35
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Mar 05, 2014 3:08 pm    Post subject: Reply with quote

A few ways to go about it, some better than others in different circumstances.

1) change smarty delims to something other than {}
2) use {ldelim}{rdelim}
Code:
<div data-state="{ldelim}'key':'This is an example'{rdelim}"></div>

3) create a prefilter that identifies json strings and replaces the {} with {ldelim}{rdelim}
4) change your tags on json to something unique:
Code:
<div data-state="{{'key':'This is an example'}}"></div>

then use a prefilter to search and replace the {{}} to normal tags
5) if using smarty 3, add spaces:
Code:
<div data-state="{ 'key':'This is an example' }"></div>


option #3 does not require changes to the template, option #4 might be most practical.
Back to top
View user's profile Send private message Visit poster's website
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