0

Ok. Not sure this is a good idea. I'm building a template which passes a JSON config to a Javascript file assembling the template.

For links I need to include some logic how to build them in my JSON config. I would have to call this:

path.href.replace( /.*view=/, "" ) + ".cfm?id="+content.vcard.adresses[1]["iln/gln"]

Which in my config JSON:

<ul data-template="true" data-config='{  
    "type":"listview",
    "link":"path.href.replace( /.*view=/, '' ) + '.cfm?id='+content.vcard.adresses[1]['iln/gln']",
    "iconpos":"right"
    }'></ul>

Will not work because of quotation-mark-mess, so the JSON is valid, but wrapping it in single quotes messes the HTML up.

Question:
Is there any way to pass this without breaking the HTML? Since it's a template I would like to keep the javascript logic as clean as possible = not put in custom methods for every template instance. So I would like to keep the method call here.

Thanks!

2 Answers 2

1

You can escape quotation marks using the &#39; HTML entity:

<ul data-template="true" data-config='{  
    "type":"listview",
    "link":"path.href.replace( /.*view=/, &#39;&#39; ) + &#39;.cfm?id=&#39;+content.vcard.adresses[1][&#39;iln/gln&#39;]",
    "iconpos":"right"
    }'></ul>

Here is a demonstration: http://jsfiddle.net/fVLPd/

Sign up to request clarification or add additional context in comments.

5 Comments

hm. let's see if javascript understands. Thanks so far.
doesn't work. Throws an error in Jquery: SyntaxError: JSON.parse: expected ',' or '}' after property value in object
also, does Javascript know how to handle escaped characters? I'm passing the HTML config-JSON to Javascript.
@frequent My bad, you should be using the entity &#39;, not &quot;. &quot; is double quotes
@frequent Added a demonstration as well
1

Consider using JSON.stringify to build whole JSON string from an object. Than you don't need to worry about incorrectly encoded quotes or trying to encode quotes in some other way.

If you generate the templates server side - use JSON encoder for your server side language...

1 Comment

also a good idea. although I'm just experimenting (need to go application offline eventually), manually building JSON server-side is kind of a drag :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.