.. include:: /Includes.rst.txt .. _creating-expressionnodes: ======================== Creating ExpressionNodes ======================== To understand what an ExpressionNode is and which cases can be solved by implementing custom ones, first read the :doc:`chapter about implementing Fluid `. Once you grasp what an ExpressionNode is and how it works, a very brief example is all you need. First: an ExpressionNode is always one PHP class. Where you place it is completely up to you - but to have the class actually be detected and used by Fluid, *the class name must be returned from a custom ViewHelperResolver*. This concept is also explained in the implementation chapter. In Fluid's default ViewHelperResolver, the following code is responsible for returning expression node class names: .. code-block:: php /** * List of class names implementing ExpressionNodeInterface * which will be consulted when an expression does not match * any built-in parser expression types. * * @var string */ protected $expressionNodeTypes = [ 'TYPO3Fluid\\Fluid\\Core\\Parser\\SyntaxTree\\Expression\\CastingExpressionNode', 'TYPO3Fluid\\Fluid\\Core\\Parser\\SyntaxTree\\Expression\\MathExpressionNode', 'TYPO3Fluid\\Fluid\\Core\\Parser\\SyntaxTree\\Expression\\TernaryExpressionNode', ]; /** * @return string */ public function getExpressionNodeTypes() { return $this->expressionNodeTypes; } You may or may not want the listed expression nodes included, but if you change the available expression types you should of course document this difference about your implementation. The following are fairly normal ways of replacing or extending this array of class names: * Override the property `$expressionNodeTypes` and define your own array * Override the `getExpressionNodeTypes` method and return a complete array * Override the `getExpressionNodeTypes` method and modify/append the array from `parent::getExpressionNodeTypes` Once you are ready to create your ExpressionNode class, all you need is a very simple one. The following class is the ternary ExpressionNode from Fluid itself which detects the `{a ? b : c}` syntax and evaluates `a` as boolean and if true, renders `b` else renders `c`. To get this behavior, we need a (relatively simple) regular expression and one method to evaluate the expression while being aware of the rendering context (which stores all variables, controller name, action name etc). .. code-block:: php