Mercurial > genshi > mirror
changeset 297:d87b76fdaebb trunk
Allow `when` directives to omit the test expression as long as the associated `choose` directive does have one. In that case, the `when` branch is followed if the expression of the `choose` directive evaluates to a truth value.
author | cmlenz |
---|---|
date | Fri, 13 Oct 2006 15:02:51 +0000 |
parents | ebc084d5bcdb |
children | 5b1d5460fe25 |
files | genshi/template.py genshi/tests/template.py |
diffstat | 2 files changed, 24 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/genshi/template.py +++ b/genshi/template.py @@ -681,14 +681,18 @@ self.filename, *stream.next()[2][1:]) if matched: return [] - if not self.expr: - raise TemplateRuntimeError('"when" directive has no test condition', + if not self.expr and '_choose.value' not in frame: + raise TemplateRuntimeError('either "choose" or "when" directive ' + 'must have a test expression', self.filename, *stream.next()[2][1:]) - value = self.expr.evaluate(ctxt) if '_choose.value' in frame: - matched = (value == frame['_choose.value']) + value = frame['_choose.value'] + if self.expr: + matched = value == self.expr.evaluate(ctxt) + else: + matched = bool(value) else: - matched = bool(value) + matched = bool(self.expr.evaluate(ctxt)) frame['_choose.matched'] = matched if not matched: return []
--- a/genshi/tests/template.py +++ b/genshi/tests/template.py @@ -198,6 +198,21 @@ </doc>""") self.assertRaises(TemplateRuntimeError, str, tmpl.generate()) + def test_when_without_test_but_with_choose_value(self): + """ + Verify that an `when` directive that doesn't have a `test` attribute + works as expected as long as the parent `choose` directive has a test + expression. + """ + tmpl = MarkupTemplate("""<doc xmlns:py="http://genshi.edgewall.org/"> + <div py:choose="foo" py:strip=""> + <py:when>foo</py:when> + </div> + </doc>""") + self.assertEqual("""<doc> + foo + </doc>""", str(tmpl.generate(foo='Yeah'))) + def test_otherwise_without_test(self): """ Verify that an `otherwise` directive can be used without a `test`