Python's 'startswith' in Jinja2/Flask

6,779 views
Skip to first unread message

Paweł Jaworowsky

unread,
Feb 5, 2015, 9:47:08 AM2/5/15
to salt-...@googlegroups.com
I'm looking for method/way which is similar to python's startswith.
What I would like to do is link some fields in table which start with "i-".

My steps:

1. I have created filter, which return True/False:
        
       
@app.template_filter('startswith')
       
def starts_with(field):
           
if field.startswith("i-"):
                   
return True
           
return False

   
then linked it to template:
 
   
 {% for field in row %}
               
{% if {{ field | startswith }} %}
                   
<td><a href="{{ url_for('munin') }}">{{ field | table_field | safe }}</a></td>
               
{% else %}
                   
<td>{{ field | table_field | safe}}</td>
               
{% endif %}
         
{% endfor %}




Unfortunatetly, it doesn't work.

Second step. I did it without filter, but in template

    {% for field in row %}
               
{% if field[:2] == 'i-' %}
                   
<td><a href="{{ url_for('munin') }}">{{ field | table_field | safe }}</a></td>
               
{% else %}
                   
<td>{{ field | table_field | safe}}</td>
               
{% endif %}
         
{% endfor %}


That works, but to that template are sending different datas, and it works only for this case. I'm thinking that [:2] could be buggy a little bit.

So I try to write filter or maybe there is some method which I skip in documentation. 

Thanks for advice. 

Nicholas Capo

unread,
Feb 5, 2015, 9:55:53 AM2/5/15
to salt-...@googlegroups.com

I don't quite understand where the distinctions are between python and jinja, but in this case they are the same.

We use this sort of statement all the time in our jinja templates:

{% if pillar['somekey'].startswith('foo') %}

Hope that helps,

Nicholas


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paweł Jaworowsky

unread,
Feb 5, 2015, 10:20:22 AM2/5/15
to salt-...@googlegroups.com
Thanks for answer. The problem is it seems doesn't work in my case. 

I'm getting:

             
Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.


However, when I change that if to something stupid or delete it, page works (of course without effect)

Paweł Jaworowsky

unread,
Feb 5, 2015, 10:46:47 AM2/5/15
to salt-...@googlegroups.com
Ok, i figure out why it doesn't work, but I have no idea how to fix it :D

I'm getting error, because startswith required strings, sometimes my "field" is list. That's why I have filter on it called "table_field". So, my question is.. how to put filter and then execute "startswith"?
This doesnt work, at all:

{{ field | table_field }}.startswith("i-")
Reply all
Reply to author
Forward
0 new messages