Is it possible to load undescore templates externally with javascript?

Asked

Viewed 62 times

3

I usually use Template Engines in order to render HTML via Javascript. I commonly use undescore.js.

I really like this way to make it easier to create HTML with Templates.

Example:

Somewhere in the HTML:

<script type="text/template" id="tpl-template">
   Meu nome é <b><%= nome %>
</script>

In the JS file

var tplTemplate = _.template($('#tpl-template').html());

$('body').append(tplTemplate({nome: 'Wallace'}));

However, it is always necessary to leave this code next to the HTML code. I wish I could organize these codes into a separate file (as a tpl.js), for example, but I haven’t yet found a way.

I believe that through the attribute src there is no way to do that - if there is one correct me.

I need to know if there is any way to do this in Javascript.

  • Any special reason why I took a -1? Was my question so stupid? kkkkkkkk

  • 1

    You want to make a new Angularjs. Ps1 - I’m not the one who said no. Ps2 - You ask many questions, must be why the -1. Ps3 - I don’t know the answer. Ps4 - It has already begun national manufacturing, will lower the price.

  • I don’t understand PS4.

  • Many questions is not a criterion for negative (I’m glad my wife doesn’t give me negative votes) kkkkkkkk

  • http://olhardigital.uol.com.br/noticia/ps4-produzido-no-brasil-ja-esta-a-venda-por-r-2-6-mil/51846

  • Sopt’s criteria is not, but it may be someone’s criteria.

  • Ps4 was a joke... You have good mood

  • So you mean the angle already does it? Thanks for the tip, I just got to it now

Show 4 more comments

1 answer

0

Friend, the best thing to do is to automate the pre-compilation process, if you want to know how to do it using Grunt, you can read more about this link;

you can also catch the recover script from the script by accessing the source property through the source property, for example, if you do this:

console.log(_.template($('#tpl-template').html()).source)

you will possibly have the following exit:

function(obj){
    var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
    with(obj||{}){
        __p+='    Meu nome é <b>'+
            ((__t=( nome ))==null?'':__t)+
            '</b>\n';
    }
    return __p;
}

then basically you would only need to have the following code in a separate JS file:

var templates = {};
templates.meuNome = function(obj){
    var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
    with(obj||{}){
        __p+='    Meu nome é <b>'+
            ((__t=( nome ))==null?'':__t)+
            '</b>\n';
    }
    return __p;
}

in your main script, you would just have to use the templates.meuNome({nome: 'Wallace'}), without the need to have the tag script#tpl-template on the page.

finally a small example with a utility to generate the Scripts:

var textTemplate = document.getElementById("textTemplate");
var textPreCompiled = document.getElementById("textPreCompiled");
var txtNomeTemplate = document.getElementById("txtNomeTemplate");
var btPrecompilar = document.getElementById("btPrecompilar");

btPrecompilar.addEventListener("click", function (event) {
    var tmplSource = textTemplate.value;
    var template = _.template(tmplSource);

    textPreCompiled.value = "var " + txtNomeTemplate.value + " = " + template.source;
});
textarea {
    width: 100%;
    height: 240px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<textarea id="textTemplate">
  Meu nome é <b><%= nome %></b>
</textarea>
<label>
  Nome template:
  <input id="txtNomeTemplate" type="text" value="tplTemplate" />
</label>
<input id="btPrecompilar" type="button" value="PreCompilar Template" />
<textarea id="textPreCompiled">

</textarea>
<script type="text/template" id="tmplNome">

</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.