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">
        <li>Hic sunt dracones</li>
<script type="text/javascript">
    template = $('#template1').html();


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?


source to share

1 answer


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:

$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





All Articles