phpbook/view

1.0.0 2018-07-20 11:09 UTC

This package is auto-updated.

Last update: 2024-05-10 23:51:35 UTC


README

About View

  • A lightweight view PHP library

Composer Install

composer require phpbook/view

Declare Configurations

<?php

/********************************************
 * 
 *  Declare Configurations
 * 
 * ******************************************/

//Path root to views

\PHPBook\View\Configuration\View::setViewsPathRoot('main', 'path\to\views\base\dir');

\PHPBook\View\Configuration\View::setViewsPathRoot('anotherAlias', 'path\to\another\views\base\dir');

\PHPBook\View\Configuration\View::setDefaultPathRoot('main');

?>

Declare Views

View One (one.php)
		
	My View Example One

	<?php echo $title; ?>

	<?php echo $jhon->name; ?>

	<?php foreach($friends as $friend): ?>

		<?php echo $friend->name; ?>

	<?php endforeach; ?>
		
View Two (two.php)
	My View Example Two using another view inside

	<?php  
		echo (new \PHPBook\View\View)
			->setPathRoot('anotherAlias')
			->setView('subpath/to/file/view')
			->render();
	?>
		

Rendering Views

<?php 

/*********************************************
 * 
 *  Rendering Views
 * 
 * *******************************************/

$jhon = new StdClass;
$jhon->name = 'Jhon';
$jhon->age = 15;

$title = $jhon->name;

$ana = new StdClass;
$ana->name = 'Ana';
$ana->age = 15;

$paul = new StdClass;
$paul->name = 'Paul';
$paul->age = 16;

$friends = [$ana, $paul];

//data must be an array os values
$content = (new \PHPBook\View\View)
	->setView('subpath/to/file/view/one')
	->setData([
		'title' => $title, 
		'jhon' => $jhon, 
		'friends' => $friends
	])
	->render();

echo $content;
    
?>