HTML & Form Helpers
Foil (v0.4+) integrates Aura.HTML library that is used for escape (and autoescape) routine.
By default, Foil has just one helper that works as interface for all Aura.HTML helpers. It is
The supported helpers are:
- anchor / anchorRaw
Please refers to Aura.HTML docs to know more.
The first argument for
html() must be the helper name (one from the list above), all other arguments required by Aura.HTML helpers are supported.
For example, to render an
<img> tag is possible to do:
$this->html('img', '/images/hello.jpg', ['id' => 'image-id']);
That will render:
<img src="/images/hello.jpg" alt="hello" id="image-id">
The example comes from Aura.HTML docs, refers to that docs page to know how to similarly use all the supported helpers.
Short HTML Helpers
Setting an Engine option,
true, Foil will register additional HTML helpers,
that allow to call the supported Aura.HTML helpers straight on template instance (
For example, to print same
<img> tag as above:
$engine = Foil\engine([ 'folders' => ['path/to/templates'], 'html_tags_functions' => true ]); echo $engine->render('example'); // and inside path/to/templates/example.php <?= $this->img('/images/hello.jpg', ['id' => 'image-id']) ?>
All the other tags are supported in same way.
Aura.HTML has the helper
a() to print an
<a> HTML tag. However,
a() is already a Foil helper,
used to force a template variable to be an array (see Data / Helpers), for this reason to print an
anchor tag when
'html_tags_functions' option is true use
anchor() function, that is supported by
Aura.HTML as well.
Aura.HTML has some form helpers that can be used to render HTML5 form inputs.
- select (including options)
Foil supports all of them via
html() helper. For example:
$this->html('submit', ['name' => 'foo','value' => 'bar']);
That will render:
<input type="submit" name="foo" value="bar" />
So the first argument for
html() must be the input name, the second argument must be an array of all
the arguments supported by Aura.HTML.
Note that Aura.HTML requires the argument
'type' to be set to the input type ('text', 'submit'...)
that is not required by Foil
html() helper because input type is passed as first argument.
An alternative way to render form inputs (more similar to Aura.HTML syntax) is to use the generic
as first argument for
html(), then pass the input type as part of arguments array.
$this->html('input', ['type' => 'submit', 'name' => 'foo','value' => 'bar']);
'html_tags_functions' option is
true (see "Short HTML Helpers" above) form input names are not available
as template functions, because something like
$this->text() is hardly recognized as something
that prints a form input.
'html_tags_functions' options is possible to use
input() helper. For example:
$this->input(['type' => 'submit', 'name' => 'foo','value' => 'bar']);