Skip to content

Commit

Permalink
Add string and nocall functions for Python expressions. Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Oct 17, 2017
1 parent f09ad39 commit ed5180d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions CHANGES.rst
Expand Up @@ -5,12 +5,15 @@
3.1.0 (unreleased)
===================

- Added support for Python 3.6.
- Dropped support for Python 3.3.
- Add support for Python 3.6.
- Drop support for Python 3.3.
- Use the adapter namespace from ``zope.pagetemplate`` if it's
available, instead of the backwards compatibility shim in
``zope.app.pagetemplate``. See `issue 3
<https://github.com/zopefoundation/z3c.pt/issues/3>`_.
- Add the ``string`` and ``nocall`` functions for use inside Python
expressions. See `issue 2
<https://github.com/zopefoundation/z3c.pt/issues/2>`_.


3.0 (2016-09-02)
Expand Down
2 changes: 1 addition & 1 deletion src/z3c/pt/expressions.py
Expand Up @@ -244,7 +244,7 @@ class PythonExpr(BasePythonExpr):
tales=Builtin("tales"),
name=ast.Str(s=name),
mode="eval")
for name in ('path', 'exists')
for name in ('path', 'exists', 'string', 'nocall')
}

def __call__(self, target, engine):
Expand Down
20 changes: 20 additions & 0 deletions src/z3c/pt/tests/test_templates.py
Expand Up @@ -28,6 +28,26 @@ def setUp(self):
import z3c.pt
zope.configuration.xmlconfig.file('configure.zcml', z3c.pt)

class TestPageTemplate(Setup,
unittest.TestCase):

def test_string_function(self):
template = pagetemplate.PageTemplate('''<div tal:content="python:string('a string')" />''')
result = template.render()
self.assertEqual(result,
'<div>a string</div>')

def test_nocall_function(self):
class Call(object):
def __call__(self):
raise AssertionError("Should not be called")
def __str__(self):
return "Not Called"
arg = {'call': Call()}
template = pagetemplate.PageTemplate('''<div tal:content="python:nocall('arg/call')" />''')
result = template.render(arg=arg)
self.assertEqual(result,
'<div>Not Called</div>')

class TestPageTemplateFile(Setup,
unittest.TestCase):
Expand Down

0 comments on commit ed5180d

Please sign in to comment.