Changeset 5355:3f87f0d36814 in livinglogic.python.xist

Show
Ignore:
Timestamp:
02/19/13 11:09:29 (7 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Subtemplates see themselves in the state of the variables of the parent template.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • docs/UL4.rst

    r5351 r5355  
    406406(via ``<?def?>``) inside another template has access to the local variables 
    407407of the outer template. The inner template sees that state of the variables at 
    408 the point in time when the ``<?def?>`` tag was executed. The following example 
     408the point in time when the ``<?def?>`` tag was executed (this includes the inner 
     409template itself, but no variables defined later). The following example 
    409410will output ``1``:: 
    410411 
     
    13041305 
    13051306 
     1307 
    13061308Delimiters 
    13071309========== 
  • src/ll/ul4c.py

    r5352 r5355  
    31963196        # Freeze variables of the currently running templates/functions 
    31973197        self.vars = vars.copy() 
     3198        # The template (i.e. the closure) itself should be visible in the parent variables 
     3199        self.vars[template.name] = self 
    31983200 
    31993201    def render(self, **vars): 
  • test/test_ul4.py

    r5354 r5355  
    31323132    assert "0!1!2!" == r(source, keepws=False) 
    31333133 
    3134     # Subtemplates see the state of the variable at the point of the ``<?def?>`` tag, 
     3134    # Subtemplates see the state of the variable at the point after the ``<?def?>`` tag, 
    31353135    # so the following code will use ``i = 1`` instead of ``i = 2`` even if the subtemplate is called after the variable has been changed. 
    31363136    source = """ 
     
    31433143    """ 
    31443144    assert "1" == r(source, keepws=False) 
     3145 
     3146 
     3147    # Subtemplates see themselves (i.e. the ``TemplateClosure`` object created for them), but no variables defined later 
     3148    source = """ 
     3149    <?def x?> 
     3150        <?print type(x)?>;<?print type(y)?> 
     3151    <?end def?> 
     3152    <?code y = 42?> 
     3153    <?code x.render()?> 
     3154    """ 
     3155    assert "template;undefined" == r(source, keepws=False) 
    31453156 
    31463157    # This shows the difference between local variables and variables from the parent.