.. versionadded:: 1.2
The ``attribute`` function was added in Twig 1.2.
-``attribute`` can be used to access a "dynamic" attribute of a variable:
+The ``attribute`` function can be used to access a "dynamic" attribute of a
+variable:
.. code-block:: jinja
{{ attribute(object, method, arguments) }}
{{ attribute(array, item) }}
+In addition, the ``defined`` test can check for the existence of a dynamic
+attribute:
+
+.. code-block:: jinja
+
+ {{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}
+
.. note::
The resolution algorithm is the same as the one used for the ``.``
{{ attribute(obj, method) }}
{{ attribute(array, item) }}
{{ attribute(obj, "bar", ["a", "b"]) }}
+{{ attribute(obj, method) is defined ? 'ok' : 'ko' }}
+{{ attribute(obj, nonmethod) is defined ? 'ok' : 'ko' }}
--DATA--
-return array('obj' => new TwigTestFoo(), 'method' => 'foo', 'array' => array('foo' => 'bar'), 'item' => 'foo')
+return array('obj' => new TwigTestFoo(), 'method' => 'foo', 'array' => array('foo' => 'bar'), 'item' => 'foo', 'nonmethod' => 'xxx')
--EXPECT--
foo
bar
bar_a-b
+ok
+ko