Browser templates with src

I recently studied Backbone and they encourage the use of the templating engine.

Now I have a bunch of html files that I want to display with this, and I will likely have to edit them often during development. In most tutorials, I read the recommendation something like this:

<script type="text/template" id="template1">
    <ul>
        <li>Hic sunt dracones</li>
    </ul>
</script>
<script type="text/javascript">
    template = $('#template1').html();
<script>

      

So I would like to know if this can be done to work with an attribute src

to include the template files. Or, if not, what is the common approach for downloading template files?

+3


source to share


1 answer


src

It cannot be used for this. You can include a JavaScript file that is generated on the server from separate template files; with PHP it can be as simple as:

<?php
$templates = array(
    'foo' => file_get_contents('foo.html'),
    'bar' => file_get_contents('bar.html'),
);
header('Content-type: text/javascript');
echo 'var templates = ' . json_encode($templates);
?>

      



Loading that file with a tag <script>

then gives you an object templates

where you can access various templates via templates.foo

, templates.bar

etc.

+2


source







All Articles