I was trying to create a basic app, using backbone, underscore, and Parse.
In my index.html, I tried including some scripts like this:
<script data-main="js/main" src="../bower_components/requirejs/require.js"></script> <script type="text/template" id="login-template" src="js/templates/login.js">
When I tried to do the following on my backbone part, it did not work.
template: _.template($('#login-template').html()); // ... render: function() { this.$el.html(this.template()); }
However, when I changed my script, and added it directly in the html document, it worked fine.
<script type="text/template" id="login-template"> <header id="header"></header> <div class="login"> <form class="login-form"> <h2>Log In</h2> <div class="error" style="display:none"></div> <input type="text" id="login-username" placeholder="Username" /> <input type="password" id="login-password" placeholder="Password" /> <button>Log In</button> </form> </script>
why is this? Is there any way to include templates from an external source in a <script>
tag?
(I cannot use $.get
for this case, as it's not supposed to use web server right now and keep getting XHR errors doing it normally.)
You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.
Definition and Usage The src attribute specifies the URL of an external script file. If you want to run the same JavaScript on several pages in a web site, you should create an external JavaScript file, instead of writing the same script over and over again.
By setting the type to "text/template", it's not a script that the browser can understand, and so the browser will simply ignore it. This allows you to put anything in there, which can then be extracted later and used by a templating library to generate HTML snippets.
JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.
The <script>
tag's src
attribute will cause the browser to make a HTTP request for login.js
. It won't, however, insert the response into the DOM. You'd need that to happen for your code to work.
Browsers don't do this for the simple reason that the HTML5 spec doesn't say that they should.
In particular, the scripting spec has lists of the actions that user agents must take when preparing a <script>
tag and executing its source. These lists don't instruct browsers to insert the script's source into the DOM. The inline approach works because the script source is already in the DOM.
You can see this behaviour by inspecting any <script>
tag with a src
attribute - after loading, parsing and executing its source, it will contain no child nodes.
You can load templates using AJAX requests, but it's not recommended - if your application has a small number of templates it's simpler to just include them in your main HTML file; if it has several you'd need a lot of server round trips to fetch them.
The best solution is usually a build step that compiles your templates into a single Object in a JavaScript file, which can be included like any other script.
With a step like this that compiles your templates into a variable called AppTemplates
, your code would look something like:
template: AppTemplates['templates/login.tpl']
Grunt has a task called grunt-contrib-jst that does this for Underscore.js templates. There are equivalent tasks for other template engines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With