1

Is there any possibility to use template filters in Chameleon as I'm used to in Mako? In Mako one could do something like

${s | filter}

where filter is a function mapping strings to strings and Mako applied filter to s and outputs it (after escaping HTML entities). So one could easily do things like

${s | nl2br}

I'm aware that the Chameleon way of doing it is Line Breaking in Chameleon. But I have more applications for this filters so I'm wondering if something similar is supported in Chameleon.

Community
  • 1
  • 1
born
  • 656
  • 1
  • 6
  • 21

3 Answers3

1

No, there isn't. Do the processing in your view instead, or use python code in your TALES expressions.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

In Chameleon, the pipe operator (" | ") operator means try-except, instead of "apply filter".

But since the expression language is simply Python, it shouldn't be necessary with such syntactic sugar.

Also, Chameleon has an import: expression type that can help you import helper functions:

<div tal:define="h import: helpers">
  ...
  ${structure: h.nl2br(s)}

Note that if nl2br returned an object that implemented an __html__() method, Chameleon would call this method to produce the output, and you wouldn't need structure: to avoid the HTML-escape.

That said, it's true that Mako's filter operator sometimes makes for more readable template code.

malthe
  • 1,237
  • 13
  • 25
0

The Chameleon way is

${nl2br(s)}

I'm doing it like this

views.py

form helpers import nl2br
def my_view(request):
    return {
        'nl2br':nl2br,
    }

template.pt

${structure: nl2br(s)}
bismigalis
  • 171
  • 1
  • 8
  • I'm not too sure but, this only works if I disable the encoding of htmlentities of Chameleon, right? This is not a good idea - at least in my situation :-/ – born Feb 24 '13 at 16:43
  • add structure: if output is to be interpreted as html – bismigalis Feb 24 '13 at 18:29
  • just as a warning to later readers: adding structure to the output can imply a major security bug. – born Feb 24 '13 at 18:30