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.

2.0.2 2023-05-11 09:37 UTC

This package is auto-updated.

Last update: 2024-10-11 12:27:20 UTC


README

Miniview Logo 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.

Latest Stable Version License Scrutinizer Code Quality Code Coverage

Quick Install

composer require maslosoft/miniview

Documentation

Full 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