Random string template tag

252 views
Skip to first unread message

Bryan Veloso

unread,
Jul 3, 2007, 9:45:07 PM7/3/07
to Django users
I don't know if this exists or not, but I'd like to insert a tag (much
like cycle) that allows the random selection of a string. I'm trying
to randomly assign a class to a <div> to simulate "random image
rotator" type functionality. Something like...

<div class="{% random class1,class2,class3,class4 %}">

Any ideas?

Tim Chase

unread,
Jul 3, 2007, 10:06:07 PM7/3/07
to django...@googlegroups.com


Sounds like you want to make a custom template tag that uses
random.choice() to make the choice for you. I haven't tested the
code below, but it's a starting point for what you describe:

from random import choice
from django import template
from django.template import resolve_variable
class RandomChoiceNode(template.Node):
def __init__(self, *choices):
self.choices = choices

def render(self, context):
try:
which = choice(self.choices)
result = resolve_variable(which, context)
return result
except template.VariableDoesNotExist:
return ''

http://www.djangoproject.com/documentation/templates_python/#passing-template-variables-to-the-tag

Fiddle accordingly.

-tim


Russell Keith-Magee

unread,
Jul 3, 2007, 11:02:01 PM7/3/07
to django...@googlegroups.com

One option is to use the 'random' filter; your context would need to
define the list of possible strings:

Context({
'classes': ['class1','class2','class3','class4']
})

but then your template could use:

{{ classes|random }}

Yours,
Russ Magee %-)

Bryan Veloso

unread,
Jul 3, 2007, 11:06:43 PM7/3/07
to Django users
> One option is to use the 'random' filter; your context would need to
> define the list of possible strings:
>
> Context({
> 'classes': ['class1','class2','class3','class4']
>
> })
>
> but then your template could use:
>
> {{ classes|random }}

Can you serve Context to the base template?
I thought of that earlier, but I couldn't think of how to give it
Context.

Russell Keith-Magee

unread,
Jul 3, 2007, 11:17:18 PM7/3/07
to django...@googlegroups.com
On 7/4/07, Bryan Veloso <bryan...@gmail.com> wrote:
>
> > {{ classes|random }}
>
> Can you serve Context to the base template?
> I thought of that earlier, but I couldn't think of how to give it
> Context.

What do you mean? You pass a context to the template renderer; if the
template you render references a base template, then it will get the
same contexts that the renderer was given.

If you want to hardcode the availability of the 'classes' variable in
every context (so you don't have to remember to define it every time),
you could write a context processor. See
django.core.context_processors for examples.

Russ %-)

Bryan Veloso

unread,
Jul 3, 2007, 11:21:52 PM7/3/07
to Django users
> If you want to hardcode the availability of the 'classes' variable in
> every context (so you don't have to remember to define it every time),
> you could write a context processor. See
> django.core.context_processors for examples.

That's probably what I'll need since it'll always have to be there.

SmileyChris

unread,
Jul 5, 2007, 5:13:11 PM7/5/07
to Django users
Or you could do {{ "class1,class2,class3,class4"|split:","|random }}

(you'd need to make the |split filter too, Django only comes with |
join)

Reply all
Reply to author
Forward
0 new messages