6

I'm learning pyramid and it seems they are trying to get people to use chameleon instead of mako so I thought I'd give chameleon a chance. I like it so far and I can do basic things in the template such as if and for loops but I'm not sure how to get message flashes to appear.

In the pyramid tutorial they do this in a todo list but in the wiki example they do not. According to the instructions about sessions and using the todolist tutorial as a example I have been able to get my app to create messages but I'm unable to receive them in my template. In a nutshell, I'm wondering if chameleon has a equivalent of this mako code:

  % if request.session.peek_flash():
  <div id="flash">
    <% flash = request.session.pop_flash() %>
    % for message in flash:
    ${message}<br>
    % endfor
  </div>
  % endif
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • [there is no such preference](https://groups.google.com/d/msg/pylons-discuss/enal9DcMiAM/T_b4Zoe1crMJ) – tshepang Feb 03 '13 at 21:13

1 Answers1

10

The (untested) equivalent in chameleon is:

<div id="flash" tal:condition="request.session.peek_flash()">
  <span tal:omit-tag="" 
        tal:repeat="message request.session.pop_flash()">
      ${message}<br>
  </span>
</div>

The tal:omit-tag attribute is optional; it drops the <span> tag from the output, as it is only used as dummy tag to attach the repeat to. By dropping it the output of the Chameleon template will match the Mako example.

See the Chameleon documentation for an introduction and full specification of how the template language works.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • If a user is yet to access your site, the access of ``request.session`` will automatically create a session. Whilst this may not be an issue, it means that if only a few users will have sessions (eg admins only), then you're adding unneeded overhead to your server and your clients who may/will never have a session. If this concerns you, my suggestion is to check ``'session' in vars(request)`` before referencing ``request.session`` at all. – davidjb May 23 '14 at 03:04