This is a class I created to simplify rendering view files independently of any business logic. This is just a wrapper for a simple include. Nothing too impressive, but gets the job done.
Read the API documentation.
View on github emyu10/php-view
Using composer
composer require emyu10/php-view
I am no expert in security. So I don't know if this implements the best security practice out there. Check the code for yourself.
include_once 'vendor/autoload.php'; use \emyu10\PhpView\Renderer; use \emyu10\PhpView\FileNotFoundException; $renderer = new emyu10\PhpView\Renderer(); $renderer->setPath('/var/www/html/views'); $renderer->setFile('table'); $renderer->setData(['title' => 'My Awesome Webpage']); try { $renderer->render(); } catch (FileNotFoundException $e) { echo $e->getMessage(); }
$renderer = new emyu10\PhpView\Renderer(); $renderer->initialize('/var/www/html/views', 'table', ['title' => 'My Awesome Webpage'], false); try { $renderer->render(); } catch (FileNotFoundException $e) { echo $e->getMessage(); }
$renderer = new emyu10\PhpView\Renderer(); $renderer->initialize('/var/www/html/views', 'table', ['title' => 'My Awesome Webpage'], true); try { $theHtml = $renderer->render(); //returns the content of the file as a string and assigns to $theHtml variable. echo $theHtml; } catch (FileNotFoundException $e) { echo $e->getMessage(); }
try { Renderer::staticRender('/var/www/html/views/', 'profile', ['name' => 'John Wick', 'dob' => '02/03/1978'], false); } catch (FileNotFoundException $e) { echo $e->getMessage(); }