How can my chameleon template accept message flashes from the pyramid framework?
Asked Answered
D

1

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
Didymous answered 26/6, 2012 at 3:32 Comment(1)
there is no such preferenceFluttery
C
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.

Codeine answered 26/6, 2012 at 12:30 Comment(1)
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.Jackboot

© 2022 - 2024 — McMap. All rights reserved.