[albatross-users] impedance mismatch 1
Matt Goodall
matt at pollenation.net
Thu Jul 3 03:26:10 EST 2003
That's not really what I meant, I was thinking more about how ctx.locals
is accessed in the Python code. It probably wasn't as clear as it could
have been ... it was a long post and it was very late ;-).
What I was actually suggesting was *adding* a dictionary-like interface
to ctx.locals (a Vars instance) to allow something like this:
def page_display(ctx):
ctx.locals.first_name = 'Matt'
... or ...
ctx.locals['first_name'] = 'Matt'
ctx.run_template('template.html')
It wouldn't change anything in the template, it would still be
"<al-value expr="first_name" />".
From what I've seen on the list people sometimes expect ctx.locals to
be a dictionary and get confused when it turns out that it's just an
object that stores attributes. I think that was the concept that was
holding Eric back from true enlightenment.
To be honest, I'm not sure it's a good idea anyway. For a start, it's
more typing to use ctx.locals like a dictionary (all those quotes and
square brackets ... yeuch!) but more importantly, if people start mixing
styles it could make examples harder to understand. To support the
dictionary interface completely would also clutter ctx.locals with
unnecessary attributes such as "keys" which would break at least one of
the examples in the documentation.
It seems best just to document what ctx.locals is and why
"ctx.local.first_name = 'Matt'" is allowed.
Cheers, Matt
Michael C. Neel wrote:
>>This reminds me that I have a note in a text file somewhere that says
>>something like, "could/should ctx.locals behave like a dictionary?".
>>
>>
>
>There is no need for this, you can just use the locals() method of
>python (don't confuse ctx.locals with locals())
>
>so, if you need a ctx.locals dict, do:
>
><al-value expr="locals()['__page__']">
>
>locals() only will grab what is in the ctx.locals namespace. If you
>want to see globals as a dict, use the python globals() method.
>
>so:
>
>import os
>
>class myform:
> def page_display(self, ctx):
> ctx.locals.myvar = "two"
> ctx.run_template('index.html')
>
>----------------
><al-value expr="locals()"> <!-- will have myvar as a key, but not os
>-->
><al-value expr="globals()"> <!-- will have os as a key, but not myvar
>-->
>
>With the .has_key() method of dicts, it's easy to have templates that
>can deal with not having a value passed to them they may want to use.
>This is really useful in making the same template handle both add and
>edit a record.
>
>Mike
>
>
--
Matt Goodall, Pollenation Internet Ltd
e: matt at pollenationinternet.com
More information about the Albatross-users
mailing list