Templates with Handlebars in Backbone.js

Simple example of templates in Backbone.js using handlebars as the templating engine.

by oscard

HTML

<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
<div id="navigation">
    <a href="#/home">Home</a>
    <a href="#/about">About</a> 
</div>    
<div id="content">
</div>

<script type="text/template" id="home-template">
    <h1>Home Page</h1>
   {{ greeting }}
</script>

<script type="text/template" id="about-template">
    <h1>About Page</h1>
    {{ content }}
</script>

SCSS

body { margin: 30px;}
body {font-family: Arial;}
h1{font-weight:bold;font-size:16px;}
p{margin:5px 0px 5px 0px;}
div{margin:15px;}
div a{margin:5px;}

JavaScript

{
    "articles": [{
        "id": 6,
        "date_published": 1454609040,
        "date_updated": 1454664833,
        "title": "Acheter ou louer ? Le match",
        "url": "http://bnpparibas.webedia.fr//acheter-ou-louer-le-match/6",
        "nb_images": 2,
        "tags": [{
            "name": "Logement",
            "id": 6,
            "background_color": "",
            "font_color": ""
        }, {
            "name": "Achat Immobilier",
            "id": 11,
            "background_color": "",
            "font_color": ""
        }],
        "medias": [{
            "id": 11,
            "image": "http://static1.fr.bnpparibas.betawebedia.com/articles/6/6/@/11-achat-immobilier-le-classement-des-vil-[WBFORMAT]-1.png",
            "copyright": "DR",
            "legend": "Achat immobilier : le classement des villes par années de rentabilité (source : Meilleurtaux.com)",
            "type_id": 1
        }, {
            "id": 6,
            "image": "http://static1.fr.bnpparibas.betawebedia.com/articles/6/6/@/6-acheter-rime-avec-frais-financiers-et-se-[WBFORMAT]-2.jpg",
            "copyright": "Getty Images, Jason Robert Black",
            "legend": "Acheter rime avec frais financiers et sécurité.",
            "type_id": 1
        }],
        "nb_videos": 0,
        "text_html": "<p>80 % des Fran&ccedil;ais veulent acc&eacute;der &agrave; la propri&eacute;t&eacute;, mais si celle-ci offre des avantages ind&eacute;niables, elle soumet aussi &agrave; des obligations. Alors, vaut-il mieux acheter ou louer ? C&rsquo;est notre match du jour.</p><p>Ca y est, vous et votre conjoint(e) &ecirc;tes d&eacute;sormais dans une situation financi&egrave;re stable, et vous envisagez l&rsquo;avenir avec plus de s&eacute;r&eacute;nit&eacute;. La prochaine &eacute;tape serait donc logiquement de faire l&rsquo;acquisition d&rsquo;un bien immobilier. Avez-vous bien tout pris en compte avant de vous lancer ? Faisons le point.</p>\n<h2>Sur le prix ? Match nul</h2>\n<p>A...