Download the PHP package popphp/pop-view without Composer
On this page you can find all versions of the php package popphp/pop-view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download popphp/pop-view
More information about popphp/pop-view
Files in popphp/pop-view
Package pop-view
Short Description Pop View Component for Pop PHP Framework
License BSD-3-Clause
Homepage https://github.com/popphp/pop-view
Informations about the package pop-view
pop-view
- Overview
- Install
- Quickstart
- File Template
- Stream Template
- Includes
- Inheritance
- Iteration
- Conditionals
- Compiled & Cached Templates
- Filters
- Working with View Data
Overview
pop-view is the view template component that can be used as the "V" in an MVC stack or
independently as well. It supports using both PHP-file based templates and stream templates.
Within the stream templates, there is basic support for logic and iteration for dynamic
control over the view template.
pop-view is a component of the Pop PHP Framework.
Top
Install
Install pop-view using Composer.
composer require popphp/pop-view
Or, require it in your composer.json file
"require": {
"popphp/pop-view" : "^5.0.0"
}
Top
Quickstart
Consider a phtml template file like this:
You can set up a view object and populate data like this:
View auto-detects the template type from what you pass it: a path ending in .phtml/.php that
exists on disk becomes a file template, and anything else (an .html path, or a raw template string)
becomes a stream template - no need to explicitly wrap it in Template\File/Template\Stream
yourself unless you want more control, such as passing a
cache directory to a stream template. See the
Stream Template sections below for the
explicit form.
Data can also be passed directly to the constructor instead of being set afterward:
which will produce:
Top
File Template
A file template simply uses PHP variables to deliver the data and content to template to be rendered. With a file template, you have full access to the PHP environment to write any additional code or helper scripts. However, in using this, you must make sure to adhere to the best practices and standards regarding the security of the application.
hello.phtml
You can set up the view object like this:
Top
Stream Template
A stream template uses a formatted string placeholder to deliver the data and content to template to be rendered:
hello.html
You can set up the view object in a similar way and it will render the exact same as the file template example.
A value nested one level down in an array can be substituted directly with [{name[index]}],
without having to flatten the data first:
Top
Includes
Stream templates support includes to allow you to include other templates within them.
header.html
footer.html
index.html
You can set up the view object like before:
Top
Inheritance
Stream templates support inheritance to allow you to extend other templates.
parent.html
child.html
You can set up the view object like before:
A parent template isn't limited to a single named block - declare as many {{blockname}}...{{/blockname}}
pairs as you need, and a child can override any subset of them:
Top
Iteration
Iteration is possible in stream templates when working with arrays and array-like objects.
A loop over a plain, numerically-indexed list of records works the same way, except each entry's own
fields are addressed directly by name instead of via [{key}]/[{value}]. [{i}] is available in
either form and gives the 1-indexed position within the loop:
A loop entry whose own value is itself an array, keyed by a non-numeric name, is treated as a nested sub-loop - declare a tag with that same name inside the outer loop's body:
Top
Conditionals
Conditional logic is possible within a stream template as well.
A nested array value can be checked and printed with the same [{name[index]}] syntax used for
array-index scalars outside of a conditional:
Conditionals also work inside a loop body, evaluated per-row against that row's own fields:
Top
Compiled & Cached Templates
By default, a stream template is re-parsed on every render() call. For templates rendered
repeatedly (e.g. on every request), you can opt in to a compiled/cached render path by giving
Template\Stream a writable cache directory - either as the constructor's second argument, or via
setCacheDir():
The first render compiles the template into plain PHP and writes it to the cache directory, keyed by
a hash of the fully-resolved template content (after @extends/@include/blocks have all been
merged in). Subsequent renders - including across requests, once the compiled file exists on disk -
skip parsing entirely and just include the compiled PHP file. The cache is automatically
invalidated whenever the resolved template content changes, so there's nothing to clear by hand
during normal development.
Every placeholder, loop, and conditional shown above is supported identically on the compiled path - switching a template over to a cache directory is a pure performance opt-in with no change in rendered output.
Template\File templates don't need this: they're already plain PHP, included directly with no
separate parsing step.
Top
Filters
View can run its data through one or more Pop\Filter
filters before rendering, useful for things like sanitizing output. Pass a filter (or an array of
filters) as the third constructor argument, or add them afterward:
A filter can be excluded from specific fields by name, so it only applies to the rest of the data:
Other available methods: hasFilters(), getFilters(), and clearFilters() to remove all
configured filters.
Top
Working with View Data
Besides passing data through the constructor, View supports a few equivalent ways of getting data
in and out, since it extends Pop\Utils\ArrayObject:
Top