{"id":140,"date":"2012-05-28T10:34:30","date_gmt":"2012-05-28T14:34:30","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/acts\/?p=140"},"modified":"2012-06-20T10:59:55","modified_gmt":"2012-06-20T14:59:55","slug":"using-smarty-variables-inside-javascript","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/acts\/2012\/05\/28\/using-smarty-variables-inside-javascript\/","title":{"rendered":"Using Smarty variables inside Javascript"},"content":{"rendered":"<p>I looked for appropriate answers for this on the smarty site, and the resounding answer was escaping javascript&#8217;s {}s with {literal}&#8217;s<\/p>\n<p><a href=\"http:\/\/www.codingforums.com\/showthread.php?t=120965\">Smarty template vars and Javascript functions<\/a><br \/>\n<a href=\"http:\/\/www.smarty.net\/forums\/viewtopic.php?t=1067\">How to use Javascript codes in template files?<\/a><br \/>\n<a href=\"http:\/\/www.codingforums.com\/showthread.php?t=199026\">Access Smarty value with javascript<\/a><\/p>\n<p>They basically all say to do something like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\/\/ $question_ids_json = &#x5B;&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;]\r\n{literal}\r\n&lt;script&gt;\r\n$(document).ready(function(){\r\n\tvar question_ids = {\/literal}{$question_ids_json}{literal};\r\n\t\r\n\talert(question_ids&#x5B;0]);\r\n\r\n});\r\n&lt;\/script&gt;\r\n{\/literal}\r\n<\/pre>\n<p>So they want to put everything in a literal tag, and then basically escape that literal tag when smarty needs to be used.<\/p>\n<p>I didn&#8217;t like this, so I put the value in a hidden input and then just retrieved that input in the javascript, thereby keeping the js clean of smarty shenanigans:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;input type=&quot;hidden&quot; id=&quot;question_ids&quot; value=&#039;{$question_ids_json}&#039;\/&gt;\r\n\r\n&lt;script&gt;\r\n$(document).ready(function(){\r\n\t\/\/var question_ids = {$question_ids_json};\r\n\tvar question_ids = eval($(&#039;#question_ids&#039;).val());\r\n\t\r\n\talert(question_ids&#x5B;0]);\r\n\r\n\r\n});\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>But this is just a matter of personal preference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I looked for appropriate answers for this on the smarty site, and the resounding answer was escaping javascript&#8217;s {}s with {literal}&#8217;s Smarty template vars and Javascript functions How to use Javascript codes in template files? Access Smarty value with javascript They basically all say to do something like this: \/\/ $question_ids_json = &#x5B;&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;] {literal} &lt;script&gt; [&hellip;]<\/p>\n","protected":false},"author":4571,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63924,17295,3163,63928,63969],"tags":[111293,63973,63930,63971,63972],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-atg","category-javascript","category-php","category-quizmo-atg","category-smarty","tag-javascript","tag-jquery-2","tag-php-2","tag-smarty-2","tag-templates"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/users\/4571"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":7,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/posts\/140\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/acts\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}