{"id":3649,"date":"2014-01-02T08:02:21","date_gmt":"2014-01-02T02:32:21","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=3649"},"modified":"2014-01-02T08:02:21","modified_gmt":"2014-01-02T02:32:21","slug":"client-side-templating-using-underscore-js-and-jquery","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/client-side-templating-using-underscore-js-and-jquery\/","title":{"rendered":"Client side Templating using Underscore.js and JQuery"},"content":{"rendered":"<p style=\"text-align: justify;\">Recently, I ran into requirement where I was getting <a title=\"JSON related Articles on Shiva Blog\" href=\"https:\/\/jitendrazaa.com\/blog\/tag\/json\/\">JSON <\/a>response and needed to render it on my Mobile application. I had two Options, Server side processing and Client side Templating. I studied and checked both way ; came to conclusion on using Client side Templating. There are many plugins and libraries available however there is one which stands different, easy to use and faster as compared to others which is &#8220;<a title=\"Underscore.js Templating\" href=\"http:\/\/documentcloud.github.io\/underscore\/#template\">Underscore.js<\/a>&#8220;.<\/p>\n<p>Underscore.js library needs JQuery as prerequisite. You can find complete documentation <a title=\"Undescore.js Documentation\" href=\"http:\/\/documentcloud.github.io\/underscore\/#template\">here<\/a>, Only remember following three points :<\/p>\n<ol>\n<li>&lt;% %&gt; &#8211; to execute some code<\/li>\n<li>&lt;%= %&gt; &#8211; to print some value in template<\/li>\n<li>&lt;%- %&gt; &#8211; to print some values with HTML escaped<\/li>\n<\/ol>\n<p><strong>Example Source code:<\/strong><\/p>\n<p><!--more-->Below Example shows how to render JSON in tabular format using Underscore.js and JQuery.<\/p>\n<pre class=\"brush: xml; highlight: [42,43,44,45,46,47,48,73]; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n&lt;head&gt;\n\t&lt;title&gt;\n\t\tDemo of Templating using underscore.js\n\t&lt;\/title&gt;\n\n&lt;style type=&quot;text\/css&quot;&gt;\n.outer {\n    border-collapse: collapse;\n    font: 12px\/16px arial, sans-serif;\n    margin: 20px;\n    padding: 0;\n\t}\n\n.outer td, .outer th {\n    border: solid 1px #ddd;\n    margin: 0;\n    padding: 5px;\n}\n\n.outer th, .outer div.th {\n    background: #EEE;\n    font-weight: bold;\n}\n&lt;\/style&gt;\n\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;table class=&quot;outer&quot;&gt;\n    &lt;thead&gt;\n        &lt;tr&gt;\n            &lt;th&gt;Sr. No&lt;\/th&gt;\n            &lt;th&gt;Name&lt;\/th&gt;\n        &lt;\/tr&gt;\n    &lt;\/thead&gt;\n    &lt;tbody&gt;\n    &lt;\/tbody&gt;\n&lt;\/table&gt;\n\n&lt;script type=&quot;text\/html&quot; id='table-data'&gt;\n    &lt;% _.each(items,function(item,key,list){ %&gt;\n        &lt;tr&gt;\n            &lt;td&gt;&lt;%= key+1 %&gt;&lt;\/td&gt;\n            &lt;td&gt;&lt;%= item.name %&gt;&lt;\/td&gt;\n        &lt;\/tr&gt;\n    &lt;% }) %&gt;\n\n&lt;\/script&gt;\n\n\t&lt;script type=&quot;text\/JavaScript&quot;\n\nsrc=&quot;http:\/\/code.jquery.com\/jquery-1.10.2.min.js&quot;\n\n&gt;&lt;\/script&gt;\n\t&lt;script type=&quot;text\/JavaScript&quot;\n\nsrc=&quot;http:\/\/underscorejs.org\/underscore-min.js&quot; &gt;&lt;\/script&gt;\n\n\t&lt;script type=&quot;text\/JavaScript&quot; &gt;\n\tvar items = &#x5B;\n\t\t{name:&quot;Shivasoft&quot;},\n\t\t{name:&quot;Salesforce&quot;},\n\t\t{name:&quot;JQuery&quot;},\n\t\t{name:&quot;UnderscoreJS&quot;},\n\t\t{name:&quot;Template&quot;},\n\t\t{name:&quot;Client Side Templating&quot;}\n\t]\n\n\tvar tableTemplate = $(&quot;#table-data&quot;).html();\n\n\t$(&quot;table.outer tbody&quot;).html(_.template( tableTemplate,{items:items} ));\n\n\t&lt;\/script&gt;\n\n&lt;\/body&gt;\n\n&lt;\/html&gt;\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<figure id=\"attachment_3651\" aria-describedby=\"caption-attachment-3651\" style=\"width: 210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/01\/Underscore-js.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3651\" alt=\"Underscore.js\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/01\/Underscore-js.png?resize=210%2C219&#038;ssl=1\" width=\"210\" height=\"219\" \/><\/a><figcaption id=\"caption-attachment-3651\" class=\"wp-caption-text\">Underscore.js<\/figcaption><\/figure>\n<p><strong>How it Works ?<\/strong><\/p>\n<ol>\n<li>Line 31, we are going to append data in this table.<\/li>\n<li>Line 42-48, defines template on how we are going to render JSON values. We are looping through values and rendering in &#8220;td&#8221; tag of &#8220;table&#8221;.<\/li>\n<li>Line 73, Single line is doing the magic here. It is getting template which is defined between line 42 and 48 and then rendering values in table defined at line 31.<\/li>\n<\/ol>\n<p>Please leave comment for feedback.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I ran into requirement where I was getting JSON response and needed to render it on my Mobile application. I had two Options, Server side processing and Client side Templating. I studied and checked both way ; came to conclusion on using Client side Templating. There are many plugins and libraries available however there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[26,9],"tags":[134,194,206,207],"class_list":["post-3649","post","type-post","status-publish","format-standard","hentry","category-web","category-salesforce","tag-jquery","tag-template","tag-underscore","tag-underscore-js"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2843,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/streaming-api-using-jquery-salesforce\/","url_meta":{"origin":3649,"position":0},"title":"Streaming API Using JQuery &#8211; Salesforce","author":"Jitendra","date":"May 6, 2012","format":false,"excerpt":"Tutorial of Streaming API in Salesforce with Source code and Demo Video - Implementation of Bayeux protocol and CometD using JQuery and JSON","rel":"","context":"In &quot;Tech Tips&quot;","block_context":{"text":"Tech Tips","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/tips\/"},"img":{"alt_text":"Salesforce Streaming API - Create Push topic","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4063,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/why-to-use-bower-for-your-web-developement\/","url_meta":{"origin":3649,"position":1},"title":"Why to use Bower for your web developement","author":"Jitendra","date":"November 23, 2014","format":false,"excerpt":"What is Bower It is client side package Manager and Great tool to manage all you web dependencies like JavaScript libraries and other assets. What can be done from Bower Web sites are made of lots of things \u2014 frameworks, libraries, assets, utilities, and rainbows. Bower manages all these things\u2026","rel":"","context":"In &quot;Node.js&quot;","block_context":{"text":"Node.js","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/node-js\/"},"img":{"alt_text":"Bower","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/11\/bower-logo-150x150.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3762,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-rest-api-playground\/","url_meta":{"origin":3649,"position":2},"title":"Salesforce REST API Playground","author":"Jitendra Zaa","date":"February 25, 2014","format":false,"excerpt":"What is REST API ? In my words, Getting data from Other System or Same System using HTTP request is known as REST API. If you know, how website works, you know REST API. Before REST API, there was SOAP request which needed lots of configuration and very tightly coupled.\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"REST API playground in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/02\/REST-API-playground-in-Salesforce-1024x362.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/02\/REST-API-playground-in-Salesforce-1024x362.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/02\/REST-API-playground-in-Salesforce-1024x362.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":65,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/web\/highlight-current-field-using-jquery\/","url_meta":{"origin":3649,"position":3},"title":"Highlight Current field using JQuery","author":"Jitendra","date":"May 10, 2010","format":false,"excerpt":"Write below HTML code : This will create two text boxes. [sourcecode lang=\"css\"] <form> <div> <label for=\"Name\">Name:<\/label> <input name=\"Name\" type=\"text\"><\/input> <\/div> <div> <label for=\"Email\">Email:<\/label> <input name=\"Email\" type=\"text\"><\/input> <\/div> <\/form> [\/sourcecode] Using jQuery, we can watch for an event where an input form comes into focus: Add link to JQuery file\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Selecting field using jquery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/11-300x50.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3134,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-helptext-like-custom-tooltip-using-jquery\/","url_meta":{"origin":3649,"position":4},"title":"Salesforce Helptext like Custom Tooltip using JQuery","author":"Jitendra","date":"February 4, 2013","format":false,"excerpt":"Welcome back Readers. This is my first blog entry for year 2013, i know its too late. However i can ensure that i have lots of unique post which will come this year. I am starting this year with very light post , mimic the help-text style of Salesforce. Download\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Salesforce like Helptext - Tooltip using JQuery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/02\/Salesforce-like-Tooltip-using-JQuery.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2281,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/web\/disable-inputs-after-submit-to-avoid-double-submission-using-jquery-and-ajax\/","url_meta":{"origin":3649,"position":5},"title":"Disable inputs after submit to avoid double submission using JQuery and Ajax","author":"Jitendra","date":"June 24, 2011","format":false,"excerpt":"This article will explain the safe way to submit the forms using JQuery. It will avoid accidental double submission.","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Disable inputs after submit to avoid double submission using JQuery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/06\/Disable-inputs-after-submit-to-avoid-double-submission-using-JQuery.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=3649"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3649\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=3649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=3649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=3649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}