jsRender - How to call an external template from a nested template

心不动则不痛 提交于 2020-01-11 03:53:22

问题


I'm really new to jsRender(only a couple of days) and I just can say ..I love it!!

One article I found really useful was this one by john papa

So far I've been able to do what I want(everything in the same page), but one thing John says in his article :

If a template is defined inside of a tag in the same page that it’s used, then the template isn’t as reusable as it could be.

made me want to try and see if could move all my templates into separate files.

Following John's instructions I created a jsrender.utils.js file that retrieves the template using the $.get function.

Now the problem is that it only works for templates that doens't call other templates from inside, like my template:

_estructura.tmpl.html

    <tr>
    <td>
        {{!------------------------------Start - Estructure------------------------}}
        <fieldset id="e{{>nivelEst}}">
            <legend>"Estructura para Retorno {{>nivelEst}}"</legend>
            <div>
                <span>Tipo Expresion</span>
                <select id="tipoExp_e{{>nivelEst}}">
                    {{for tipoExp tmpl="#dropdown" /}}
                </select>
            </div>
            <hr />
            {{!-------------------------Start- Sentence-----------------------}}
            <fieldset id="{{>idSent}}">
                <div>
                    <select id="subTipoExp_{{>idSent}}">
                        {{for subTipoExp tmpl="#dropdown" /}}
                    </select>
                </div>
                <br />
                <div>
                    {{!-----------------Start - Expression--------------------}}
                    <table id="tbExp_{{>idSent}}" class="list" align="center" cellpadding="0" cellspacing="0">
                        <tbody>
                            {{if idSent tmpl="#if" /}}
                        </tbody>
                        <tfoot>
                            {{if idSent tmpl="#else" /}}
                        </tfoot>
                    </table>
                    {{!----------------------End - Expression----------------}}
                </div>
            </fieldset>
            {{!-------------------------End - Sentence -------------------------}}
        </fieldset>
        {{!----------------------------End - Estructure  -------------------------}}
    </td>
</tr>

Here I need to call other templates such as : #if, #else, and #dropdown

They all are very simple and work perfectly when called directly

_dropdown.tmpl.html

<option value="{{>value}}">{{>text}}</option>

_if.tmpl.html

<tr>
    <td>
         <span class="rightAlig">IF(</span><input type="text" id="exp1_tbExp_{{>idSent}}" class="conditionInput" />
    </td>
    <td>
          :<input type="text" id="ret1_tbExp_{{>idSent}}" />)
    </td>
    <td>
    </td>
</tr>

_else.tmpl.html

<tr>
    <td colspan="3" style="text-align: left;">
        <input type="button" id="btnAñadir_tbExp_{{>idSent}}" value="+ Add" class="button small blue" />
    </td>
</tr>
<tr>
    <td colspan="3">
        <span>Else</span>(<input type="text" id="else_tbExp_{{>idSent}}" />)
        <input type="hidden" id="c_tbExp_{{>idSent}}" value="1" />
    </td>
</tr>

When I call the "_estructura.tmpl.html" template which in turn calls the dropdown, if, and else templates it just doenst do the logic inside those templates(I guess because it can't find them)

here's is how I call the "_estructura.tmpl.html" template:

var data_Est = new Object();
data_Est.nivelEst = counter;
data_Est.idSent = idSent;
data_Est.tipoExp = tipoEsp_data;
data_Est.subTipoExp = subTipoEsp_data;
my.utils.renderExternalTemplate("estructura", "#tbEstructuras >tbody", data_Est);

and here's is the data for the two dropdown lists of the main template: "tipoEsp_data" and "subTipoEsp_data"

var tipoEsp_data = [{ "value": 1, "text": "Expresión Condicional" }, { "value": 2, "text": "Lógico/Matemática" }, { "value": 3, "text": "Matriz de doble Entrada" }, { "value": 4, "text": "Árbol de decisiones"}];
var subTipoEsp_data = [{ "value": 1, "text": "Si.Pero.Si" }, { "value": 2, "text": "Si" }, { "value": 3, "text": "Fórmula Matemática"}];

As I told you at the beginning I really new to jsrender and it would just amazing if you could help out with this problem.

Thanks in advance.


回答1:


There are documentation topics that explain how to do remote loading of templates:

http://www.jsviews.com/#samples/jsr/composition/remote-tmpl

http://www.jsviews.com/#compiletmpl

You need to load all your templates before calling render() or link(). Your renderExternalTemplate() call needs to return a promise, and you then combine all such promises in a $.when().

Here is how it is done in the sample:

function showPeople(people) {
  $.when(

    lazyGetTemplate("people"),
// '<div>{{:name}} lives in {{for address tmpl="address" /}}</div>'
// fetched from www.jsviews.com/samples/resources/templates/people.js

    lazyGetTemplate("address")
// Template: '<b>{{>city}}</b>'
// fetched from www.jsviews.com/samples/resources/templates/address.js

  )
    .done(function() {
      // Render once all templates for template composition are loaded
      var html = $.templates.people.render(people);
      $("#peopleList").html(html);
    });
}


来源:https://stackoverflow.com/questions/19100184/jsrender-how-to-call-an-external-template-from-a-nested-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!