.. include:: /Includes.rst.txt .. _variables: ========= Variables ========= Assign a variable in PHP: .. code-block:: php $this->view->assign('title', 'An example title'); In the template's HTML code, wrap the variable name into curly braces to output it: .. code-block:: html

{title}

The result: .. code-block:: html

An example title

.. _variable-escaping: Output Escaping for Variables ============================= By default, Fluid escapes the output of variables to prevent cross-site-scripting attacks. If a variable contains characters that have special meaning in HTML, such as `<` or `>`, these will be replaced with the matching HTML entities, such as `<` or `>`. This can be avoided by passing the variable to a ViewHelper that disables output escaping, such as ` `_. .. important:: Be extra careful when using `` in your template. Make sure that all user-provided input is escaped properly! The following variable: .. code-block:: php $this->view->assign('title', 'An example title'); would be escaped automatically in a template, unless wrapped in ``: .. code-block:: html {title} Result: An <b>example</b> title {title} Result: An example title .. _variable-all: Special _all Variable ===================== The special variable `{_all}` contains an array with all variables that are currently defined in your template. This can be helpful for debugging purposes, but also if you want to pass all variables to a partial: .. code-block:: xml However, be advised that this makes it more difficult to re-use partials, so it's recommend to only pass the variables that are actually needed in the partial. .. _variable-scopes: Variable Scopes =============== Each Fluid template, partial and section has its own variable scope. For templates, these variables are set via the PHP API, for partials and sections the `` ViewHelper has a `arguments` argument to provide variables. Inside templates, partials and sections there are two variable scopes: global variables and local variables. Local variables are created by ViewHelpers that provide additional variables to their child nodes. Local variables are only valid in their appropriate context and don't leak out to the whole template. For example, `` and `` create local variables: .. code-block:: xml If a global variable uses the same name as a local value, the state of the global value will be restored when the local variable is invalidated: .. code-block:: xml If a variable is created in a local block, for example by using the `` ViewHelper, that variable is treated as a global variable, so it will leak out of the scope: .. code-block:: xml If a global variable is created inside a local scope and uses the same name as a local variable, it will still leak out of the scope and will also be valid inside the scope: .. code-block:: xml