<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>template JavaScript API Module</title><link>https://community.telligent.com/community/13/w/api-documentation/75729/template-javascript-api-module</link><description>&lt;p&gt;&lt;span&gt;Developing on Verint Community? Use the API documentation as a reference for all supported interactions with Verint Community.&lt;/span&gt;&lt;/p&gt;</description><dc:language>en-US</dc:language><generator>14.0.0.586 14</generator><item><title>template JavaScript API Module</title><link>https://community.telligent.com/community/13/w/api-documentation/75729/template-javascript-api-module</link><pubDate>Fri, 08 Mar 2024 17:51:11 GMT</pubDate><guid isPermaLink="false">e2114b96-15cf-483f-acb1-afcfc8751061</guid><dc:creator>Ben Tiedt</dc:creator><comments>https://community.telligent.com/community/13/w/api-documentation/75729/template-javascript-api-module#comments</comments><description>Current Revision posted to API Documentation by Ben Tiedt on 03/08/2024 17:51:11&lt;br /&gt;
&lt;hr class="generated-documentation-start" style="border-width:0;" /&gt;&lt;h3&gt;jQuery.telligent.evolution.template&lt;/h3&gt;
&lt;p&gt;The template module provides a simple client-side templating. This is useful when building UI from REST responses or other cases when it&amp;#39;s impractical to render UI in the widget on the server side.&lt;/p&gt;
&lt;h3&gt;Usage&lt;/h3&gt;
&lt;h4&gt;Defining a template&lt;/h4&gt;
&lt;p&gt;Client templates can be defined either as plain JavaScript strings or embedded in HTML rendered by a widget. When rendering in HTML, the template should be defined in a script block of type &lt;code&gt;text/html&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/html&amp;quot; id=&amp;quot;hello&amp;quot;&amp;gt;
    &amp;lt;p&amp;gt;Hello, &amp;lt;%: name %&amp;gt;.&amp;lt;/p&amp;gt;
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Delimiters in the template support rendering (and optionally encoding) variables as well as embedding JavaScript logic.&lt;/p&gt;
&lt;p&gt;The delimiter format &lt;code&gt;&amp;lt;%= value %&amp;gt;&lt;/code&gt; renders a variable without encoding:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;p&amp;gt;This variable will be rendered directly without encoding &amp;lt;%= variableName %&amp;gt;&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The delimiter format &lt;code&gt;&amp;lt;%: value %&amp;gt;&lt;/code&gt; renders a variable after HTML-encoding it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;p&amp;gt;This variable will be rendered with HTML encoding &amp;lt;%: variableName %&amp;gt;&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The delimiter format &lt;code&gt;&amp;lt;% %&amp;gt;&lt;/code&gt; renders nothing but allows embedding of raw JavaScript logic.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;div&amp;gt;
    &amp;lt;% if (answered) { %&amp;gt;
        &amp;lt;span class=&amp;quot;answered&amp;quot;&amp;gt;Answered&amp;lt;/span&amp;gt;
    &amp;lt;% } else { %&amp;gt;
        &amp;lt;span class=&amp;quot;not-answered&amp;quot;&amp;gt;Not Answered&amp;lt;/span&amp;gt;
    &amp;lt;% } %&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looping can be performed with the template helper, &lt;code&gt;foreach&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
    &amp;lt;% foreach(users, function(user){ %&amp;gt;
        &amp;lt;li&amp;gt;
            &amp;lt;a href=&amp;quot;&amp;lt;%: user.profileUrl %&amp;gt;&amp;quot;&amp;gt;
                &amp;lt;%= user.displayName %&amp;gt;
            &amp;lt;/a&amp;gt;
        &amp;lt;/li&amp;gt;
    &amp;lt;% }); %&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Using the template&lt;/h4&gt;
&lt;p&gt;To use a template, it must first be compiled. This transforms it from a string into an efficient function which can be passed data to template, returning a rendered string.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// given a &amp;lt;script type=&amp;quot;text/html&amp;quot; id=&amp;quot;hello-world&amp;quot;&amp;gt; ... &amp;lt;/script&amp;gt;
var helloWorldTemplate = $.telligent.evolution.template.compile(&amp;#39;hello-world&amp;#39;);

// alternatively, a raw template string can be passed to compile
var helloWorldTemplate = $.telligent.evolution.template.compile(&amp;#39;&amp;lt;p&amp;gt;Hello &amp;lt;%: name %&amp;gt;&amp;lt;/p&amp;gt;&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The compiled template can then be efficiently used (and re-used) by passing data to template. The data must contain keys that match those contained in the template. In this case, the template can refer to a variable named &lt;code&gt;&amp;#39;name&amp;#39;&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var renderedHello = helloWorldTemplate({
    name: &amp;#39;Rob&amp;#39;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Arrays can be passed as well. In this case, a template&amp;#39;s &lt;code&gt;foreach&lt;/code&gt; could iterate over &lt;code&gt;&amp;#39;users&amp;#39;&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var renderedUserList = user({
    users: [
        { displayName: 'Name1', profileUrl: 'url1' },
        { displayName: 'Name2', profileUrl: 'url3' },
        { displayName: 'Name3', profileUrl: 'url3' }
    ]
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Customizing template syntax&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;foreach&lt;/code&gt; is a platform-defined template helper. Custom helpers can also be defined by passing an object of functions during compilation.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var myTemplate = $.telligent.evolution.template.compile(&amp;#39;myTemplate&amp;#39;, {
    pluralize: function(raw) {
        // naive pluralization demonstration
        return raw + &amp;#39;s&amp;#39;;
    }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then the helper can be used within the template&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&amp;quot;text/html&amp;quot; id=&amp;quot;myTemplate&amp;quot;&amp;gt;
    &amp;lt;ul&amp;gt;
        &amp;lt;% foreach(vehicles, function(vehicle) { %&amp;gt;
            &amp;lt;li&amp;gt;type: &amp;lt;%= pluralize(vehicle.type) %&amp;gt;&amp;lt;%&amp;gt;
        &amp;lt;% }); %&amp;gt;
    &amp;lt;/ul&amp;gt;
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Methods&lt;/h3&gt;
&lt;h4&gt;compile&lt;/h4&gt;
&lt;p&gt;Transforms a template string into an efficient JavaScript function which can process data into a rendered string.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$.telligent.evolution.template.compile(template, extraHelpers, delimiters)
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;template&lt;/code&gt;: Either a raw JavaScript template string, or an ID of a &lt;code&gt;&amp;lt;script type=&amp;quot;text/html&amp;quot;&amp;gt;&lt;/code&gt; element containing an embedded template.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;extraHelpers&lt;/code&gt;: Optional functions made available for use within the template&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;delimiters&lt;/code&gt;: Optional object containing overrides for re-defining what delimiter formats are used. Override object must contain both &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;close&lt;/code&gt; delimiter keys and values.
&lt;ul&gt;
&lt;li&gt;
default:
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;open&lt;/code&gt;: &lt;code&gt;&amp;#39;&amp;lt;%&amp;#39;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;close&lt;/code&gt;: &lt;code&gt;&amp;#39;%&amp;gt;&amp;#39;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr class="generated-documentation-end" style="border-width:0;" /&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;
</description></item></channel></rss>