[Checkins] SVN: Sandbox/malthe/chameleon.zpt/ Interpolation expressions are now supported inside static attributes, too.

Malthe Borch mborch at gmail.com
Mon Sep 22 12:18:22 EDT 2008


Log message for revision 91357:
  Interpolation expressions are now supported inside static attributes, too.

Changed:
  U   Sandbox/malthe/chameleon.zpt/README.txt
  U   Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.py
  U   Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.txt

-=-
Modified: Sandbox/malthe/chameleon.zpt/README.txt
===================================================================
--- Sandbox/malthe/chameleon.zpt/README.txt	2008-09-22 16:12:44 UTC (rev 91356)
+++ Sandbox/malthe/chameleon.zpt/README.txt	2008-09-22 16:18:21 UTC (rev 91357)
@@ -43,9 +43,10 @@
 
 4. The default expression type is Python.
 
-5. Genshi expression interpolation syntax is supported outside tags, e.g.
+5. Genshi expression interpolation syntax is supported outside tags
+and inside static attributes, e.g.
 
-      <span>
+      <span class="hello-${'world'}">
          Hello, ${'world'}!
       </span>
 

Modified: Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.py
===================================================================
--- Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.py	2008-09-22 16:12:44 UTC (rev 91356)
+++ Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.py	2008-09-22 16:18:21 UTC (rev 91357)
@@ -5,6 +5,7 @@
 from chameleon.core import translation
 from chameleon.core import config
 from chameleon.core import etree
+from chameleon.core import types
 from chameleon.core import utils
 
 import interfaces
@@ -57,9 +58,28 @@
 
         @property
         def dynamic_attributes(self):
-            return (self.element.tal_attributes or ()) + \
-                   (self.element.meta_attributes or ())
+            attributes = []
 
+            xhtml_attributes = utils.get_attributes_from_namespace(
+                self.element, config.XHTML_NS)
+            
+            for name, value in xhtml_attributes.items():
+                parts = self.element.translator.split(value)
+                for part in parts:
+                    if isinstance(part, types.expression):
+                        attributes.append(
+                            (types.declaration((name,)), types.join(parts)))
+                        break
+                    
+            if self.element.tal_attributes is not None:
+                attributes.extend(self.element.tal_attributes)
+
+            if self.element.meta_attributes is not None:
+                attributes.extend(self.element.meta_attributes)
+
+            if len(attributes) > 0:
+                return attributes
+
         @property
         def translated_attributes(self):
             return self.element.i18n_attributes

Modified: Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.txt
===================================================================
--- Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.txt	2008-09-22 16:12:44 UTC (rev 91356)
+++ Sandbox/malthe/chameleon.zpt/src/chameleon/zpt/language.txt	2008-09-22 16:18:21 UTC (rev 91357)
@@ -295,6 +295,8 @@
 The ``chameleon.zpt`` parser supports Genshi interpolation
 expressions.
 
+Outside tags:
+
   >>> print render("""\
   ... <div xmlns="http://www.w3.org/1999/xhtml"
   ...      xmlns:tal="http://xml.zope.org/namespaces/tal">
@@ -303,3 +305,14 @@
   <div>
     Interpolation expressions are convenient.
   </div>
+
+Inside tags:
+
+  >>> print render("""\
+  ... <div xmlns="http://www.w3.org/1999/xhtml"
+  ...      xmlns:tal="http://xml.zope.org/namespaces/tal">
+  ...   <img alt="Interpolation ${'expressions'} are ${'convenient'}" />
+  ... </div>""")
+  <div>
+    <img alt="Interpolation expressions are convenient" />
+  </div>



More information about the Checkins mailing list