I am currently building a website that uses NodeJS for the server, Express Handlebars(Just Handlebars but server side) , and hopefully AngularJS for some client side stuff.
AngularJS and Handlebars use the same syntax for templating
{{foo}}
This causes a problem where AngularJS code will be interpreted by Express Handlebars first, which will then throw an error because the data it is trying to pull only exists in Angular not Node.
Is there a way to get AngularJS and Express Handlebars to work together?
ng
helper in Express Handlebars.
Your first solution is possible, AngularJS allow to change the start/end symbols of text interpolation like this:
appModule.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});
Then you could use it in your template:
<div>{[{message}]}</div>
Also see: $interpolateProvider documentation
Hope this helps.
I would recommend using the AngularJS's data-binding syntax (what looks similar to Handlebars) and have your NodeJS server simply serve the static AngularJS source code. I prefer to offload the templating onto the client and therefore put less stress on your server, not to mention that AngularJS and other MVC frameworks (my favourite is EmberJS) are great for dynamically building the webpage.
I am a fan of Yeoman and here is a generator for building an Angular project served by NodeJS: https://github.com/yeoman/generator-angular
You can always use the ng-bind syntax instead:
<p ng-bind="user.profile.description"></p>
This is identical to
<p>{{user.profile.description}}</p>
From the Angular docs for ngBind:
Typically, you don't use ngBind directly, but instead you use the double curly markup like {{ expression }} which is similar but less verbose.
It is preferable to use ngBind instead of {{ expression }} if a template is momentarily displayed by the browser in its raw state before Angular compiles it. Since ngBind is an element attribute, it makes the bindings invisible to the user while the page is loading.
In order to maintain the AngularJS Style, your second solution is better, Create a helper in Express Handlebars.
References to the Handlebars Web Site: http://handlebarsjs.com/block_helpers.html, you can register a helper raw-helper
Handlebars.registerHelper('raw-helper', function(options) {
return options.fn();
});
and use it in your hbs template by putting it in a quadruple-stash {{{{
{{{{raw-helper}}}}
<div class="container" ng-controller="AppCtrl">
Total Members: {{members.length}}
</div>
{{{{/raw-helper}}}}