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

[Cheetahtemplate-discuss] When using ``#include'', output not as expected, Is this a bug?

Expand Messages
  • Suriya Narayanan M S
    I have a small example which is not working as I think it should. from Cheetah.Template import Template templateDef = #for $i in $list #include
    Message 1 of 8 , Jun 14, 2004
    • 0 Attachment
      I have a small example which is not working as I think it should.

      <code>
      from Cheetah.Template import Template

      templateDef = """
      #for $i in $list
      #include source=$i
      #end for
      """

      class T1(Template):
      list = [ '$one', '$two', '$three' ]
      one = 1
      two = 2
      three = 3

      t = T1(templateDef)
      print t
      </code>

      The output I am getting is 111, but I expect 123. Am I missing something
      here?

      I am not in the mailing list yet. So please Cc a reply to me.

      Thanks,
      Suriya


      -------------------------------------------------------
      This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
      Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
      Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
      REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Suriya Narayanan M S
      ... I am using Debian unstable with Python 2.3.4 and Cheetah 0.9.15. ... That is not the problem. I guess it is because of the #for loop. #for $i in
      Message 2 of 8 , Jun 14, 2004
      • 0 Attachment
        On Mon, Jun 14, 2004 at 08:11:23PM -0700, Mike Orr wrote:
        > >
        > > <code>
        > > from Cheetah.Template import Template
        > >
        > > templateDef = """
        > > #for $i in $list
        > > #include source=$i
        > > #end for
        > > """
        > >
        > > class T1(Template):
        > > list = [ '$one', '$two', '$three' ]
        > > one = 1
        > > two = 2
        > > three = 3
        > >
        > > t = T1(templateDef)
        > > print t
        > > </code>
        > >
        > > The output I am getting is 111, but I expect 123. Am I missing something
        > > here?
        > >
        > > I am not in the mailing list yet. So please Cc a reply to me.
        >
        > I'm getting 123 with Python 2.3.4 and a CVS version of Cheetah. Which
        > versions are you using?

        I am using Debian unstable with Python 2.3.4 and Cheetah 0.9.15.

        >
        > The name 'list' shadows a Python builtin. This is probably not the
        > problem, but try renaming the variable and see if the result changes.

        That is not the problem. I guess it is because of the #for loop.

        <code>
        #for $i in $list
        #include source=$i
        #end for
        </code>

        does not work.

        But (manual unrolling)

        <code>
        #include source=$list[0]
        #include source=$list[1]
        #include source=$list[2]
        </code>

        works.


        >
        > Is there a reason you need to use #include this way? #include is one
        > of those obscure features that exists for the few cases where it's
        > indespensible, but it's generally better to use methods or subtemplates
        > instead. (subtemplate = a placeholder whose value is an instance of
        > another template) If you describe the problem, we can try to come up
        > with a solution that doesn't use #include.
        >

        This is the problem. I have a list of URLs and want to print them. They
        all have a common root. This is what I had.
        rootURL = 'http://something'
        url_list = [
        '$rootURL/a',
        'http://somethingelse/b',
        '$rootURL/b',
        ]
        And I wrote the code
        #for $i in $url_list
        #include source=$i
        #end for
        The output is
        http://something/a
        http://something/a
        http://something/a
        Only the first instance of the loop is #included. (Similar to the 123
        example).

        I thought of using filters or something else. Could you tell what in
        your opinion is an elegant way to do it.

        Thanks,
        Suriya


        -------------------------------------------------------
        This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
        Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
        Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
        REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      • Terrel Shumway
        ... Right. What s a subtemplate? The word doesn t occur in the users guide, and it appears only once in the developer s guide (in the history section where
        Message 3 of 8 , Jun 15, 2004
        • 0 Attachment
          >>#include is one
          >>of those obscure features that exists for the few cases where it's
          >>indespensible, but it's generally better to use methods or subtemplates
          >>instead. (subtemplate = a placeholder whose value is an instance of
          >>another template) If you describe the problem, we can try to come up
          >>with a solution that doesn't use #include.
          >>
          >>

          "Right."

          "What's a subtemplate?"

          The word doesn't occur in the users guide, and
          it appears only once in the developer's guide
          (in the history section where it seems to refer
          to something else).

          I have been using #include quite extensively in
          a very cumbersome way.

          ======== INCLUDE/INDEX.tmpl.htm =============
          #from webtools.myapp import TemplateBase
          #extends TemplateBase
          <html>
          <head>
          <title>$PAGE_TITLE</title>
          $STYLE
          </head>
          <body>
          .....
          #include $find_template_file("CONTENT")
          ....
          </html>
          ==============================================

          ========== ./CONTENT.tmpl.htm ================
          <h1>$PAGE_TITLE</h1>
          <p>sign up for free information</p>
          $OPTIN
          ===============================================

          =========== ./other-stuff/CONTENT.tmpl.htm ====
          <h1>$PAGE_TITLE</h1>
          <p>Here is some alternate copy to help people
          want to sign up.</p>
          $OPTIN
          ================================================

          webtools.myapp.TemplateBase has a method
          find_template_file(basename) that looks in
          list of directories to find a file "basename.tmpl.htm"
          and returns the full path. so if I am in ./other-stuff/
          the CONTENT.tmpl.htm in that directory overrides the
          that file in ./ but inherits OPTIN and other stuff
          from above. ($OPTIN gets populated from the contents of
          ./OPTIN.htm using the same path search, but it is
          strictly text.)

          This all "works", but when I have to change OPTIN.htm
          to contain directives and placeholders, I need to
          1) rename it to OPTIN.tmpl.htm
          2) change all occurrences of $OPTIN to
          #include $find_template_file("OPTIN")

          YUK!

          When I was building the system, I originally wanted to
          be able to include subtemplates transparently, but I
          could never get it work without the include hack.

          Will "subtemplates" work for me?

          -- Terrel


          p.s. Complete code and a website included in bundle at:
          http://jdiworks.net/download/sitebuilder.tar.bz2

          mkdir ~/work
          cd ~/work
          tar xjf ....
          cd website/source
          ./makeall

          (requires Cheetah, yaml and python2.3)


          -------------------------------------------------------
          This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
          Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
          Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
          REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
          _______________________________________________
          Cheetahtemplate-discuss mailing list
          Cheetahtemplate-discuss@...
          https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
        • Aaron Held
          Why not subclass the template (did I miss something?) parent.tmpl ..... #block content THIS PAGE HAS NO CONTENT #end block .... Child: #from
          Message 4 of 8 , Jun 16, 2004
          • 0 Attachment
            Why not subclass the template (did I miss something?)

            parent.tmpl
            <html>
            .....
            #block content
            THIS PAGE HAS NO CONTENT
            #end block
            ....
            </html>

            Child:

            #from parent import parent
            #extends parent
            #def content
            This is the real content
            #end def

            This is the main feature that drew me to cheetah. Of course you have to precompile
            the parent templates first, and reload everything if you change any parent tempate.

            That became so much trouble I adopted this pattern:

            parent.tmpl
            <html>
            ...
            $content
            </html>

            servlet.py
            pageData['Title'] = 'Page'
            pageDate['User'] = username

            writeHTML:
            write template(parent.tmpl, search=pageData)

            content:
            write template(content.tmpl, search=pageData)


            its less elegant but changes to templates reflected right away.

            -Aaron








            Terrel Shumway wrote:

            >
            >>> #include is one
            >>> of those obscure features that exists for the few cases where it's
            >>> indespensible, but it's generally better to use methods or subtemplates
            >>> instead. (subtemplate = a placeholder whose value is an instance of
            >>> another template) If you describe the problem, we can try to come up
            >>> with a solution that doesn't use #include.
            >>>
            >>
            >
            > "Right."
            >
            > "What's a subtemplate?"
            >
            > The word doesn't occur in the users guide, and
            > it appears only once in the developer's guide
            > (in the history section where it seems to refer
            > to something else).
            >
            > I have been using #include quite extensively in
            > a very cumbersome way.
            >
            > ======== INCLUDE/INDEX.tmpl.htm =============
            > #from webtools.myapp import TemplateBase
            > #extends TemplateBase
            > <html>
            > <head>
            > <title>$PAGE_TITLE</title>
            > $STYLE
            > </head>
            > <body>
            > .....
            > #include $find_template_file("CONTENT")
            > ....
            > </html>
            > ==============================================
            >
            > ========== ./CONTENT.tmpl.htm ================
            > <h1>$PAGE_TITLE</h1>
            > <p>sign up for free information</p>
            > $OPTIN
            > ===============================================
            >
            > =========== ./other-stuff/CONTENT.tmpl.htm ====
            > <h1>$PAGE_TITLE</h1>
            > <p>Here is some alternate copy to help people
            > want to sign up.</p>
            > $OPTIN
            > ================================================
            >
            > webtools.myapp.TemplateBase has a method
            > find_template_file(basename) that looks in
            > list of directories to find a file "basename.tmpl.htm"
            > and returns the full path. so if I am in ./other-stuff/
            > the CONTENT.tmpl.htm in that directory overrides the
            > that file in ./ but inherits OPTIN and other stuff
            > from above. ($OPTIN gets populated from the contents of
            > ./OPTIN.htm using the same path search, but it is
            > strictly text.)
            >
            > This all "works", but when I have to change OPTIN.htm
            > to contain directives and placeholders, I need to
            > 1) rename it to OPTIN.tmpl.htm
            > 2) change all occurrences of $OPTIN to
            > #include $find_template_file("OPTIN")
            >
            > YUK!
            >
            > When I was building the system, I originally wanted to
            > be able to include subtemplates transparently, but I
            > could never get it work without the include hack.
            >
            > Will "subtemplates" work for me?
            >
            > -- Terrel
            >
            >
            > p.s. Complete code and a website included in bundle at:
            > http://jdiworks.net/download/sitebuilder.tar.bz2
            >
            > mkdir ~/work
            > cd ~/work
            > tar xjf ....
            > cd website/source
            > ./makeall
            >
            > (requires Cheetah, yaml and python2.3)
            >
            >
            > -------------------------------------------------------
            > This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
            > Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
            > Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
            > REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
            > _______________________________________________
            > Cheetahtemplate-discuss mailing list
            > Cheetahtemplate-discuss@...
            > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss



            --
            -Aaron
            http://www.MetroNY.com/
            If the car industry behaved like the computer industry
            over the last 30 years, a Rolls-Royce would cost $5,
            get 300 miles per gallon, and blow up once a year
            killing all passengers inside.



            -------------------------------------------------------
            This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
            Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
            Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
            REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
            _______________________________________________
            Cheetahtemplate-discuss mailing list
            Cheetahtemplate-discuss@...
            https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
          • Aaron Held
            I overcame the staic problem w/ the pseudocode on the bottom of my last email: if you setup a template that extends a servlet and has placeholder functions
            Message 5 of 8 , Jun 16, 2004
            • 0 Attachment
              I overcame the staic problem w/ the pseudocode on the bottom of my last
              email:

              if you setup a template that extends a servlet and has placeholder
              functions such as
              <html>
              <body>
              $header
              $content
              </body>
              </html>

              then in your servlet you can have methods called
              def header(self)
              def content(self)
              and let the template execute thoes functions.

              Then each function in turn compiles a template and returns the string

              does this solve the problem?

              -Aaron

              Mike Orr wrote:

              >On Wed, Jun 16, 2004 at 05:19:01PM -0700, Mike Orr wrote:
              >
              >
              >>On Wed, Jun 16, 2004 at 02:59:06PM -0400, Aaron Held wrote:
              >>
              >>
              >>>Why not subclass the template (did I miss something?)
              >>>
              >>>
              >>That works too. But the static nature of subclass/superclass is
              >>inconvenient in some dynamic situations, especially when the subtemplate path
              >>cannot be known until the parent template has started executing, or indeed
              >>when mutiple subtemplate paths must be calculated on the fly and
              >>inserted in a loop.
              >>
              >>
              >
              >In other words, superclass/subclass templates (using
              >#block/#extends/#def, aka the "inheritance method" in the Users' Guide)
              >works in cases where the user directly chooses the subclass to display.
              >/articles/10140
              >It fails in cases where the user chooses the generic template and then
              >provides query parameters to indirectly choose the specific templates.
              >/products?id=111&id=222&id=333
              >The generic template itself has to calculate which specific templates
              >to incorporate. That can't be done with subclass templates.
              >
              >
              >




              -------------------------------------------------------
              This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
              Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
              Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
              REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
              _______________________________________________
              Cheetahtemplate-discuss mailing list
              Cheetahtemplate-discuss@...
              https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
            • Terrel Shumway
              ... Because I am an inheritance bigot :-). I never do with inheritance what SHOULD (IMOVHO) be done with composition or delegation. (Under the hood, even
              Message 6 of 8 , Jun 17, 2004
              • 0 Attachment
                Aaron Held wrote:

                > Why not subclass the template (did I miss something?)

                Because I am an inheritance bigot :-). I never
                do with inheritance what SHOULD (IMOVHO) be done
                with composition or delegation.

                (Under the hood, even inheritance in python is
                implemented by delegation.)

                > parent.tmpl
                > <html>
                > .....
                > #block content
                > THIS PAGE HAS NO CONTENT
                > #end block
                > ....
                > </html>
                >
                YUK! That's worse than #include $find_... :-)

                > Child:
                >
                > #from parent import parent
                > #extends parent
                > #def content
                > This is the real content
                > #end def

                Too brittle. If, for example, I introduce another
                layer "layout.tmpl", then I have to change all of
                the child templates to inherit from layout instead
                of parent.

                I want CONTENT.tmpl to know as little as possible
                about the context it is used in.

                This also prevents me from using different layouts
                in different contexts. The normal situation may be:

                /INCLUDE/INDEX.tmpl
                /CONTENT.tmpl
                /foo/CONTENT.tmpl

                and both / and /foo/ use the INDEX.tmpl in /INCLUDE

                But I may (and do) want to use a different layout
                for another part of the site:

                /bar/INDEX.tmpl
                /bar/CONTENT.tmpl
                /bar/baz/CONTENT.tmpl

                /bar/ and /bar/baz/ use the more local copy.

                Yes, maybe I *could* do this by making the directories all
                python packages and mucking around a lot with sys.path,
                but I don't *want* to. (This is a document tree, not a
                python package tree.)

                > Of course you have to precompile the parent templates first, and
                > reload everything if you change any parent tempate.

                I run a script to generate static html, so
                recompiling/reloading is a non-issue (so far).

                >
                > That became so much trouble I adopted this pattern:
                >
                > parent.tmpl
                > <html>
                > ...
                > $content
                > </html>

                Hey. that looks nice -- subtemplates. :-)


                Abundantly yours,
                -- Terrel


                p.s. Thank you all for your help. I did figure out how
                to make it work. (Now I'm scratching my head because the
                solution seems so simple I don't know why I didn't do it
                before.)

                I'll post the solution with some docs and examples
                after I knock off the rougher edges.

                p.p.s. Suriya, thanks for the aquarium pointer.
                It may fill perfectly a pressing need.

                _________________________________________________
                "Where can I find the tools I need to automate my
                online marketing?"

                http://direct-response-toolbox.com/?ref=ct57




                -------------------------------------------------------
                This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
                Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
                Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
                REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
                _______________________________________________
                Cheetahtemplate-discuss mailing list
                Cheetahtemplate-discuss@...
                https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
              • deelan
                ... yeah, subtemplates, recently i ve came accross to a similar solution too, take a look at my message on the webware list:
                Message 7 of 8 , Jun 17, 2004
                • 0 Attachment
                  Terrel Shumway wrote:

                  >>
                  >> That became so much trouble I adopted this pattern:
                  >>
                  >> parent.tmpl
                  >> <html>
                  >> ...
                  >> $content
                  >> </html>
                  >
                  >
                  > Hey. that looks nice -- subtemplates. :-)
                  yeah, subtemplates, recently i've came accross to a similar solution
                  too, take a look at my message on the webware list:

                  <https://sourceforge.net/mailarchive/message.php?msg_id=7728701>

                  hope this helps.

                  cheers,
                  deelan.






                  -------------------------------------------------------
                  This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
                  Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
                  Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
                  REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
                  _______________________________________________
                  Cheetahtemplate-discuss mailing list
                  Cheetahtemplate-discuss@...
                  https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                • Shannon --jj Behrens
                  Automatically compiling templates as well as making the connection between layouts and screens more flexible via:
                  Message 8 of 8 , Jun 17, 2004
                  • 0 Attachment
                    Automatically compiling templates as well as making the connection
                    between layouts and screens more flexible via:

                    <http://aquarium.sourceforge.net/api/public/aquarium.util.InternalLibrary.InternalLibrary-class.html#inverseExtend>

                    were two of my most prized additions to Aquarium.

                    Best Regards,
                    -jj

                    --- Terrel Shumway <mail@...> wrote:
                    > Aaron Held wrote:
                    >
                    > > Why not subclass the template (did I miss something?)
                    >
                    > Because I am an inheritance bigot :-). I never
                    > do with inheritance what SHOULD (IMOVHO) be done
                    > with composition or delegation.
                    >
                    > (Under the hood, even inheritance in python is
                    > implemented by delegation.)
                    >
                    > > parent.tmpl
                    > > <html>
                    > > .....
                    > > #block content
                    > > THIS PAGE HAS NO CONTENT
                    > > #end block
                    > > ....
                    > > </html>
                    > >
                    > YUK! That's worse than #include $find_... :-)
                    >
                    > > Child:
                    > >
                    > > #from parent import parent
                    > > #extends parent
                    > > #def content
                    > > This is the real content
                    > > #end def
                    >
                    > Too brittle. If, for example, I introduce another
                    > layer "layout.tmpl", then I have to change all of
                    > the child templates to inherit from layout instead
                    > of parent.
                    >
                    > I want CONTENT.tmpl to know as little as possible
                    > about the context it is used in.
                    >
                    > This also prevents me from using different layouts
                    > in different contexts. The normal situation may be:
                    >
                    > /INCLUDE/INDEX.tmpl
                    > /CONTENT.tmpl
                    > /foo/CONTENT.tmpl
                    >
                    > and both / and /foo/ use the INDEX.tmpl in /INCLUDE
                    >
                    > But I may (and do) want to use a different layout
                    > for another part of the site:
                    >
                    > /bar/INDEX.tmpl
                    > /bar/CONTENT.tmpl
                    > /bar/baz/CONTENT.tmpl
                    >
                    > /bar/ and /bar/baz/ use the more local copy.
                    >
                    > Yes, maybe I *could* do this by making the directories all
                    > python packages and mucking around a lot with sys.path,
                    > but I don't *want* to. (This is a document tree, not a
                    > python package tree.)
                    >
                    > > Of course you have to precompile the parent templates first, and
                    > > reload everything if you change any parent tempate.
                    >
                    > I run a script to generate static html, so
                    > recompiling/reloading is a non-issue (so far).
                    >
                    > >
                    > > That became so much trouble I adopted this pattern:
                    > >
                    > > parent.tmpl
                    > > <html>
                    > > ...
                    > > $content
                    > > </html>
                    >
                    > Hey. that looks nice -- subtemplates. :-)
                    >
                    >
                    > Abundantly yours,
                    > -- Terrel
                    >
                    >
                    > p.s. Thank you all for your help. I did figure out how
                    > to make it work. (Now I'm scratching my head because the
                    > solution seems so simple I don't know why I didn't do it
                    > before.)
                    >
                    > I'll post the solution with some docs and examples
                    > after I knock off the rougher edges.
                    >
                    > p.p.s. Suriya, thanks for the aquarium pointer.
                    > It may fill perfectly a pressing need.
                    >
                    > _________________________________________________
                    > "Where can I find the tools I need to automate my
                    > online marketing?"
                    >
                    > http://direct-response-toolbox.com/?ref=ct57
                    >
                    >
                    >
                    >
                    > -------------------------------------------------------
                    > This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
                    > Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
                    > Conference, June 28 - July 1 at the Moscone Center in San Francisco,
                    > CA
                    > REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code
                    > NWMGYKND
                    > _______________________________________________
                    > Cheetahtemplate-discuss mailing list
                    > Cheetahtemplate-discuss@...
                    > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
                    >





                    __________________________________
                    Do you Yahoo!?
                    New and Improved Yahoo! Mail - 100MB free storage!
                    http://promotions.yahoo.com/new_mail


                    -------------------------------------------------------
                    This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
                    Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
                    Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
                    REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
                    _______________________________________________
                    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.