Re: little problem with the django template language

2008-06-20 Thread Daniel Roseman

On Jun 20, 8:50 am, mark <[EMAIL PROTECTED]> wrote:
> I guess it was a little confusing how I formulated my problem. I just
> try again. Sorry for that.
>
> In Python what I want to do would look like the following:
> 
> item1 = '/test/something/'
> item2 = '/test/somethingelse/'
> 
> if item2.startswith(item1):
>     do_the_trick()
>
> Now since Django has this nice template language I need to do
> something like that:
> {% ifstartswith item2 item1 %}
>     ...do the trick
> {% endifstartswith %}
>
> is that possible?


No, you can't do that with the basic template language - it's
deliberately restricted to a few common operations. However you can
very easily define a custom template filter to do this. In your
application directory, create a subdirectory called templatetags and
put in a blank __init__.py and a file called (eg) mytags.py containing
something like:

from django import template
register = template.Library()

@register.filter
def startswith(val1, val2)
return val1.startswith(val2)


Then in your template:

{% load mytags %}
{% if item1|startswith:item2 %}
...
{% endif %}


--
DR
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: little problem with the django template language

2008-06-20 Thread mark

I guess it was a little confusing how I formulated my problem. I just
try again. Sorry for that.

In Python what I want to do would look like the following:

item1 = '/test/something/'
item2 = '/test/somethingelse/'

if item2.startswith(item1):
do_the_trick()


Now since Django has this nice template language I need to do
something like that:
{% ifstartswith item2 item1 %}
...do the trick
{% endifstartswith %}

is that possible?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: little problem with the django template language

2008-06-19 Thread phillc

yes they are strings,
yes in python if you do

if item1:
pass

it will evaluate the string as a true/false depending on existance.

however, comparing two strings for equality... checks if they are
equal.

{% if item1 %}
{% if item2 %}
blah
{% endif %}
{% endif %}


On Jun 19, 1:50 pm, mark <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am stuck, please help.
>
> This is what I am trying to do:
>
> {% ifequal item1 item2 %}
>     ...do what I want
>
> item1 = '/test/something/'
> item2 = '/test/somethingelse/'
>
> or
>
> item1 = '/test/'
> tem2 = '/test/simething'
>
> both should evaluate to true and execute the if block.
>
> I experimented with 'item|slice[:]' but I could not find out how to
> determin the position (index) of the second '/'.
>
> Please note I am a beginner in Django and I should probably know how
> to solve this but obviously I do not :-((
>
> Best Regards,
> Mark
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: little problem with the django template language

2008-06-19 Thread roj

Hi,

If you are trying to compare url string then use
request.path
which will give you URL and split by "/"

is that work for you?

Cheers!
Roj
Django Debugging Resource
http://django.freelancernepal.com


On Jun 19, 6:50 pm, mark <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am stuck, please help.
>
> This is what I am trying to do:
>
> {% ifequal item1 item2 %}
> ...do what I want
>
> item1 = '/test/something/'
> item2 = '/test/somethingelse/'
>
> or
>
> item1 = '/test/'
> tem2 = '/test/simething'
>
> both should evaluate to true and execute the if block.
>
> I experimented with 'item|slice[:]' but I could not find out how to
> determin the position (index) of the second '/'.
>
> Please note I am a beginner in Django and I should probably know how
> to solve this but obviously I do not :-((
>
> Best Regards,
> Mark

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---