maslosoft / miniview
Mini view is minimal template rendering library with pluggable template engines. Out of the box it support plain PHP templates, Latte and Twig.
Installs: 4 181
Dependents: 5
Suggesters: 1
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- maslosoft/embedi: ^2
Requires (Dev)
Suggests
- latte/latte: To use tatte templates
- twig/twig: To use twig templates
README
Maslosoft Miniview
Quick Install
composer require maslosoft/miniview
Documentation
Minimal PHP view
PHP itself is a kind of templating language. Some other
supplemental languages have been implemented too. To
use each one of them, we need to know how to use it,
locate file exacly, and use some low-level commands like require
.
Common interface
This view library provides common interface for PHP, or other templating engines, with option to extend it. Main power of miniview, is that it requires minimum code and effort to use MVC like views. It will also locate file relativelly to currently used class. It is safe to use with any functions directly outputting text, as it will capture any output and allow it to be passed anywhere, or just be displayed.
Usage
This is some example widget using MiniView:
namespace Company\SomeNamespace; use Maslosoft\MiniView\MiniView; class MyWidget { /** * View renderer * @var MiniView */ public $view = null; /** * @var string */ public $version = ''; public function __construct() { $this->view = new MiniView($this); $this->version = $this->view->getVersion(); } public function show() { return $this->view->render('myView', ['user' => 'Joe'], true); } public function greet($name) { return "Nice to meet you $name!" . PHP_EOL; } }
In view file, all widget public properties as well as methods are available using $this
.
View file is located in folder views
located in same folder as widget class.
Example view file:
Hello <?= $user ?>! <?= $this->greet($user);?> The version is <?= $this->version;?>
Calling show()
will return rendered view file located in classFolder/views/myView.php
with variable $user
with value Joe
.
use Company\SomeNamespace\MyWidget; require __DIR__ . '/../src/Miniview.php'; require __DIR__ . '/MyWidget.phps'; $widget = new MyWidget; echo $widget->show();
Run example
Go to examples folder and type php run.php