Search code examples
javascriptjsrenderjsviews

Is it possible for jsrender to render a field name dynamically?


Is it possible to have a dynamic field name in jsrender like this.

{{<cellcontent>}}

Common template as generic one. Cell content value is bound to a datasource. Is it possible?


Solution

  • Your question is not very clear, but if you are asking about using JsRender templates along with data-binding so that when the data changes the rendered result is automatically updated, dynamically, well yes, that is what JsViews is all about: http://www.jsviews.com/#jsviews. (JsViews is a data-binding layer on top of JsRender).

    Here is a simple example:

    var tmpl = $.templates("#myTemplate"),
        data = {name: "Jo"};
    
    tmpl.link("#content", data);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="//www.jsviews.com/download/jsviews.js"></script>
    
    <script id="myTemplate" type="text/x-jsrender">
        {^{:name}} <br/>
        <input data-link="name trigger=true" />
    </script>
    
    <div id="content"></div>