Download the PHP package coreorm/slim3-view without Composer
On this page you can find all versions of the php package coreorm/slim3-view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download coreorm/slim3-view
More information about coreorm/slim3-view
Files in coreorm/slim3-view
Package slim3-view
Short Description view renderer for slim3 with very straightforward and simple (yet still customisable) theme/layout/templates structure
License MIT
Informations about the package slim3-view
slim3-view
View renderer for slim3 with very straightforward and simple (yet still customisable) theme/layout/templates structure.
This also works with any PSR-7 compliant response objects.
Example
- live demo: https://slim3-view-example.herokuapp.com/
- source: https://github.com/coreorm/slim3-view-example
Requirement
- php 5.6+
- composer
Install
composer require coreorm/slim3-view
Test
phpunit
Structure
make sure your themes directory are in the following structure:
- Layout: layout files are to be placed in
<theme name>/layouts/
directory. - Note that a default layout is recommended in case a custom layout is not found, it will fall back to the default one.
- Views: views must be under views, but can be in nested directories, when you need to render it, simply use the relative path against views, e.g. if you have
views/foo/bar.phtml
, simply call$theme->render($response, 'foo/bar')
Layout template
Layout template must contain a default variable $mainContent
in the body so sub templates can appear, e.g.
Another way to send rendered content to the page is to use $theme->renderView()
, e.g.
Usage Example (or just open up examples/app/index.php):
-
Use with slim
- Use with other PSR-7 compliant library
APIs
Theme APIs
Theme::instance($templatePath = null, $theme = 'default')
instantiate the theme class: We use a singleton pattern to ensure all shared data are available to each and every template, so simply use:
$theme->setLayout($layout)
switch layout: It's possible to switch layout either from the beginning of the code, or inside the routes at run time.
E.g.
$theme->setTheme($theme)
switch theme: This will set the current theme to the string value of $theme
.
$theme->share($templateRelativePath, $theme)
mark a template from a specific theme as shareable: this will essentially make it a global template to go to when a template with the same name from a different theme is not found.
$templateRelativePath
the relative path to the template$theme
the theme name
E.g.
This will mark the page themes/default/views/page1.phtml
as the shared view for relative path views/page1
, so if say I'm in theme 'new' and I call renderView('page1')
, if themes/new/views/page1.phtml
doesn't exist, it will use themes/default/views/page1.phtml
instead.
$theme->render(ResponseInterface $response, $template, array $data = [], $shouldFallback = false)
render a template plus the layout: This is the final render function you'd want to run in your route/controller, as it renders the give template and assigns it to the $mainContent
partial data which in turn will be displayed in the template main body (or wherever you would like to, by calling ``). So it renders template, then layout, and then output to the browser.
$shouldFallback
- when this is set to true, the system will try to find the fallback template if the given template is not found (see aboveshare
API for more details).
E.g.
$theme->renderView($template, $data = [], $shouldFallback = true, $reuseHTML = false)
render a template and retrieve the content: This renders a template and will return the html.
$reuseHTML
if this is set to true, the given template will render only once, and any future renderView/import calls to this template will reuse the HTML that's rendered from the very first one. Use this for elements that are repeated on the same page with exactly the same HTML source.
E.g.
Template (layout/views) API
$this->import($templateFile, $data = [], $shouldFallback = true, $reuseHTML = false)
import a sub template inside a template This is only available inside the template itself.
In the template code, do:
Data scope
Global (data available to all templates)
$theme->setData($k, $v)
sets data that is available for all templates/layouts;
Private (partial data that will override global data, when used for rendering individual views)
passing partial data to the render/import function will set data that is private to the given template only.
e.g. $theme->render($response, 'page', ['foo' => 'bar'])
will set the $foo value to 'bar' only for the page
template.