hcportal-dev/Aiko/Framework/Template.php

108 lines
3.0 KiB
PHP

<?php
// class ini berfungsi untuk menentukan view nya yang digunakan
namespace Aiko\Template;
Class Template {
private $registry; // variable ini berfungsi untuk menampung object registry
private $vars = array(); // variable ini berfungsi untuk menyimpan variable variable yang digunakan
// oleh templatenya
function __construct($registry) {
$this->registry = $registry; // set registry object
}
public function __set($index, $value) // magic method yang berfungsi untuk set variable untuk template saja
{
$this->vars[$index] = $value;
}
// ini method yang berfungsi untuk menampilkan view
function show($name,$listJS= array(),$listCSS= array()) {
// variable path berfungsi menyimpan path file view
$path = __SITE_PATH .'/src/modules/'.$this->registry->ContPath. '/views' . '/' . $name . '.php';
$pathJS= __SITE_PATH .'/src/modules/'.$this->registry->ContPath. '/js' . '/' . $name . '.js';
$srcjs= __SERVERADDR.'/src/modules'.$this->registry->ContPath. '/js' . '/' . $name . '.js';
$pathCSS= __SITE_PATH .'/src/modules/'.$this->registry->ContPath. '/css' . '/' . $name . '.css';
$srccss= __SERVERADDR.'/src/modules'.$this->registry->ContPath. '/css' . '/' . $name . '.css';
if (file_exists($path) == false)
{
throw new \Exception('Template not found in '. $path);
return false;
}
// Load variables, jadikan index array sebagai variable pada php
foreach ($this->vars as $key => $value)
{
//set variable php
$$key = $value;
}
if(sizeof($listCSS)>0)
{
foreach ($listCSS as $val) {
echo "<link href=\"$val\" rel=\"stylesheet\" type=\"text/css\" />";
}
}
// include file
if (file_exists($pathCSS) == true)
{
echo "<link href=\"$srccss\" rel=\"stylesheet\" type=\"text/css\" />";
}
include ($path); // load view
if (file_exists($pathJS) == true)
{
echo "<script type='text/javascript' src='$srcjs'></script>";
}
if(sizeof($listJS)>0)
{
foreach ($listJS as $val) {
echo "<script type='text/javascript' src='$val'></script>";
}
}
}
/**
* method ini digunakan untuk menampilkan data dalam PDF
* require dompdf
*/
public function getContentFile($name)
{
$path = __SITE_PATH .'/src/modules/'.$this->registry->ContPath. '/pdf' . '/' . $name . '.php';
if (file_exists($path) == false)
{
throw new \Exception('Template not found in '. $path);
return false;
}
// Load variables, jadikan index array sebagai variable pada php
foreach ($this->vars as $key => $value)
{
//set variable php
$$key = $value;
}
$obstart=ob_start();
if ($obstart == false)
{
throw new \Exception('output bueffering not start ');
return false;
}
include ($path); // load view
$out = ob_get_clean();
return $out;
}
}
?>