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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package pop-view

pop-view

Build Status Coverage Status

Join the chat at https://discord.gg/TZjgT74U7E

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


All versions of pop-view with dependencies

PHP Build Version
Package Version
Requires php Version >=8.4.0
popphp/pop-filter Version ^5.0.0
popphp/pop-utils Version ^3.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package popphp/pop-view contains the following files

Loading the files please wait ...