Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py:import doesn't work with py:extends #15

Closed
ollyc opened this issue Oct 21, 2015 · 1 comment · Fixed by #81
Closed

py:import doesn't work with py:extends #15

ollyc opened this issue Oct 21, 2015 · 1 comment · Fixed by #81

Comments

@ollyc
Copy link
Contributor

ollyc commented Oct 21, 2015

I can't seem to use <py:import> within a template that is called via <py:extends>

Here's a test case to show what I'm talking about:

def test_extends_with_import(self):
    loader = MockLoader({
        'parent.html': XMLTemplate(
            '<div>'
            '<py:import href="lib.html" alias="lib"/>'
            '${lib.foo()}'
            '</div>'),
        'lib.html': XMLTemplate(
            '<div>'
            '<py:def function="foo()"><b>foo</b></py:def>'
            '</div>'),
        'child.html': XMLTemplate('<py:extends href="parent.html"/>')})

    child = loader.import_('child.html')
    r = child().render()
    assert r == '<div><b>foo</b></div>'

This fails with the error NameError: name 'lib' is not defined.

I can fix it easily enough with this patch:

diff --git a/kajiki/ir.py b/kajiki/ir.py
--- a/kajiki/ir.py
+++ b/kajiki/ir.py
@@ -96,10 +96,13 @@
         self.alias = alias

     def py(self):
-        yield self.line(
-            'local.__kj__.import_(%r, %r, self.__globals__)' % (
-                self.tpl_name, self.alias))
+        s = 'local.__kj__.import_(%r, %r, self.__globals__)' % (
+                self.tpl_name, self.alias)

+        if self.alias:
+            yield self.line('%s = %s' % (self.alias, s))
+        else:
+            yield self.line(s)

 class IncludeNode(Node):
     def __init__(self, tpl_name):
diff --git a/kajiki/tests/test_xml.py b/kajiki/tests/test_xml.py
--- a/kajiki/tests/test_xml.py
+++ b/kajiki/tests/test_xml.py
@@ -542,6 +542,23 @@

 </html>''', rsp

But I'm not really happy with this. I don't completely understand what the role of self.__globals__ is here. I'm sure there must be a better fix than this!

@alexbodn
Copy link

thanks a lot. working for me.
could include be activated too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants