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.
lazyGetTemplate
method Boris, thank you. – Elison