{"id":11578,"date":"2014-01-31T16:12:35","date_gmt":"2014-01-31T10:42:35","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11578"},"modified":"2014-01-31T16:12:35","modified_gmt":"2014-01-31T10:42:35","slug":"using-ejs-in-your-node-js-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-ejs-in-your-node-js-application\/","title":{"rendered":"Using EJS in your Node.JS application"},"content":{"rendered":"<p>EJS as the name implies embedded JavaScript, is a JavaScript templating library used to render html code along with JSON data in an easy and clean way.<\/p>\n<p>In a Node.JS application(here we are using an <a href=\"http:\/\/expressjs.com\/\">express<\/a> application), you need to set the value of &#8216;view engine&#8217; parameter to &#8216;ejs&#8217;.<\/p>\n<p>[js]<br \/>\nvar app = express();<br \/>\napp.set(&#8216;view engine&#8217;, &#8216;ejs&#8217;);<br \/>\n[\/js]<\/p>\n<p>Now request goes to backend, a function is called there which renders the ejs file:<br \/>\n[js] res.render(&#8216;demo&#8217;, {data:data})[\/js]<\/p>\n<p>Lets say it returns JSON data like:<br \/>\n<code>{name: 'Sakshi', age : '24', hobbies: ['blogging', 'reading']}<br \/>\n<\/code><\/p>\n<p>Now the final step, Rendering the ejs file. Our demo.ejs looks like:<\/p>\n<p>[js]<br \/>\n&lt;html&gt;<br \/>\n  &lt;body&gt;<br \/>\n  &lt;br\/&gt;<br \/>\n    &lt;h2&gt;EJS Demo!!!&lt;\/h2&gt;<br \/>\n    &lt;div&gt;Name : &lt;%= data.name%&gt;&lt;\/div&gt;<br \/>\n    &lt;div&gt;Age : &lt;%= data.age%&gt;&lt;\/div&gt;<br \/>\n  &lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/js]<\/p>\n<p>Output:<br \/>\n<code>Name: Sakshi<br \/>\n Age: 24<br \/>\n<\/code><\/p>\n<p>Some useful ejs tags are:<br \/>\n1.<strong> &lt;% &gt;<\/strong> : This tag executes all the JavaScript code present inside it. So we can easily use for loops, if\/else statements, switch statements and variable declarations inside the EJS template.<br \/>\nFor examlpe:<br \/>\n[js]<br \/>\n &lt;table&gt;<br \/>\n        &lt;tr&gt;<br \/>\n          &lt;th&gt;Name&lt;\/th&gt;<br \/>\n          &lt;th&gt;Age&lt;\/th&gt;<br \/>\n          &lt;th&gt;Email&lt;\/th&gt;<br \/>\n        &lt;\/tr&gt;<br \/>\n&lt;% result.forEach(function(obj){ %&gt;<br \/>\n        &lt;tr&gt;<br \/>\n          &lt;td&gt;&lt;%=obj.name%&gt;&lt;\/td&gt;<br \/>\n          &lt;td&gt;&lt;%=obj.age%&gt;&lt;\/td&gt;<br \/>\n          &lt;td&gt;&lt;%=obj.email%&gt;&lt;\/td&gt;<br \/>\n        &lt;\/tr&gt;<br \/>\n        &lt;%})%&gt;<br \/>\n&lt;\/table&gt;<br \/>\n[\/js]<\/p>\n<p>2.<strong> &lt;%= &gt;<\/strong> : The value of the variable present between this tag is printed as it is given by JSON data.<\/p>\n<p>3. <strong>&lt;%- &gt;<\/strong> : This tag renders the actual html code present between it.<br \/>\nFor example:<br \/>\nJSON data = {myPage : &#8220;&lt;html&gt;&lt;body&gt;&lt;h3&gt;Hello World&lt;\/h3&gt;&lt;\/body&gt;&lt;\/html&gt;&#8221;}<\/p>\n<p>So for rendering this html code we use:<br \/>\n[js]<br \/>\n&lt;div&gt;&lt;%- myPage&gt;&lt;\/div&gt;<br \/>\n[\/js]<\/p>\n<p>4. <strong>&lt;%== &gt;<\/strong> : This tag is commonly used to include templates in other templates.<\/p>\n<p><strong>NOTE<\/strong>: EJS also allow us to include another ejs template or file into our current ejs page. This can be done using &lt;% include otherPage.ejs %&gt;.<\/p>\n<p>Hope this will help \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EJS as the name implies embedded JavaScript, is a JavaScript templating library used to render html code along with JSON data in an easy and clean way. In a Node.JS application(here we are using an express application), you need to set the value of &#8216;view engine&#8217; parameter to &#8216;ejs&#8217;. [js] var app = express(); app.set(&#8216;view [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1],"tags":[1294,1124],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11578"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11578"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11578\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}