diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py
index aaa9c4719fb265db9de7283c6138b473709bfec2..779f27f748425cb9868b2d51e1a70ff2ddd026ea 100644
--- a/lib/mako/codegen.py
+++ b/lib/mako/codegen.py
@@ -136,7 +136,7 @@ class _GenerateRenderMethod(object):
     def write_inherit(self, node):
         self.printer.writeline("def _mako_inherit(context):")
         self.printer.writeline("_mako_generate_namespaces(context)")
-        self.printer.writeline("return runtime.inherit_from(context, %s, _template_filename)" % (repr(node.attributes['file'])))
+        self.printer.writeline("return runtime.inherit_from(context, %s, _template_filename)" % (node.parsed_attributes['file']))
         self.printer.writeline(None)
 
     def write_namespaces(self, namespaces):
diff --git a/lib/mako/parsetree.py b/lib/mako/parsetree.py
index d1a35a1330c83ba14b11283ad899812b298d9a2a..476dbb2f6893c23237fdefa298832f9bb024649b 100644
--- a/lib/mako/parsetree.py
+++ b/lib/mako/parsetree.py
@@ -199,7 +199,8 @@ class Tag(Node):
                         undeclared_identifiers = undeclared_identifiers.union(code.undeclared_identifiers)
                         expr.append(m.group(1))
                     else:
-                        expr.append(repr(x))
+                        if x:
+                            expr.append(repr(x))
                 self.parsed_attributes[key] = " + ".join(expr)
             elif key in nonexpressions:
                 if re.search(r'${.+?}', self.attributes[key]):
diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py
index 54445497fb15675d7136ff81dc12b03cb1ad62a7..c98b9148bf1018c65c707beaa1945f4cd7378175 100644
--- a/lib/mako/runtime.py
+++ b/lib/mako/runtime.py
@@ -163,6 +163,8 @@ def include_file(context, uri, calling_filename):
 def inherit_from(context, uri, calling_filename):
     """called by the _inherit method in template modules to set up the inheritance chain at the start
     of a template's execution."""
+    if uri is None:
+        return None
     template = _lookup_template(context, uri, calling_filename)
     self_ns = context['self']
     ih = self_ns
@@ -193,7 +195,7 @@ def _populate_self_namespace(context, template, self_ns=None):
         self_ns = Namespace('self:%s' % template.description, context, template=template, populate_self=False)
     context._data['self'] = context._data['local'] = self_ns
     if hasattr(template.module, '_mako_inherit'):
-        return template.module._mako_inherit(context)
+        return template.module._mako_inherit(context) or (template.callable_, context)
     else:
         return (template.callable_, context)
 
diff --git a/test/inheritance.py b/test/inheritance.py
index 28bc78f5b0ba124fa6f293c6065a1596f5789a0c..fbebcf3b26673e1f30eb6a49faaef9cfd5afedef 100644
--- a/test/inheritance.py
+++ b/test/inheritance.py
@@ -180,7 +180,30 @@ ${next.body()}
          'a is: layout_a',
          'c is: secondary_c. a is layout_a b is base_b d is secondary_d.'
          ]
-        
-        
+
+    def test_dynamic(self):
+        collection = lookup.TemplateLookup()
+        collection.put_string("base", """
+            this is the base.
+            ${next.body()}
+        """)
+        collection.put_string("index", """
+            <%!
+                def dyn(context):
+                    if context.get('base', None) is not None:
+                        return 'base'
+                    else:
+                        return None
+            %>
+            <%inherit file="${dyn(context)}"/>
+            this is index.
+        """)
+        assert result_lines(collection.get_template('index').render()) == [
+            'this is index.'
+        ]
+        assert result_lines(collection.get_template('index').render(base=True)) == [
+            'this is the base.',
+            'this is index.'
+        ]
 if __name__ == '__main__':
     unittest.main()