[ADD] qweb-js: jinja-style interpolation pattern
authorXavier Morel <xmo@openerp.com>
Mon, 8 Sep 2014 14:42:15 +0000 (16:42 +0200)
committerXavier Morel <xmo@openerp.com>
Mon, 6 Oct 2014 17:13:41 +0000 (19:13 +0200)
addons/web/static/lib/qweb/qweb-test-attributes.xml
addons/web/static/lib/qweb/qweb-test.js.html
addons/web/static/lib/qweb/qweb2.js

index d0d22b1..d03c20e 100644 (file)
     <t t-name="format-multiple">
         <div t-attf-foo="a #{value1} is #{value2} of #{value3} ]"/>
     </t>
+
+    <t t-name="format2-literal">
+        <div t-attf-foo="bar"/>
+    </t>
+    <t t-name="format2-value">
+        <div t-attf-foo="b{{value}}r"/>
+    </t>
+    <t t-name="format2-expression">
+        <div t-attf-foo="{{value + 37}}"/>
+    </t>
+    <t t-name="format2-multiple">
+        <div t-attf-foo="a {{value1}} is {{value2}} of {{value3}} ]"/>
+    </t>
 </templates>
index 510702e..b768ed9 100644 (file)
                     '<div foo="a 0 is 1 of 2 ]"></div>',
                     "each format string should be evaluated independently");
             });
+            QUnit.test('Fixed name, jinja-formatted', function (assert) {
+                assert.equal(render('format2-literal', {}), '<div foo="bar"></div>',
+                    "Literal format");
+                assert.equal(render('format2-value', {value:'a'}), '<div foo="bar"></div>',
+                    "Valued format");
+                assert.equal(
+                    render('format2-expression', {value: 5}),
+                    '<div foo="42"></div>',
+                    "Format strings are evaluated expressions");
+                assert.equal(render('format2-multiple', {
+                        value1: 0,
+                        value2: 1,
+                        value3: 2,
+                    }),
+                    '<div foo="a 0 is 1 of 2 ]"></div>',
+                    "each format string should be evaluated independently");
+            });
 
             QUnit.module("Template calling (including)", {
                 setup: function () {
index 983a3d4..e4a792e 100644 (file)
@@ -609,12 +609,12 @@ QWeb2.Element = (function() {
                 s && r.push(_this.engine.tools.js_escape(s));
             }
 
-            var re = /#{(.*?)}/g, start = 0, r = [], m;
+            var re = /(?:#{(.+?)}|{{(.+?)}})/g, start = 0, r = [], m;
             while (m = re.exec(s)) {
                 // extract literal string between previous and current match
                 append_literal(s.slice(start, re.lastIndex - m[0].length));
                 // extract matched expression
-                r.push('(' + this.format_expression(m[1]) + ')');
+                r.push('(' + this.format_expression(m[2] || m[1]) + ')');
                 // update position of new matching
                 start = re.lastIndex;
             }