Avoiding ambiguous mustaches from Jinja2 that includes jQuery templates

前端 未结 2 1594
情书的邮戳
情书的邮戳 2021-01-31 03:24

I\'m trying to insert jQuery templates into Jinja2 templates. Alas they both (in the default setup) use the mustaches {{ & }} to indicate expressio

2条回答
  •  花落未央
    2021-01-31 04:27

    I have found this via google while experimenting with polymer but did not like the proposed solution, so another alternative: Use filters.

    In your python code define a filter:

    #Filter to create curly braces
    @app.template_filter('curly')
    def curly(value):
        #Handle value as string  {{'foo'|curly}}
        if(isinstance(value,str)):
            return_value = value
        #Handle value directly. {{foo|curly}}
        else:
            return_value = value._undefined_name
        return "{{" + return_value + "}}"
    

    Then in your template you can use {{'foo'|curly}} or {{foo|curly}}

    PS: If you don't use flask I think you can't use the decorator but have to register the filter explicitly instead: environment.filters['curly'] = curly.

提交回复
热议问题