Question

I'm using pyramid with Chameleon and I try to add a class to a link dependent of the page number with tal:condition. When I replace page_number for a string it's working but the code below isn't. So something is wrong with the page_number. Any help would be appreciated

<div tal:repeat="page_number range(pages['min'],(pages['max']+1))">
    <a tal:condition="request.matchdict['page_number'] is page_number"
        href="${request.route_url('results', page_number=page_number, searchQuery=request.matchdict['searchQuery'])}"  
        class="page-nav-link">${page_number}
     </a>
     <a tal:condition="request.matchdict['page_number'] is not page_number"
        href="${request.route_url('results', page_number=page_number, searchQuery=request.matchdict['searchQuery'])}"  
        class="page-nav-selected">${page_number}
     </a>
</div>
Was it helpful?

Solution

First problem : you're using the identity operator ("is") instead of the equality operator ("=="). The fact that it was "working" (kind of...) is an accident due to an implementation detail of cpython.

Second problem: well this is kind of a wild guess since I don't know much about what's really in your request.matchdict, but : range() (if it's python's builtin range function at least returns a list of ints, and I bet request.matchdict['page_number'] is a string. Try this instead (if allowed by tour template engine):

<a tal:condition="request.matchdict['page_number'] == str(page_number)"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top