Search code examples
javascripthtmltemplating-engine

Simple client-side templating directly inside HTML


I'm searching for a simple, small templating engine which usage would be similar to this:

<html>
<head>
    <script src="templating.js"></script>
    <script>
        var data = {
            person: {
                name: "John",
                age: 28
            }
        };
        templating(data);
    </script>
    <title>Site</title>
</head>
<body>

My name is {{person.name}} and I'm {{person.age}} years old.

</body>
</html>

What should result in a page rendering:

My name is John and I'm 28 years old.

I've looked over every single templating engine found here: http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more and it I dind't find one which would work like that.

So the question is: am I missing something here, or is this kind of templating not a good practice for some reason?


Solution

  • Take a look at https://angularjs.org/

    It uses a very similar syntax to what you provided

    <!doctype html>
    <html ng-app>
      <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.16/angular.min.js"></script>
      </head>
      <body>
        <div>
          <label>Name:</label>
          <input type="text" ng-model="yourName" placeholder="Enter a name here">
          <hr>
          <h1>Hello {{yourName}}!</h1>
        </div>
      </body>
    </html>
    

    And has some extra features regarding templating you might find useful.