3

Recently I've been exploring Backbone, and they do encourage using a template engine.

Now, I've got a bunch of html files that I want to display using that, and I will most likely have to edit them often in the development stage. The most tutorials I read recomment 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'd like to know if this can be made to work with the src-attribute to include the template files. Or, if not, what is the usual approach to load template files?

1 Answer 1

2

Using src for this is not possible. You could include a JavaScript file though which is generated on the server from separate template files; with PHP it could be as simple as this:

<?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 this file with a <script> tag then gives you an object templates where you can acccess the various templates via templates.foo, templates.bar, etc.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.