composer require maslosoft/miniview
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
.
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.
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();
Go to examples folder and type php run.php