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

[Cheetahtemplate-discuss] Re: Having trouble with #include

Expand Messages
  • Aaron Buhr
    ... I preferred to avoid set global because it s not thread-safe and because in general global vars seem to be a bit sloppy programming practice. However if
    Message 1 of 3 , Aug 5, 2003
    • 0 Attachment
      > Message: 2
      > Date: Mon, 4 Aug 2003 08:10:53 -0700
      > From: Mike Orr <mso@...>
      > To: Cheetah Template List <cheetahtemplate-discuss@...>
      > Subject: Re: [Cheetahtemplate-discuss] Having trouble with #include
      > Reply-To: mso@...
      >
      > On Mon, Aug 04, 2003 at 10:53:43AM -0400, Aaron Buhr wrote:
      > > However my first problem was that the variables were coming up
      > undefined,
      > > which from reading the archives is how the #include is supposed
      > to work -
      > > namespace not shared or some such. So in my #included file I
      > then copied
      > > over my variable assignment (from a session var) from the
      > including file,
      >
      > searchList variables should be visible automatically. Use '#set global'
      > instead of '#set' for other variables that should be visible in the
      > included file.

      I preferred to avoid set global because it's not thread-safe and because in
      general global vars seem to be a bit sloppy programming practice. However
      if that's the simplest way I will do it.

      I am unclear on why re-loading the variable from session state gave me the
      Nonetype error though. My original file (the including file) works fine and
      looks more or less like this:
      #extends CCGWeb_ServiceApp
      #implements respond
      #set $ValidPage1 = $IsValidPage1()
      #set $serviceAppMsg = $self.session().value('serviceAppMsg', None)
      #set $ssn = $self.session().value('ssn', None)

      #if $ValidPage1
      #include svcapp_page2
      #else
      There was an error with your data:<br>
      $serviceAppMsg<br>
      Click <a href="svcapp_page1">here</a> to try again.
      <meta http-equiv="Refresh" content="0; URL=svcapp_page1">
      #end if

      The second file was just this:
      #set $ssn = $self.session().value('ssn', None)
      Testing: $ssn

      Is the file specified at #include supposed to be a Cheetah template file,
      or is it supposed to be the compiled Cheetah .py file? I just tried the .py
      file and instead of the Nonetype not callable I get "Notfound: ssn". FWIW
      if I do use set global then it works when I #include the template file, but
      excluding the #set line from the included file. Are Cheetah directives not
      allowed in included files?

      Thanks for the help.



      -------------------------------------------------------
      This SF.Net email sponsored by: Free pre-built ASP.NET sites including
      Data Reports, E-commerce, Portals, and Forums are available now.
      Download today and enter to win an XBOX or Visual Studio .NET.
      http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Mike Orr
      ... #include is a mixed blessing. The Cheetah developers rarely use it. It mainly exists for unforeseen situations where it s necessary, like yours is. The
      Message 2 of 3 , Aug 5, 2003
      • 0 Attachment
        On Tue, Aug 05, 2003 at 02:03:50PM -0400, Aaron Buhr wrote:
        > > searchList variables should be visible automatically. Use '#set global'
        > > instead of '#set' for other variables that should be visible in the
        > > included file.
        >
        > I preferred to avoid set global because it's not thread-safe and because in
        > general global vars seem to be a bit sloppy programming practice. However
        > if that's the simplest way I will do it.

        #include is a mixed blessing. The Cheetah developers rarely use it.
        It mainly exists for unforeseen situations where it's necessary, like
        yours is.

        The biggest thing we wanted to prevent was people using
        #include instead of $functions(), using global variables to pass data
        in and out, and having the include file cause unexpected changes to the
        parent's environment. Overuse of #include is endemic in PHP and some
        other languages, and we didn't want Cheetah to go that route. That's
        the reason '#set global' exists: to make it possible but inconvenient to
        share non-searchList data, while making it loud and clear what you're
        doing.

        Your example shows that our compromises with #include cut both ways:
        the same construct is good for some ppl but bad for others. But at
        least the construct allows ppl to do what they need somehow.

        > I am unclear on why re-loading the variable from session state gave me the
        > Nonetype error though. My original file (the including file) works fine and
        > looks more or less like this:
        > #extends CCGWeb_ServiceApp
        > #implements respond
        > #set $ValidPage1 = $IsValidPage1()
        > #set $serviceAppMsg = $self.session().value('serviceAppMsg', None)
        > #set $ssn = $self.session().value('ssn', None)
        >
        > #if $ValidPage1
        > #include svcapp_page2
        > #else
        > There was an error with your data:<br>
        > $serviceAppMsg<br>
        > Click <a href="svcapp_page1">here</a> to try again.
        > <meta http-equiv="Refresh" content="0; URL=svcapp_page1">
        > #end if
        >
        > The second file was just this:
        > #set $ssn = $self.session().value('ssn', None)
        > Testing: $ssn
        >
        > Is the file specified at #include supposed to be a Cheetah template file,
        > or is it supposed to be the compiled Cheetah .py file? I just tried the .py
        > file and instead of the Nonetype not callable I get "Notfound: ssn". FWIW
        > if I do use set global then it works when I #include the template file, but
        > excluding the #set line from the included file. Are Cheetah directives not
        > allowed in included files?

        The included file should be Cheetah source. ('#include raw' inserts the
        content literally without interpretation.) I'm not sure if it's possible
        to include a compiled template module.

        The reason you're getting None (if I understand
        #include correctly) is that the include file is a separate template instance,
        so it has a different 'self'. Webware transaction info is not passed to
        the second instance, so it acts like a standalone template. We discovered
        recently that $methods() also don't have access to the Webware transaction.
        So your best choice is to '#set global' the data you need.

        '#set global' is not thread safe for template instances, but each
        thread can have its own instance. We haven't found a lot of need to
        reuse template instances since it takes no perceptable time to create
        a new instance. And with Webware, it's convenient to let Webware handle
        servlet threading and not worry if the template is thread unsafe, because
        it makes for much more convenient programming.

        I usually receommend avoiding #include and instead using methods or
        functions, especially if you need to pass data in and out. If your
        application has too many branches to do an 'if' switch, or you have to
        calculate which method to call at runtime, that could be difficult, but
        it's probably not insurmountable, and may lead to cleaner code than
        using #include.

        --
        -Mike (Iron) Orr, mso@... (iron@...)
        http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol


        -------------------------------------------------------
        This SF.Net email sponsored by: Free pre-built ASP.NET sites including
        Data Reports, E-commerce, Portals, and Forums are available now.
        Download today and enter to win an XBOX or Visual Studio .NET.
        http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      • Mike Orr
        ... The entire top-level template (outside any #def/#block) executes in a method in the template instance -- normally .respond(). #set creates/updates a local
        Message 3 of 3 , Aug 5, 2003
        • 0 Attachment
          > On Tue, Aug 05, 2003 at 02:03:50PM -0400, Aaron Buhr wrote:
          > > if I do use set global then it works when I #include the template file, but
          > > excluding the #set line from the included file. Are Cheetah directives not
          > > allowed in included files?

          The entire top-level template (outside any #def/#block) executes in a
          method in the template instance -- normally .respond(). #set
          creates/updates a local variable in that method.

          The presence of #set also tells the Cheetah compiler that this variable
          (in THIS method or include file) is local, so any $placeholder referring
          to that variable is done as a local variable lookup (which is much
          faster) rather than a searchList lookup. This bypasses any '#set
          global' value, because '#set global' is handled via the searchList.

          #set is independent for each method or include file. In any one it
          appears, it makes lookups for that variable local.

          --
          -Mike (Iron) Orr, mso@... (iron@...)
          http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol


          -------------------------------------------------------
          This SF.Net email sponsored by: Free pre-built ASP.NET sites including
          Data Reports, E-commerce, Portals, and Forums are available now.
          Download today and enter to win an XBOX or Visual Studio .NET.
          http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
          _______________________________________________
          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.