On May 3, 1:15 pm, Julián C. Pérez <jcp...@gmail.com> wrote:
>
> example:
> {% createLink "a link to something" src "class attr" "id attr" "title
> attri" %}
> would output something like:
> <a id="id attr" class="class attr" title="title attr" href="src">a
> link to something</a>
>
> how can i make the 'src' input to be the result of url tag calling a
> valid view??

I recommend the captureas snippet below:

http://www.djangosnippets.org/snippets/545/

You will see in the author's writeup that they are solving essentially
the same problem as you are trying to solve.

(I found it by googling "django snippet assign variable.")

> i mean, something like:
> {% createLink "a link to something" "{% url aValidView %}" "class
> attr" "id attr" "title attri" %} # obviously that is wrong... just to
> make my point
> <a id="id attr" class="class attr" title="title attr" /a/valid/view/
> url/="src">a link to something</a>
>

As others have mentioned in this thread, I don't recommend actually
trying to call the url tag that far "inline," i.e. in another tag call
(createLink).

Hopefully the captureas tag gets you a lot closer to where you want to
be.  Do something like this:

{% captureas valid_view_url %}}{% url aValidView %} {% endcaptureas %}
{% createLink "a link to something" valid_view_url "class attr" "id
attr" "title attri" %}

I would stop reading this now. :)

But I can sympathize with the gist of your dilemma...sometimes you
want to inject a little power into the template language, live on the
edge a bit.  The code below might provide some inspiration for you,
although it doesn't really solve your problem, just suggests some
techniques at your disposal.

'''
  A node that gets re-rendered as needed.
'''
class RenderNode(Node):
    def __init__(self, var):
        self.var = var

    def render(self, context):
        val = resolve_variable(self.var, context)
        try:
            return val.render(context)
        except:
            return val

@register.tag
def render(parser, token):
    """
    if a variable resolves to a template object, expands the object...
    allows you to do something like this
    context['name'] = 'Steve'
    context['greeting'] = template.Template('Hi {{ name }}, nice to
meet you!')
    template.html:
        {{ render greeting}}
    """
    bits = token.split_contents()
    tag = bits.pop(0)
    try:
        var = bits.pop(0)
    except IndexError:
        raise TemplateSyntaxError, "%r tag requires at least one
param" % tag
    return RenderNode(var)

--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to