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

[Cheetahtemplate-discuss] recipe for extending/subclassing a dynamically generated template class

Expand Messages
  • Tavis Rudd
    Before yesterday it was only possible to subclass templates that had been compiled statically with the command line cheetah tool. Now you can do something
    Message 1 of 10 , Dec 13, 2005
    • 0 Attachment
      Before yesterday it was only possible to subclass templates that had been
      compiled statically with the command line 'cheetah' tool. Now you can do
      something like this:

      from Cheetah.Template import Template
      import Cheetah
      src = '''
      #extends object
      #set name = "tempate"
      This is a $name
      $meth1
      #def meth1
      this is meth1 in klass1
      #end def
      '''
      klass1 = Template.compile(src)
      print klass1
      print klass1()
      Cheetah.aDynamicallyCompiledClass = klass1

      klass2 = Template.compile("""
      #from Cheetah import aDynamicallyCompiledClass
      #extends aDynamicallyCompiledClass
      #implements meth1
      this is meth1 redefined in klass2
      """)
      print klass2
      print klass2()


      -------------------------------------------------------
      This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
      for problems? Stop! Download the new AJAX search engine that makes
      searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
      http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Shannon -jj Behrens
      Actually, it was already possible to subclass templates that had not been compiled statically in Aquarium. This was a crucial feature, so I had to hack it a
      Message 2 of 10 , Dec 15, 2005
      • 0 Attachment
        Actually, it was already possible to subclass templates that had not
        been compiled statically in Aquarium. This was a crucial feature, so
        I had to hack it a bit. Specifically, when Aquarium is loading a
        template on the fly, if it sees an "#extends" line, it'll
        automatically load the parent class. Nonetheless, I'm happy to see it
        in Cheetah.

        Happy Hacking!
        -jj

        On 12/13/05, Tavis Rudd <tavis@...> wrote:
        > Before yesterday it was only possible to subclass templates that had been
        > compiled statically with the command line 'cheetah' tool. Now you can do
        > something like this:
        >
        > from Cheetah.Template import Template
        > import Cheetah
        > src = '''
        > #extends object
        > #set name = "tempate"
        > This is a $name
        > $meth1
        > #def meth1
        > this is meth1 in klass1
        > #end def
        > '''
        > klass1 = Template.compile(src)
        > print klass1
        > print klass1()
        > Cheetah.aDynamicallyCompiledClass = klass1
        >
        > klass2 = Template.compile("""
        > #from Cheetah import aDynamicallyCompiledClass
        > #extends aDynamicallyCompiledClass
        > #implements meth1
        > this is meth1 redefined in klass2
        > """)
        > print klass2
        > print klass2()
        >
        >
        > -------------------------------------------------------
        > This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
        > for problems? Stop! Download the new AJAX search engine that makes
        > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
        > http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
        > _______________________________________________
        > Cheetahtemplate-discuss mailing list
        > Cheetahtemplate-discuss@...
        > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
        >


        --
        Law is much too important a thing to leave to politicians, lawyers,
        and celebrities.


        -------------------------------------------------------
        This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
        for problems? Stop! Download the new AJAX search engine that makes
        searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
        http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      • Tavis Rudd
        note that the hooks I added in 2.0b1 make this sort of template introspection easy for all directives. ... This SF.net email is sponsored by: Splunk Inc. Do
        Message 3 of 10 , Jan 6, 2006
        • 0 Attachment
          note that the hooks I added in 2.0b1 make this sort of template introspection
          easy for all directives.

          On Thursday 15 December 2005 6:21 pm, Shannon -jj Behrens wrote:
          > Actually, it was already possible to subclass templates that had not
          > been compiled statically in Aquarium. This was a crucial feature, so
          > I had to hack it a bit. Specifically, when Aquarium is loading a
          > template on the fly, if it sees an "#extends" line, it'll
          > automatically load the parent class. Nonetheless, I'm happy to see it
          > in Cheetah.
          >
          > Happy Hacking!
          > -jj
          >
          > On 12/13/05, Tavis Rudd <tavis@...> wrote:
          > > Before yesterday it was only possible to subclass templates that had been
          > > compiled statically with the command line 'cheetah' tool. Now you can do
          > > something like this:
          > >
          > > from Cheetah.Template import Template
          > > import Cheetah
          > > src = '''
          > > #extends object
          > > #set name = "tempate"
          > > This is a $name
          > > $meth1
          > > #def meth1
          > > this is meth1 in klass1
          > > #end def
          > > '''
          > > klass1 = Template.compile(src)
          > > print klass1
          > > print klass1()
          > > Cheetah.aDynamicallyCompiledClass = klass1
          > >
          > > klass2 = Template.compile("""
          > > #from Cheetah import aDynamicallyCompiledClass
          > > #extends aDynamicallyCompiledClass
          > > #implements meth1
          > > this is meth1 redefined in klass2
          > > """)
          > > print klass2
          > > print klass2()
          > >
          > >
          > > -------------------------------------------------------
          > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log
          > > files for problems? Stop! Download the new AJAX search engine that
          > > makes searching your log files as easy as surfing the web. DOWNLOAD
          > > SPLUNK! http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
          > > _______________________________________________
          > > Cheetahtemplate-discuss mailing list
          > > Cheetahtemplate-discuss@...
          > > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
          >
          > --
          > Law is much too important a thing to leave to politicians, lawyers,
          > and celebrities.
          >
          >
          > -------------------------------------------------------
          > This SF.net email is sponsored by: Splunk Inc. Do you grep through log
          > files for problems? Stop! Download the new AJAX search engine that makes
          > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
          > http://ads.osdn.com/?ad_idv37&alloc_id865&op=Click
          > _______________________________________________
          > Cheetahtemplate-discuss mailing list
          > Cheetahtemplate-discuss@...
          > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss


          -------------------------------------------------------
          This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
          for problems? Stop! Download the new AJAX search engine that makes
          searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
          http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
          _______________________________________________
          Cheetahtemplate-discuss mailing list
          Cheetahtemplate-discuss@...
          https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
        • Shannon -jj Behrens
          As long as you have an import hook, life is easy. In Aquarium, I had to look at the file, find the parent class, and compile and import that before I could
          Message 4 of 10 , Jan 6, 2006
          • 0 Attachment
            As long as you have an import hook, life is easy. In Aquarium, I had
            to look at the file, find the parent class, and compile and import
            that before I could compile and import the child. I specifically
            needed to avoid coding my own import hook, but I doubt everyone else
            has the same problems or weird environment that I do.

            -jj

            On 1/6/06, Tavis Rudd <tavis@...> wrote:
            > note that the hooks I added in 2.0b1 make this sort of template introspection
            > easy for all directives.
            >
            > On Thursday 15 December 2005 6:21 pm, Shannon -jj Behrens wrote:
            > > Actually, it was already possible to subclass templates that had not
            > > been compiled statically in Aquarium. This was a crucial feature, so
            > > I had to hack it a bit. Specifically, when Aquarium is loading a
            > > template on the fly, if it sees an "#extends" line, it'll
            > > automatically load the parent class. Nonetheless, I'm happy to see it
            > > in Cheetah.
            > >
            > > Happy Hacking!
            > > -jj
            > >
            > > On 12/13/05, Tavis Rudd <tavis@...> wrote:
            > > > Before yesterday it was only possible to subclass templates that had been
            > > > compiled statically with the command line 'cheetah' tool. Now you can do
            > > > something like this:
            > > >
            > > > from Cheetah.Template import Template
            > > > import Cheetah
            > > > src = '''
            > > > #extends object
            > > > #set name = "tempate"
            > > > This is a $name
            > > > $meth1
            > > > #def meth1
            > > > this is meth1 in klass1
            > > > #end def
            > > > '''
            > > > klass1 = Template.compile(src)
            > > > print klass1
            > > > print klass1()
            > > > Cheetah.aDynamicallyCompiledClass = klass1
            > > >
            > > > klass2 = Template.compile("""
            > > > #from Cheetah import aDynamicallyCompiledClass
            > > > #extends aDynamicallyCompiledClass
            > > > #implements meth1
            > > > this is meth1 redefined in klass2
            > > > """)
            > > > print klass2
            > > > print klass2()
            > > >
            > > >
            > > > -------------------------------------------------------
            > > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log
            > > > files for problems? Stop! Download the new AJAX search engine that
            > > > makes searching your log files as easy as surfing the web. DOWNLOAD
            > > > SPLUNK! http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
            > > > _______________________________________________
            > > > Cheetahtemplate-discuss mailing list
            > > > Cheetahtemplate-discuss@...
            > > > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
            > >
            > > --
            > > Law is much too important a thing to leave to politicians, lawyers,
            > > and celebrities.
            > >
            > >
            > > -------------------------------------------------------
            > > This SF.net email is sponsored by: Splunk Inc. Do you grep through log
            > > files for problems? Stop! Download the new AJAX search engine that makes
            > > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
            > > http://ads.osdn.com/?ad_idv37&alloc_id865&op=Click
            > > _______________________________________________
            > > Cheetahtemplate-discuss mailing list
            > > Cheetahtemplate-discuss@...
            > > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
            >


            --
            Law is much too important to be left to politicians, lawyers, and celebrities.


            -------------------------------------------------------
            This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
            for problems? Stop! Download the new AJAX search engine that makes
            searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
            http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
            _______________________________________________
            Cheetahtemplate-discuss mailing list
            Cheetahtemplate-discuss@...
            https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
          • Jose Galvez
            Can someone please tell my why this does not work in a template, I thought it should but it does not ================ #set base =
            Message 5 of 10 , Jun 13, 2006
            • 0 Attachment
              Can someone please tell my why this does not work in a template, I
              thought it should but it does not

              ================
              #set base = Template.compile(file='chTemplates/base.tmpl'))
              #from $base import base
              #extends base
              the rest of the template

              If I comment out the above code the template compile just fine.
              Jose

              Shannon -jj Behrens wrote:
              > As long as you have an import hook, life is easy. In Aquarium, I had
              > to look at the file, find the parent class, and compile and import
              > that before I could compile and import the child. I specifically
              > needed to avoid coding my own import hook, but I doubt everyone else
              > has the same problems or weird environment that I do.
              >
              > -jj
              >
              > On 1/6/06, Tavis Rudd <tavis@...> wrote:
              >
              >> note that the hooks I added in 2.0b1 make this sort of template introspection
              >> easy for all directives.
              >>
              >> On Thursday 15 December 2005 6:21 pm, Shannon -jj Behrens wrote:
              >>
              >>> Actually, it was already possible to subclass templates that had not
              >>> been compiled statically in Aquarium. This was a crucial feature, so
              >>> I had to hack it a bit. Specifically, when Aquarium is loading a
              >>> template on the fly, if it sees an "#extends" line, it'll
              >>> automatically load the parent class. Nonetheless, I'm happy to see it
              >>> in Cheetah.
              >>>
              >>> Happy Hacking!
              >>> -jj
              >>>
              >>> On 12/13/05, Tavis Rudd <tavis@...> wrote:
              >>>
              >>>> Before yesterday it was only possible to subclass templates that had been
              >>>> compiled statically with the command line 'cheetah' tool. Now you can do
              >>>> something like this:
              >>>>
              >>>> from Cheetah.Template import Template
              >>>> import Cheetah
              >>>> src = '''
              >>>> #extends object
              >>>> #set name = "tempate"
              >>>> This is a $name
              >>>> $meth1
              >>>> #def meth1
              >>>> this is meth1 in klass1
              >>>> #end def
              >>>> '''
              >>>> klass1 = Template.compile(src)
              >>>> print klass1
              >>>> print klass1()
              >>>> Cheetah.aDynamicallyCompiledClass = klass1
              >>>>
              >>>> klass2 = Template.compile("""
              >>>> #from Cheetah import aDynamicallyCompiledClass
              >>>> #extends aDynamicallyCompiledClass
              >>>> #implements meth1
              >>>> this is meth1 redefined in klass2
              >>>> """)
              >>>> print klass2
              >>>> print klass2()
              >>>>
              >>>>
              >>>> -------------------------------------------------------
              >>>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
              >>>> files for problems? Stop! Download the new AJAX search engine that
              >>>> makes searching your log files as easy as surfing the web. DOWNLOAD
              >>>> SPLUNK! http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
              >>>> _______________________________________________
              >>>> Cheetahtemplate-discuss mailing list
              >>>> Cheetahtemplate-discuss@...
              >>>> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
              >>>>
              >>> --
              >>> Law is much too important a thing to leave to politicians, lawyers,
              >>> and celebrities.
              >>>
              >>>
              >>> -------------------------------------------------------
              >>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
              >>> files for problems? Stop! Download the new AJAX search engine that makes
              >>> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
              >>> http://ads.osdn.com/?ad_idv37&alloc_id865&op=Click
              >>> _______________________________________________
              >>> Cheetahtemplate-discuss mailing list
              >>> Cheetahtemplate-discuss@...
              >>> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
              >>>
              >
              >
              > --
              > Law is much too important to be left to politicians, lawyers, and celebrities.
              >
              >
              > -------------------------------------------------------
              > This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
              > for problems? Stop! Download the new AJAX search engine that makes
              > searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
              > http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
              > _______________________________________________
              > Cheetahtemplate-discuss mailing list
              > Cheetahtemplate-discuss@...
              > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
              >
              >
              >



              _______________________________________________
              Cheetahtemplate-discuss mailing list
              Cheetahtemplate-discuss@...
              https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
            • Jose Galvez
              messing around with the code and paying more attention to what Shannon originally wrote I found out that this code works: #set base =
              Message 6 of 10 , Jun 13, 2006
              • 0 Attachment
                messing around with the code and paying more attention to what Shannon
                originally wrote I found out that this code works:
                #set base = Template.compile(file='chTemplates/base.tmpl'))
                #import Cheetah
                #set $Cheetah.mybase = $base
                #from Cheetah import mybase
                #extends mybase
                the rest of the template.

                Is this the correct syntax or is there a better way to do it?
                Jose

                Jose Galvez wrote:
                > Can someone please tell my why this does not work in a template, I
                > thought it should but it does not
                >
                > ================
                > #set base = Template.compile(file='chTemplates/base.tmpl'))
                > #from $base import base
                > #extends base
                > the rest of the template
                >
                > If I comment out the above code the template compile just fine.
                > Jose
                >
                > Shannon -jj Behrens wrote:
                >
                >> As long as you have an import hook, life is easy. In Aquarium, I had
                >> to look at the file, find the parent class, and compile and import
                >> that before I could compile and import the child. I specifically
                >> needed to avoid coding my own import hook, but I doubt everyone else
                >> has the same problems or weird environment that I do.
                >>
                >> -jj
                >>
                >> On 1/6/06, Tavis Rudd <tavis@...> wrote:
                >>
                >>
                >>> note that the hooks I added in 2.0b1 make this sort of template introspection
                >>> easy for all directives.
                >>>
                >>> On Thursday 15 December 2005 6:21 pm, Shannon -jj Behrens wrote:
                >>>
                >>>
                >>>> Actually, it was already possible to subclass templates that had not
                >>>> been compiled statically in Aquarium. This was a crucial feature, so
                >>>> I had to hack it a bit. Specifically, when Aquarium is loading a
                >>>> template on the fly, if it sees an "#extends" line, it'll
                >>>> automatically load the parent class. Nonetheless, I'm happy to see it
                >>>> in Cheetah.
                >>>>
                >>>> Happy Hacking!
                >>>> -jj
                >>>>
                >>>> On 12/13/05, Tavis Rudd <tavis@...> wrote:
                >>>>
                >>>>
                >>>>> Before yesterday it was only possible to subclass templates that had been
                >>>>> compiled statically with the command line 'cheetah' tool. Now you can do
                >>>>> something like this:
                >>>>>
                >>>>> from Cheetah.Template import Template
                >>>>> import Cheetah
                >>>>> src = '''
                >>>>> #extends object
                >>>>> #set name = "tempate"
                >>>>> This is a $name
                >>>>> $meth1
                >>>>> #def meth1
                >>>>> this is meth1 in klass1
                >>>>> #end def
                >>>>> '''
                >>>>> klass1 = Template.compile(src)
                >>>>> print klass1
                >>>>> print klass1()
                >>>>> Cheetah.aDynamicallyCompiledClass = klass1
                >>>>>
                >>>>> klass2 = Template.compile("""
                >>>>> #from Cheetah import aDynamicallyCompiledClass
                >>>>> #extends aDynamicallyCompiledClass
                >>>>> #implements meth1
                >>>>> this is meth1 redefined in klass2
                >>>>> """)
                >>>>> print klass2
                >>>>> print klass2()
                >>>>>
                >>>>>
                >>>>> -------------------------------------------------------
                >>>>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
                >>>>> files for problems? Stop! Download the new AJAX search engine that
                >>>>> makes searching your log files as easy as surfing the web. DOWNLOAD
                >>>>> SPLUNK! http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
                >>>>> _______________________________________________
                >>>>> Cheetahtemplate-discuss mailing list
                >>>>> Cheetahtemplate-discuss@...
                >>>>> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                >>>>>
                >>>>>
                >>>> --
                >>>> Law is much too important a thing to leave to politicians, lawyers,
                >>>> and celebrities.
                >>>>
                >>>>
                >>>> -------------------------------------------------------
                >>>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
                >>>> files for problems? Stop! Download the new AJAX search engine that makes
                >>>> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
                >>>> http://ads.osdn.com/?ad_idv37&alloc_id865&op=Click
                >>>> _______________________________________________
                >>>> Cheetahtemplate-discuss mailing list
                >>>> Cheetahtemplate-discuss@...
                >>>> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                >>>>
                >>>>
                >> --
                >> Law is much too important to be left to politicians, lawyers, and celebrities.
                >>
                >>
                >> -------------------------------------------------------
                >> This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
                >> for problems? Stop! Download the new AJAX search engine that makes
                >> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
                >> http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
                >> _______________________________________________
                >> Cheetahtemplate-discuss mailing list
                >> Cheetahtemplate-discuss@...
                >> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                >>
                >>
                >>
                >>
                >
                >
                >
                > _______________________________________________
                > Cheetahtemplate-discuss mailing list
                > Cheetahtemplate-discuss@...
                > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                >
                >
                >



                _______________________________________________
                Cheetahtemplate-discuss mailing list
                Cheetahtemplate-discuss@...
                https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
              • Mike Orr
                ... #set is a runtime directive. Remember that when you re filling a template, you re actually calling an instance method. The above is exactly like putting:
                Message 7 of 10 , Jun 14, 2006
                • 0 Attachment
                  On 6/13/06, Jose Galvez <jose@...> wrote:
                  > Can someone please tell my why this does not work in a template, I
                  > thought it should but it does not
                  >
                  > ================
                  > #set base = Template.compile(file='chTemplates/base.tmpl'))
                  > #from $base import base
                  > #extends base
                  > the rest of the template
                  >
                  > If I comment out the above code the template compile just fine.
                  > Jose

                  #set is a runtime directive. Remember that when you're filling a
                  template, you're actually calling an instance method. The above is
                  exactly like putting:

                  base = Template.compile(file='chTemplates/base.tmpl')

                  inside your own Python method; it sets a local variable.

                  The #from puts a "from ... import" at the top of the module. 'base'
                  is not defined at that point. There are two other errors here:

                  1) In Python, the word after 'from' must be the name of a module that
                  will be found in the filesystem (or has been previously registered in
                  sys.modules), not an expression referring to a module object.

                  2) Because Cheetah compiles '$base' to 'base' if it knows it's a
                  local/global/builtin rather than a searchList item, you can get away
                  with some unorthodox usages, like putting a variable name where Python
                  requires a literal, or using #set or #silent to create a 'del'
                  statement. But this depends on a quirk in Cheetah that happens to
                  exploit a characteristic of Python's syntax (variable names in Python
                  don't have $prefixes @like %Perl). It's not a feature that should be
                  depended on. Use $placeholders only where Python would allow an
                  expression, or as traditional $placeholders in the text.

                  To continue the analysis, #extends is a signal to the compiler to
                  change the template's base class. That's equivalent to:

                  class MyTemplate(base):

                  Of course, 'base' is not defined at this point either -- it's defined
                  inside a method of that class.

                  --
                  Mike Orr <sluggoster@...>


                  _______________________________________________
                  Cheetahtemplate-discuss mailing list
                  Cheetahtemplate-discuss@...
                  https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                • Shannon -jj Behrens
                  ... Some other things you might want to think about (bare in mind, it s pretty early in the morning for me): 1. You could move all the magic stuff to a fixed
                  Message 8 of 10 , Jun 14, 2006
                  • 0 Attachment
                    On 6/14/06, Mike Orr <sluggoster@...> wrote:
                    > On 6/13/06, Jose Galvez <jose@...> wrote:
                    > > Can someone please tell my why this does not work in a template, I
                    > > thought it should but it does not
                    > >
                    > > ================
                    > > #set base = Template.compile(file='chTemplates/base.tmpl'))
                    > > #from $base import base
                    > > #extends base
                    > > the rest of the template
                    > >
                    > > If I comment out the above code the template compile just fine.
                    > > Jose
                    >
                    > #set is a runtime directive. Remember that when you're filling a
                    > template, you're actually calling an instance method. The above is
                    > exactly like putting:
                    >
                    > base = Template.compile(file='chTemplates/base.tmpl')
                    >
                    > inside your own Python method; it sets a local variable.
                    >
                    > The #from puts a "from ... import" at the top of the module. 'base'
                    > is not defined at that point. There are two other errors here:
                    >
                    > 1) In Python, the word after 'from' must be the name of a module that
                    > will be found in the filesystem (or has been previously registered in
                    > sys.modules), not an expression referring to a module object.
                    >
                    > 2) Because Cheetah compiles '$base' to 'base' if it knows it's a
                    > local/global/builtin rather than a searchList item, you can get away
                    > with some unorthodox usages, like putting a variable name where Python
                    > requires a literal, or using #set or #silent to create a 'del'
                    > statement. But this depends on a quirk in Cheetah that happens to
                    > exploit a characteristic of Python's syntax (variable names in Python
                    > don't have $prefixes @like %Perl). It's not a feature that should be
                    > depended on. Use $placeholders only where Python would allow an
                    > expression, or as traditional $placeholders in the text.
                    >
                    > To continue the analysis, #extends is a signal to the compiler to
                    > change the template's base class. That's equivalent to:
                    >
                    > class MyTemplate(base):
                    >
                    > Of course, 'base' is not defined at this point either -- it's defined
                    > inside a method of that class.

                    Some other things you might want to think about (bare in mind, it's
                    pretty early in the morning for me):

                    1. You could move all the "magic" stuff to a fixed Python module in
                    order to get around Cheetah.

                    2. Use the "type" function to create a new class on the fly that is
                    the subclass of some list of classes.

                    If you're just trying to make sure parent class templates get compiled
                    on the fly, there are easier ways:

                    1. Use the import hook.

                    2. Steal my code from Aquarium.

                    Best Regards,
                    -jj


                    _______________________________________________
                    Cheetahtemplate-discuss mailing list
                    Cheetahtemplate-discuss@...
                    https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                  • Tavis Rudd
                    Hi Jose, have a look at Template.compile s baseclass argument, or the Template.subclass method. Cheers, Tavis ...
                    Message 9 of 10 , Jun 14, 2006
                    • 0 Attachment
                      Hi Jose,
                      have a look at Template.compile's baseclass argument, or the
                      Template.subclass method. Cheers,
                      Tavis


                      On Tue, 13 Jun 2006, Jose Galvez wrote:

                      > Can someone please tell my why this does not work in a template, I
                      > thought it should but it does not
                      >
                      > ================
                      > #set base = Template.compile(file='chTemplates/base.tmpl'))
                      > #from $base import base
                      > #extends base
                      > the rest of the template
                      >
                      > If I comment out the above code the template compile just fine.
                      > Jose
                      >
                      > Shannon -jj Behrens wrote:
                      >> As long as you have an import hook, life is easy. In Aquarium, I had
                      >> to look at the file, find the parent class, and compile and import
                      >> that before I could compile and import the child. I specifically
                      >> needed to avoid coding my own import hook, but I doubt everyone else
                      >> has the same problems or weird environment that I do.
                      >>
                      >> -jj
                      >>
                      >> On 1/6/06, Tavis Rudd <tavis@...> wrote:
                      >>
                      >>> note that the hooks I added in 2.0b1 make this sort of template introspection
                      >>> easy for all directives.
                      >>>
                      >>> On Thursday 15 December 2005 6:21 pm, Shannon -jj Behrens wrote:
                      >>>
                      >>>> Actually, it was already possible to subclass templates that had not
                      >>>> been compiled statically in Aquarium. This was a crucial feature, so
                      >>>> I had to hack it a bit. Specifically, when Aquarium is loading a
                      >>>> template on the fly, if it sees an "#extends" line, it'll
                      >>>> automatically load the parent class. Nonetheless, I'm happy to see it
                      >>>> in Cheetah.
                      >>>>
                      >>>> Happy Hacking!
                      >>>> -jj
                      >>>>
                      >>>> On 12/13/05, Tavis Rudd <tavis@...> wrote:
                      >>>>
                      >>>>> Before yesterday it was only possible to subclass templates that had been
                      >>>>> compiled statically with the command line 'cheetah' tool. Now you can do
                      >>>>> something like this:
                      >>>>>
                      >>>>> from Cheetah.Template import Template
                      >>>>> import Cheetah
                      >>>>> src = '''
                      >>>>> #extends object
                      >>>>> #set name = "tempate"
                      >>>>> This is a $name
                      >>>>> $meth1
                      >>>>> #def meth1
                      >>>>> this is meth1 in klass1
                      >>>>> #end def
                      >>>>> '''
                      >>>>> klass1 = Template.compile(src)
                      >>>>> print klass1
                      >>>>> print klass1()
                      >>>>> Cheetah.aDynamicallyCompiledClass = klass1
                      >>>>>
                      >>>>> klass2 = Template.compile("""
                      >>>>> #from Cheetah import aDynamicallyCompiledClass
                      >>>>> #extends aDynamicallyCompiledClass
                      >>>>> #implements meth1
                      >>>>> this is meth1 redefined in klass2
                      >>>>> """)
                      >>>>> print klass2
                      >>>>> print klass2()
                      >>>>>
                      >>>>>
                      >>>>> -------------------------------------------------------
                      >>>>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
                      >>>>> files for problems? Stop! Download the new AJAX search engine that
                      >>>>> makes searching your log files as easy as surfing the web. DOWNLOAD
                      >>>>> SPLUNK! http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
                      >>>>> _______________________________________________
                      >>>>> Cheetahtemplate-discuss mailing list
                      >>>>> Cheetahtemplate-discuss@...
                      >>>>> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                      >>>>>
                      >>>> --
                      >>>> Law is much too important a thing to leave to politicians, lawyers,
                      >>>> and celebrities.
                      >>>>
                      >>>>
                      >>>> -------------------------------------------------------
                      >>>> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
                      >>>> files for problems? Stop! Download the new AJAX search engine that makes
                      >>>> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
                      >>>> http://ads.osdn.com/?ad_idv37&alloc_id865&op=Click
                      >>>> _______________________________________________
                      >>>> Cheetahtemplate-discuss mailing list
                      >>>> Cheetahtemplate-discuss@...
                      >>>> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                      >>>>
                      >>
                      >>
                      >> --
                      >> Law is much too important to be left to politicians, lawyers, and celebrities.
                      >>
                      >>
                      >> -------------------------------------------------------
                      >> This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
                      >> for problems? Stop! Download the new AJAX search engine that makes
                      >> searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
                      >> http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
                      >> _______________________________________________
                      >> Cheetahtemplate-discuss mailing list
                      >> Cheetahtemplate-discuss@...
                      >> https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                      >>
                      >>
                      >>
                      >
                      >
                      >
                      > _______________________________________________
                      > Cheetahtemplate-discuss mailing list
                      > Cheetahtemplate-discuss@...
                      > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                      >


                      _______________________________________________
                      Cheetahtemplate-discuss mailing list
                      Cheetahtemplate-discuss@...
                      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                    • jose@cybergalvez.com
                      thanks Mike, now the errors I was getting make more sense. The couple of times I did get the below code to work was because I had at some point already
                      Message 10 of 10 , Jun 14, 2006
                      • 0 Attachment
                        thanks Mike, now the errors I was getting make more sense. The couple
                        of times I did get the below code to work was because I had at some
                        point already defined base elsewhere. At this point I might just stick
                        with statically compiling my parent templates as that seems to be the
                        most stable thing to do, and there is really no reason not to do that
                        Jose


                        > -------- Original Message --------
                        > Subject: Re: [Cheetahtemplate-discuss] recipe for extending/subclassing
                        > a dynamically generated template class
                        > From: "Mike Orr" <sluggoster@...>
                        > Date: Wed, June 14, 2006 12:20 am
                        > To: "Jose Galvez" <jose@...>
                        > Cc: cheetahtemplate-discuss@...
                        >
                        > On 6/13/06, Jose Galvez <jose@...> wrote:
                        > > Can someone please tell my why this does not work in a template, I
                        > > thought it should but it does not
                        > >
                        > > ================
                        > > #set base = Template.compile(file='chTemplates/base.tmpl'))
                        > > #from $base import base
                        > > #extends base
                        > > the rest of the template
                        > >
                        > > If I comment out the above code the template compile just fine.
                        > > Jose
                        >
                        > #set is a runtime directive. Remember that when you're filling a
                        > template, you're actually calling an instance method. The above is
                        > exactly like putting:
                        >
                        > base = Template.compile(file='chTemplates/base.tmpl')
                        >
                        > inside your own Python method; it sets a local variable.
                        >
                        > The #from puts a "from ... import" at the top of the module. 'base'
                        > is not defined at that point. There are two other errors here:
                        >
                        > 1) In Python, the word after 'from' must be the name of a module that
                        > will be found in the filesystem (or has been previously registered in
                        > sys.modules), not an expression referring to a module object.
                        >
                        > 2) Because Cheetah compiles '$base' to 'base' if it knows it's a
                        > local/global/builtin rather than a searchList item, you can get away
                        > with some unorthodox usages, like putting a variable name where Python
                        > requires a literal, or using #set or #silent to create a 'del'
                        > statement. But this depends on a quirk in Cheetah that happens to
                        > exploit a characteristic of Python's syntax (variable names in Python
                        > don't have $prefixes @like %Perl). It's not a feature that should be
                        > depended on. Use $placeholders only where Python would allow an
                        > expression, or as traditional $placeholders in the text.
                        >
                        > To continue the analysis, #extends is a signal to the compiler to
                        > change the template's base class. That's equivalent to:
                        >
                        > class MyTemplate(base):
                        >
                        > Of course, 'base' is not defined at this point either -- it's defined
                        > inside a method of that class.
                        >
                        > --
                        > Mike Orr <sluggoster@...>
                        >
                        >
                        > _______________________________________________
                        > Cheetahtemplate-discuss mailing list
                        > Cheetahtemplate-discuss@...
                        > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss



                        _______________________________________________
                        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.