4

Since I wanted to use AngularJS in combination with Flask, I searched for a cool tool to handle these frameworks properly since Jinja and Angular would have problems with each other. I found Triangle which is pretty cool and working but just up to a certain point. Stuff like this works for example:

<a ng-show="post.link" href="{{post.link|angular}}">
    {{post.title|angular}}
</a>

But on the other hand this does not work:

<span>
    <a href="#/posts/{{$index|angular}}">Comments</a>
</span>

When I try to do that, I receive following error

jinja2.exceptions.TemplateSyntaxError

TemplateSyntaxError: unexpected char u'$' at 875

Am I doing something wrong or is the framework just limited in that case? Help is highly appreciated.

BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70

2 Answers2

5

As the error message is saying, $ cannot be used in Jinja as part of a variable.

Instead, you'll need to change Angular's delimiter notation:

var app = angular.module('Application', []);

app.config(['$interpolateProvider', function($interpolateProvider) {
  $interpolateProvider.startSymbol('{a');
  $interpolateProvider.endSymbol('a}');
}]);

Whatever is chosen for the start and end symbols will act as the new delimiters. In this case, you would express a variable > to Angular using {a some_variable a}.

Community
  • 1
  • 1
Celeo
  • 5,583
  • 8
  • 39
  • 41
4

Just looking at the angular filter code, and it looks like that quoting a string and applying angular filter should work fine.

<span>
    <a href="#/posts/{{'$index'|angular}}">Comments</a>
</span>

Below is the angular filter code copied from https://github.com/morgan-del/flask-triangle

def angular_filter(value):
    """
    A filter to tell Jinja2 that a variable is for the AngularJS template
    engine.
    If the variable is undefined, its name will be used in the AngularJS
    template, otherwise, its content will be used.
    """

    if is_undefined(value):
        return '{{{{{}}}}}'.format(value._undefined_name)
    if type(value) is bool:
        value = repr(value).lower()
    return '{{{{{}}}}}'.format(value)
Boris
  • 2,275
  • 20
  • 21