{"id":807,"date":"2022-08-30T15:07:30","date_gmt":"2022-08-30T15:07:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/backbone-jst-ejs-blank-values-in-templates-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:07:30","modified_gmt":"2022-08-30T15:07:30","slug":"backbone-jst-ejs-blank-values-in-templates-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/backbone-jst-ejs-blank-values-in-templates-collection-of-common-programming-errors\/","title":{"rendered":"Backbone, JST, EJS: Blank values in templates-Collection of common programming errors"},"content":{"rendered":"<p>I have a JST&#8211;&gt;EJS backbone template for a &#8216;projects&#8217; form. Ideally, I&#8217;d like to use the same template for both creating and editing models.<\/p>\n<p>At the moment, I have something such as this in the template:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>That works fine so long as demo_field has a value, but if it doesn&#8217;t, the whole form fails with an uncaught referenceError.<\/p>\n<p>So, what&#8217;s the most elegant way to assure variable fields return blank, instead of fail, in Backbone&#8230;or JST&#8230;or EJS?<\/p>\n<p>Cheers&#8230;<\/p>\n<ol>\n<li>\n<p>I&#8217;d probably make a helper, since javascript doesn&#8217;t have any nice way of doing something like <code>field if field?<\/code> in CoffeeScript.<\/p>\n<p>Throw something like this into a helper function:<\/p>\n<pre><code>function printIfExists(field) {\n  return (typeof field !== \"undefined\" &amp;&amp; field !== null) ? field : '';\n}\n<\/code><\/pre>\n<p>and call it in your templates. Easy as pie!<\/p>\n<p>You might also want to use the alternate <code>function<\/code> declaration:<\/p>\n<pre><code>var printIfExists = function(field){...}\n<\/code><\/pre>\n<p>I&#8217;ve been using coffeescript so long I can&#8217;t recall why&#8230; scoping issues with the former example, probably.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:46:42. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a JST&#8211;&gt;EJS backbone template for a &#8216;projects&#8217; form. Ideally, I&#8217;d like to use the same template for both creating and editing models. At the moment, I have something such as this in the template: That works fine so long as demo_field has a value, but if it doesn&#8217;t, the whole form fails with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-807","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=807"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/807\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}