197

As you know, both angular and twig has common control construction - double curly braces. How can I change default value of Angular?

I know that I can do it in Twig, but in some projects I can't, only JS.

kangax
  • 38,898
  • 13
  • 99
  • 135
Meliborn
  • 6,495
  • 6
  • 34
  • 53
  • 2
    possible duplicate of [Angular JS custom delimiter](http://stackoverflow.com/questions/12923521/angular-js-custom-delimiter) – pkozlowski.opensource Dec 02 '12 at 17:37
  • 10
    Another twig-specific solution for moustache madness is to use the `verbatim` tag; e.g:`{% verbatim %}{{ angular_var }}{% endverbatim %}` to preserve your moustaches for AngularJS: http://twig.sensiolabs.org/doc/tags/verbatim.html – Darragh Enright Jun 05 '13 at 16:19
  • Not to author of question, but future readers: if you are looking for answer to this question, consider to avoid templates rendering on server side at all, if you can afford it (if your main content is inside authenticated zone or you main search engine as source of traffic is Google (they can parse JS SPA)). – OZ_ Feb 23 '15 at 00:17
  • 1
    @OZ_ The search engine argument against AngularJS and the like becomes quite redundant when using services like [prerender.io](http://prerender.io). – E. Sundin Feb 24 '15 at 23:18

11 Answers11

288

You can change the start and end interpolation tags using interpolateProvider service. One convenient place for this is at the module initialization time.

angular.module('myApp', []).config(function($interpolateProvider){
    $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});

https://docs.angularjs.org/api/ng/provider/$interpolateProvider

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
abhaga
  • 5,455
  • 2
  • 21
  • 20
  • 5
    I wouldn't use [[]], though. It could be bad in some bindings, like this one: `[[myObject[myArray[index]]` – Andrew Joslin Dec 02 '12 at 18:23
  • 1
    True. I use `{[{}]}` with Django although it is a little odd to type. I have updated the answer. – abhaga Dec 02 '12 at 18:26
  • 4
    Thanks! Ran into a similar issue with Jeykll / Liquid and Angular JS. I opted for {[ and ]}. – jfroom Apr 14 '13 at 00:32
  • 7
    Doesn't the semicolon need removed after the } on line 3? – CashIsClay May 07 '13 at 19:49
  • Is there any way to do this on a global level in Angular? Our website will have many, many modules on many different pages, and we don't want to forget to do this. – theY4Kman Dec 05 '13 at 16:30
  • Hi, It was just a doubt in mind, and I searched for this question, This is great solution, But Why can not we just use "ng-bind" instead – RONE Sep 03 '14 at 04:58
  • Since twig is the pre-processor that gets activated first when `{{something}}` is read, wouldn't it make more sense to adjust the settings of twig rather than angular? Are there alternatives to `{` curly braces in twig? It would be nice to keep angular as is, and modify the preprocessor. – ahnbizcad Sep 14 '14 at 18:52
91

This question appears answered, but a more elegant solution that hasn't been mentioned is to simply enclose the curly braces in quote marks between the twig curly braces, like so:

{{ '{{myModelName}}' }}

If you are using a variable for the contents, do this instead:

{{ '{{' ~ yourvariable ~ '}}' }}

You should use single quotes, not double quotes. Double quotes enable string interpolation by Twig so you have to be more careful with the contents, especially if you are using expressions.


If you still hate seeing all those curly braces, you can also create a simple macro to automate the process:

{% macro curly(contents) %}
   {{ '{{' ~ contents ~ '}}' }}
{% endmacro %}

Save it as a file and import it into your template. I am using ng for the name because it is short and sweet.

{% import "forms.html" as ng %}

Or you can put the macro at the top of your template and import it as _self (see here):

{% import _self as ng %}

Then use it as follows:

{{ ng.curly('myModelName') }}

This outputs:

{{myModelName}}

...and a follow up for those that use MtHaml alongside Twig. MtHaml enables the use of AngularJS curlies in the normal manner because any Twig code is accessed though - and = instead of {{ }}. For example:

Plain HTML + AngularJS:

<tr ng-repeat="product in products">
   <td> {{ product.name }} </td>
</tr>

MtHaml + AngularJS:

%tr(ng-repeat="product in products")
   %td {{ product.name }}

MtHaml + AngularJS with MtHaml-style Twig:

- set twigVariable = "somevalue"
= twigVariable
%tr(ng-repeat="product in products")
   %td {{ product.name }}
robert.corlett
  • 911
  • 6
  • 3
  • 6
    It's maybe not the most pleasant way, but I my opinion it's the only way to don't worry about compatibility issue. Ether modify Angular or Twig {{ could create compatibility issue or bad portability. – Léo Benoist Jan 27 '14 at 04:04
  • 1
    The solution provided only started to work for me when I did this `{{'{{contact.name}\}'}}` which prints `{{contact.name}}` as I wanted – Timo Huovinen Aug 24 '15 at 08:22
37

As mentioned in similar question about Django and AngularJS, trick with changing default symbols (in Twig or AngularJS) can provide incompatibility with third-party software, which will use these symbols. So best advice I found in google: https://groups.google.com/d/msg/symfony2/kyebufz4M00/8VhF1KWsSAEJ

TwigBundle does not provide a configuration for the lexer delimiters as changing them would forbid you to use any templates provided by shared bundles (including the exception templates provided by TwigBundle itself).

However, you could use the raw tag around your angular templates to avoid the pain of escaping all curly braces: http://twig.sensiolabs.org/doc/tags/raw.html -- Christophe | Stof

Tag was renamed to verbatim

OZ_
  • 12,492
  • 7
  • 50
  • 68
27

You can use too the attribute-based directive <p ng-bind="yourText"></p> is the same as <p>{{yourText}}</p>

pabloRN
  • 866
  • 10
  • 19
  • Even better would be using `

    ` to make the HTML valid
    – Jean-Marc Zimmer Nov 14 '18 at 10:29