14

Swig templates and AngularJS both use the double curly brace notation. How can the double curlies be escaped in Swig for Angular?

Jack Gerson
  • 257
  • 3
  • 8

3 Answers3

26

Double curlies can be escaped with

{% raw %}

eg: {% raw %}{{ foobar }}{% endraw %}

Forces the content to not be auto-escaped. All swig instructions will be ignored and the content will be rendered exactly as it was given. See Swig manual/tags/raw.

orszaczky
  • 13,301
  • 8
  • 47
  • 54
14

Why not replacing the {{}} with [[]] in the templates by configuring AngularJS to accept [[]] as the new {{}}. Try this in your Angular-App-Config (tried with angularjs-1.2.4):

config(['$interpolateProvider',
    function($interpolateProvider) {
        // Swig uses {{}} for variables which makes it clash with the use of {{}} in AngularJS.
        // Replaced use of {{}} with [[]] in AngularJS to make it work with Swig.
        $interpolateProvider.startSymbol('[[');
        $interpolateProvider.endSymbol(']]');
    }
])
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Martin
  • 641
  • 1
  • 9
  • 16
  • 5
    I only need to escape a single instance on the index layout served by Express, the rest of the pages are handled by Angular, so reconfiguring Angular just for this wouldn't make sense in my case. Might be a good solution for others though. – Jack Gerson Sep 09 '14 at 05:46
9

Instead of replacing interpolation sign of angular. Change defaults for swig. Following Code will do it.

var swig = require('swig'); 
swig.setDefaults({
   varControls: ['[[', ']]'] 
});
user1438038
  • 5,821
  • 6
  • 60
  • 94
Sandip Nirmal
  • 2,283
  • 21
  • 24
  • [http://paularmstrong.github.io/swig/docs/api/](http://paularmstrong.github.io/swig/docs/api/) – Sandip Nirmal Dec 02 '15 at 11:59
  • [http://node-swig.github.io/swig-templates/docs/api/#SwigOpts](http://node-swig.github.io/swig-templates/docs/api/#SwigOpts) – csum Aug 15 '19 at 22:42