I see two options here:
A) Change the #extends statement to honor #defmacro statements in the
Necessary steps would be:
1. Find the parent template. This is probably the hardest part,
since Cheetah generally relies on the python import mechanism to
create the connection to the parent templates. Templates (previously)
don't need to be compiled in order, so using the `imp` module is
2. Parse the parent template for #defmacro (and #extends)
statements. I have no clue how this would be done, but I think a
similar thing happens when parsing a #defmacro statement.
3. Merge the found macro definitions from the parent parser into
the current parser. This is tricky and easy to get wrong, I imagine.
B) Extend the #include statement to be able to pull in template
snippets which contain the #defmacro statements. I don't like this
since this template-reuse is generally the job of #extends, and having
More Than One Way To Do It breaks the Zen of Python.
1. Make the parser look for statements like "#include template
../macros/foo.tmpl". Implementing this is simple. Open issue: what
directory should relative paths be based upon? My preference is to use
the real-absolute directory of the current script, but I'll probably
use whatever is pre-existing in the #include code.
2. Make the parser run through the included file. I hope this
isn't hard. I need to take care to preserve line numbers.
I think #A is the Right Thing, but #B is *much* easier.
On Mon, Nov 21, 2011 at 7:17 PM, Buck Golemon <buck@...> wrote:
> If I define a macro in one template, and #extend that template elsewhere, the macro is not available in the extended template, as expected.
> When looking at the cheetah code, I see why this is: since the macros are implemented purely in the parser, and the #extended (parent, superclass) template is never parsed, the macro never exists in the context of the #extended (child, subclass) template.
> What would it take to get this working?
> What would be the best approach?