Uh!! (Hits hand into forehead.)  Yep, just including a template named
by a variable would have been much simpler.

However, it's all worked out nicely.  Having seen just how easy it is
to write a custom template tag, I replaced the 'eval' tag with a
couple specialized tags, which render from another template (like an
inclusion tag, but with some processing).  This enabled something like
local variables for the "subroutine" template: if you say {% mytag
userGadget %}, the Python code for the tag copies the value of
"userGadget" into "gadget" in the Context that the mytag template
sees.  And that template still gets to see all the variables in the
top-level template's Context.

I pulled the same trick on three messy things, and now our templates
are ridiculously simple.

My hat is off to the designers of Django's templating system!
Beautiful, simple, efficient.

Ben
http://decisionero.com


On Jul 5, 7:34 am, Ned Batchelder <[EMAIL PROTECTED]> wrote:
> Now that you have an eval tag, maybe you don't need this, but I think
> Django already had a simpler solution to your problem:
>
> Context:
>     buttonDecidedAtRunTime: 'button4.html'
>     thisPage: "/some/path"
>
> blah.html:
>     <p>{% include buttonDecidedAtRunTime %}</p>
>
> The argument to the include tag doesn't need to be a literal string, it
> can be a value from the context.  This has the added advantage that the
> common "{% include " in each of your button choices is factored out and
> appears only once, in blah.html.
>
> --Ned.http://nedbatchelder.com
>
> Ben Kovitz wrote:
> > Thanks for the encouragement, Alex.  This was so easy, it should be a
> > first lesson in how to write a custom tag.  It took about 15 minutes
> > and worked the first time!
>
> > from django import template
>
> > register = template.Library()
>
> > @register.tag(name="eval")
> > def do_eval(parser, token):
> >    try:
> >       tagName, variableName = token.split_contents()
> >    except ValueError:
> >       raise template.TemplateSyntaxError("%r requires a single
> > argument: the name of the variable to evaluate as the body of a
> > template" % tagName)
>
> >    return EvalNode(variableName)
>
> > class EvalNode(template.Node):
>
> >    def __init__(self, variableName):
> >       self.variableName = variableName
>
> >    def render(self, context):
> >       t = template.Template(context[self.variableName])
> >       return t.render(context)
>
> > On Jul 4, 5:51 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> > wrote:
>
> >> I don't think it would be difficult to implement, either as a block
> >> tag, or as a regular tag with context.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to