Argument ViewHelper <f:argument>
f:argument
allows to define requirements and type constraints to variables that
are provided to templates and partials. This can be very helpful to document how
a template or partial is supposed to be used and which input variables are required.
These requirements are enforced during rendering of the template or partial:
If an argument is defined with this ViewHelper which isn't marked as optional
,
an exception will be thrown if that variable isn't present during rendering.
If a variable doesn't match the specified type and can't be converted automatically,
an exception will be thrown as well.
Note that f:argument
ViewHelpers must be used at the root level of the
template, and can't be nested into other ViewHelpers. Also, the usage of variables
in any of its arguments is not possible (e. g. you can't define an argument name
by using a variable).
Example
For the following partial:
<f:argument name="title" type="string" />
<f:argument name="tags" type="string[]" optional="{true}" />
<f:argument name="user" type="string" optional="{true}" default="admin" />
Title: {title}<br />
<f:if condition="{tags}">
Tags: {tags -> f:join(separator: ', ')}<br />
</f:if>
User: {user}
The following render calls will be successful:
<!-- All arguments supplied -->
<f:render partial="MyPartial" arguments="{title: 'My title', tags: {0: 'tag1', 1: 'tag2'}, user: 'me'}" />
<!-- "user" will fall back to default value -->
<f:render partial="MyPartial" arguments="{title: 'My title', tags: {0: 'tag1', 1: 'tag2'}}" />
<!-- "tags" will be "null", "user" will fall back to default value -->
<f:render partial="MyPartial" arguments="{title: 'My title'}" />
The following render calls will result in an exception:
<!-- required "title" has not been supplied -->
<f:render partial="MyPartial" />
<!-- "user" has been supplied as array, not as string -->
<f:render partial="MyPartial" arguments="{title: 'My title', user: {firstName: 'Jane', lastName: 'Doe'}}" />
Go to the source code of this ViewHelper: ArgumentViewHelper.php (GitHub).
Arguments
The following arguments are available for the argument ViewHelper:
default
-
- Type
- mixed
default value for optional argument
description
-
- Type
- string
description of the template argument
name
-
- Type
- string
- Required
- 1
name of the template argument
optional
-
- Type
- boolean
- Default
- false
true if the defined argument should be optional
type
-
- Type
- string
- Required
- 1
type of the template argument