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

Re: [Cheetahtemplate-discuss] blocks and locals

Expand Messages
  • Mike Orr
    ... In case it isn t clear, there are several kinds of scoping. nested lexical scoping means that if a function is defined inside another block of code, it
    Message 1 of 4 , Jul 31, 2002
      On Wed, Jul 31, 2002 at 09:33:05PM -0700, Mike Orr wrote:
      > On Wed, Jul 31, 2002 at 08:39:24PM -0700, Greg Czajkowski wrote:
      > > In the following code shouldn't I be able to use $i
      > > without declaring it global?
      > >
      > > from Cheetah.Template import Template
      > > print Template(source="""
      > > #for $i in range(10)
      > > #block testblock
      > > testing $i
      > > #end block
      > > #end for
      > > """)
      >
      > That would work for a Python program in that structure, provided it were
      > run on a recent version of Python that does nested scoping. But the
      > Cheetah compiler pulls the #block out and puts it in a method at the
      > same level as the main method (not nested inside the main method), so
      > nested scoping does not apply.

      In case it isn't clear, there are several kinds of scoping.

      "nested lexical scoping" means that if a function is defined inside
      another block of code, it can see the variables in that block. That's
      what Python's nested scoping is, and what Perl's "my" variables are.

      "nested dynamic scoping" means that when a function is called, it
      can see the variables in its caller, no matter where the calling
      code is physically defined. Python does not have this. Perl
      variables defined without "my" do this, but their use is depreciated.

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


      -------------------------------------------------------
      This sf.net email is sponsored by: Dice - The leading online job board
      for high-tech professionals. Search and apply for tech jobs today!
      http://seeker.dice.com/seeker.epl?rel_code=31
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Tavis Rudd
      ... I m not sure why you would want to do that. It s simpler and cleaner to do something like this: from Cheetah.Template import T print T( #def
      Message 2 of 4 , Aug 1, 2002
        On July 31, 2002 08:39 pm, Greg Czajkowski wrote:
        > Hi all,
        >
        > I wanted to know why locals() are never added to the
        > searchlist for block directives?
        > In the following code shouldn't I be able to use $i
        > without declaring it global?
        >
        > from Cheetah.Template import Template
        > print Template(source="""
        > #for $i in range(10)
        > #block testblock
        > testing $i
        > #end block
        > #end for
        > """)
        >
        > BTW. I understand the implementation behind #block,
        > can *my* local implementation be changed to
        > temporarily add the locals() to the called testblock
        > function's searchList? And then they would be removed
        > right after?? something like
        >
        > self._searchList.addToSearchList(locals())
        > self.blockName() # Calls the created functions
        > self._searchList.pop()

        I'm not sure why you would want to do that. It's simpler and cleaner to do
        something like this:

        from Cheetah.Template import T
        print T("""
        #def testBlock($i)
        testing $i
        #end def
        #for $i in $range(10)
        $testBlock($i)
        #end for
        """)

        Tavis


        -------------------------------------------------------
        This sf.net email is sponsored by:ThinkGeek
        Welcome to geek heaven.
        http://thinkgeek.com/sf
        _______________________________________________
        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.