Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top