1

I am trying to display conditional text in a Pyramid Chameleon template. Basically, checking if the dictionary key 'maxed_out_alerts' is empty (false) or has a string 'yes' in it.

<p tal:condition="not:maxed_out_alerts"><h3>Maxed Out.</h3></p>
<p tal:condition="maxed_out_alerts"><h3>Not Maxed Out</h3></p>

When 'maxed_out_alerts' is an empty string, 'Maxed Out' is only displayed (correctly). However, If 'maxed_out_alerts' contains 'yes' string both 'Maxed Out' and "Not Maxed Out' are displayed (incorrectly).

It seems that the NOT is always evaluated to a true condition. It should display one or the other messages not both. What am I doing wrong? thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
fat fantasma
  • 7,483
  • 15
  • 48
  • 66
  • It is unclear to me what `maxed_out_alerts` *is*; is it a python string? What does `Python representation` say when added to your document? – Martijn Pieters Jul 16 '13 at 07:19
  • @Martijn. Thanks for replying. "maxed_out_alerts" is a key in a python dictionary with the value being 'yes'. The dictionary is being passed back to the chameleon template. Your code:Python representation displays the value 'yes' in my template. – fat fantasma Jul 16 '13 at 17:37
  • I followed the following info from http://www.faqs.org/docs/ZopeBook/AdvZPT.html ----Not Expressions Not expressions let you negate the value of other expressions. For example:

    There are no contained objects.

    Not expressions return true when the expression they are applied to is false, and vice versa. In Zope, non-existent variables, zero, empty strings, empty sequences, nothing, and None are considered false, while everything else is true.
    – fat fantasma Jul 16 '13 at 17:44
  • Are you looping over the dictionary? Are there multiple keys? – Martijn Pieters Jul 16 '13 at 17:46
  • It's just one dictionary with the Key, Value as {'maxed_out_alerts':'yes'} or {'maxed_out_alerts':''}. I want my template to display certain text depending on the conditions. If 'yes' display it one way. If empty another. – fat fantasma Jul 16 '13 at 23:00
  • So, how is `maxed_out_alerts` *set* in your template? Or is it passed in from the view? – Martijn Pieters Jul 16 '13 at 23:03
  • This code does in fact work. I was doing something else incorrectly. thanks again. – fat fantasma Jul 24 '13 at 18:19

2 Answers2

3

For TAL conditionals in python you can say python: and then use a python syntax conditional

<p tal:condition="python:len(maxed_out_alerts) > 0"><h3>Maxed Out.</h3></p>
Stephan
  • 16,509
  • 7
  • 35
  • 61
  • Thanks for everybody's responses. They all work including mine. I was doing something stupid in my code that my blurry eyes just could not see. However, I like this answer the best and will be using it. – fat fantasma Jul 24 '13 at 18:19
1

It could help if you save boolean state in a boolean variable. By storing this information in a string you run into such problems you are facing right now. That's what builtin python types are made for - use them.

As a pyramid developer I would advice to move the logic to evaluate the current value of maxed_out_alerts into a string into a view method and pass the computed string in a dictionary to the renderer/template. This way you can even create tests for the view logic - any pyramid tutorial, simple or advanced shows you how to do that.

A good start for any simple logic - imagine logic gets more complicated or you even have to translate the text for the template.

@view_config(name="yourname", renderer='templates/yourtemplate.pt')
def myview(request):
    """
    #get boolean state from model
    #could be that you want to have it the other way round
    #or do it by using python ternary operator - a if test else b 
    if model['maxed_out_alerts'] == True:
        maxed_out_alerts = 'Maxed Out'
    else:
        maxed_out_alerts = 'Not Maxed Out'


    return dict(maxed_out_alerts = maxed_out_alerts)

In your Template

<h3 tal:content="maxed_out_alerts">text for maxed out alerts</h3>

or

<h3>${maxed_out_alerts}</h3>
Sascha Gottfried
  • 3,303
  • 20
  • 30