[albatross-users] load_template vs run_template, macros vs lookups
Andrew McNamara
andrewm at object-craft.com.au
Tue Dec 17 16:47:08 EST 2002
>>My App class does this:
>>
>> def create_context(self):
>> ctx = AppContext(self)
>> ctx.load_template('header.html')
>> return ctx
>>
>>'header.html' contains a bunch of <al-macro> definitions, and some <al-lookup>
>> definitions. So the macros and lookups should be defined before more-or-less
>>anything else happens.
>>
>>Then in the page_display, I run_template() a file that uses these macros and
>>lookups. The macros work, the lookups don't.
>>
>>If I change the create_context() to do
>> ctx.run_template('header.html')
>>then both the macros and lookups work. This is (cough) non-intuitive.
>
>Ah - okay - similar scenario: the *contents* of the lookup are only
>registered at run time (the short patch I mailed only addressed the
>registering of the lookup name, not it's contents). Something like I
>mentioned in my previous message should work (but I don't have time to
>check right now).
Hmmm - looking closer at the code, I don't think it would be valid to
instanciate the lookups without a run. The reason for this is that the
lookup key is the result of an eval - doing this eval at load time would
be breaking the "rules".
Possibly the eval could be lazy - essentially defer the eval until
lookup_html is called for the first time (as I suggested before). I've
just made the change in a sandbox, and it passes the unittests, but I'm
not sure I like the change (the diff is against the CVS tree version -
some fuzz may be required when applying to 1.01) - Dave - your thoughts?
--- tags.py 15 Dec 2002 08:32:41 -0000 1.103
+++ tags.py 17 Dec 2002 05:45:51 -0000
@@ -1046,6 +1046,7 @@
def __init__(self, ctx, filename, line_num, attribs):
EnclosingTag.__init__(self, ctx, filename, line_num, attribs)
self.assert_has_attrib('name')
+ ctx.register_lookup(self.get_attrib('name'), self)
self.item_list = []
def append(self, tag):
@@ -1055,13 +1056,14 @@
EnclosingTag.append(self, tag)
def to_html(self, ctx):
- ctx.register_lookup(self.get_attrib('name'), self)
- dict = {}
- for item in self.item_list:
- dict[item.eval_expr(ctx)] = item
- self.item_dict = dict
+ pass
def lookup_html(self, ctx, value):
+ if not hasattr(self, 'item_dict'):
+ dict = {}
+ for item in self.item_list:
+ dict[item.eval_expr(ctx)] = item
+ self.item_dict = dict
item = self.item_dict.get(value, self.content)
item.to_html(ctx)
--
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
More information about the Albatross-users
mailing list