Question

We are trying to use NodeJs with HoganJs for server side templating. However we are also wanting to use AngularJs for our client side needs.

The problem is that both HoganJs and AngularJs use "{{" and "}}" to full fill their compiler needs. Because of this Hogan strips out if at all there is a angular's "{{", because of the way hogan works.

My question is is there a out of the box solution that allows me to use both Angular and Hogan together, without clashing with each other.

If not, does anyone knows what/where/how to tweak one of these to make them love each other and work gracefully.

Thanks in advance...

Was it helpful?

Solution 2

Try with

Hogan.compile(text, {delimiters: '<% %>'});

so you can change the delimeters Hogan uses by passing the compile method an option overriding them.

http://comments.gmane.org/gmane.comp.lang.javascript.express/1426

NB

imo using a template system is useless using angularjs because of https://stackoverflow.com/a/20270422/356380

OTHER TIPS

If you're using express, you can change hogan's delimiters like so:

var app = express();
app.locals.delimiters = '<% %>';

Place the above before :

app.set('view engine', 'hjs');

Now in your page.hjs file, for the data { template : "Template test" }, you can do :

<p>This is a <% template %></p>

Alternative to changing Hogan delimeters as other answer shows... change Angular's! I did this while using doT (which also uses {{ and }}) and it works fine:

say you have this in your layout HTML:

<html ng-app="cooApp">

Add this script to call up Angular with custom delims (I'm also including reference to Angular just for clarification):

<script src='//ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.min.js'></script>
<script>
    var cooApp = angular.module('cooApp', [], function($interpolateProvider) {
        $interpolateProvider.startSymbol('{%');
        $interpolateProvider.endSymbol('%}');
    });
</script>

Now just use {% and %} for Angular stuff.

Without changing delimiters, on Angular 1.x you can use the ng-non-bindable directive for in the elements that uses HoganJS, Mustache or any other code of this kind:

Example:

<div>
  {{angularjs_variable}}
  <div ng-non-bindable>{{hogan_variable}}</div>
</div>

This is useful if the element contains what appears to be AngularJS directives and bindings but which should be ignored by AngularJS. [...]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top