Download the PHP package brightnucleus/view without Composer
On this page you can find all versions of the php package brightnucleus/view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brightnucleus/view
More information about brightnucleus/view
Files in brightnucleus/view
Package view
Short Description Reusable View component, that can provide different implementations.
License MIT
Informations about the package view
Bright Nucleus View Component
This is a reusable View component that can provide different implementations (in separate, optional packages).
Table Of Contents
- Installation
- Basic Usage
- Adding Locations
- Rendering A View
- Advanced Usage
- Contributing
- License
Installation
The best way to use this component is through Composer:
Basic Usage
The simplest way to use the View
component is through its Facade: BrightNucleus\Views
.
This Facade makes use of a basic BaseView
View implementation as well as a basic PHPEngine
Engine implementation. It can just be used as is and does not need additional configuration.
Adding Locations
You can add locations via the static View::addLocation($location)
method. Each location needs to implement the Location
. The View
component comes with one location provider out of the box: FilesystemLocation
.
Here's how to add a set of folders as a new location:
Rendering A View
To render a view, you pass a view identifier to the static View::render($view, $context, $type)
method.
The $view
is a view identifier that would normally be a template file name, but can also be something else depending on your configured locations & engines.
The $context
array you pass in will be extracted to be available to the template at the time it is rendered.
The $type
argument allows you to inject a specific view/engine combination, instead of letting the View
component find one on its own.
Note: In order for a view to be effectively rendered, it needs to be found amongst one of the locations that were already registered.
Here's how to render a simple view:
Context
From within the template that is being rendered, the context variables are available as properties.
As an example, for the view we rendered above, you could use echo $this->userId;
from within the template to retrieve that specific piece of context data.
If you add invokable object to the context as properties, they will in effect act as methods within the view template.
The context as a whole is available through the method $this->getContext()
, which will return an associative array.
Keep in mind that no automatic escaping is taking place, the value of the context data is passed as-is.
Sections
To render a different template as a section from within the template currently being rendered, you can use the $this->section($view, $context, $type)
method.
This does basically the same thing as an external render()
call of a View
object, with the following differences:
- It reuses the parent's
ViewBuilder
, with the same rendering engine, and the same locations. - If you provide additional context, it is merged with the parent's context.
Here's an example of how this works:
Advanced Usage
For more advanced use cases, you'll want to provide custom classes for your Views or Engines.
Instantiating A Custom ViewBuilder
To do this, you'll want to create your ViewBuilder
object manually, instead of relying on the Views
Facade. By instantiating it manually, you can provide a custom Config to map your classes.
Once, you've got a ViewBuilder
instance, you can use the addLocation($location)
method to add locations to scan for views and the create($view, $type)
method for creating an actual View. This View can then be rendered through its render($context)
method.
Configuration Schema
Here's an example for providing a custom Config. In this case, we want to replace the default classes with more awesome ones.
Of course you don't need to override all of the classes, views or engines. If you only override specific keys, the rest will be taken from the default values.
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2016-2017 Alain Schlesser, Bright Nucleus
This code is licensed under the MIT License.
All versions of view with dependencies
brightnucleus/exceptions Version >=0.4
brightnucleus/config Version >=0.5
doctrine/collections Version >=1.3
symfony/finder Version >=3.1