Loading ...
Sorry, an error occurred while loading the content.

[Cheetahtemplate-discuss] macros don't follow #extends

Expand Messages
  • Buck Golemon
    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
    Message 1 of 2 , Nov 21, 2011
    • 0 Attachment
      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?

      -buck


    • Buck Golemon
      I see two options here: A) Change the #extends statement to honor #defmacro statements in the extended template     Necessary steps would be: 1. Find the
      Message 2 of 2 , Nov 22, 2011
      • 0 Attachment
        I see two options here:

        A) Change the #extends statement to honor #defmacro statements in the
        extended template

            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
        probably out.

        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.

        Necessary steps:

        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.

        -buck


        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?
        > -buck

        ------------------------------------------------------------------------------
        All the data continuously generated in your IT infrastructure
        contains a definitive record of customers, application performance,
        security threats, fraudulent activity, and more. Splunk takes this
        data and makes sense of it. IT sense. And common sense.
        http://p.sf.net/sfu/splunk-novd2d
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      Your message has been successfully submitted and would be delivered to recipients shortly.