SMMP/build/generic/core/i_view.php

90 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace generic\core;
abstract class i_view{
/* (1) Attributes
---------------------------------------------------------*/
/* (1) Will contain patches according to warehouse's modules */
public $patch = [];
/* (2) Add a patch to the core view
*
* @patch_name<String> Patch name
* @patch_inst<i_patch> Patch instance
*
---------------------------------------------------------*/
public function patch(String $patch_name, i_patch $patch_inst){
$this->patch[$patch_name] = $patch_inst;
}
/* (3) Returns the auto patch code
*
*
---------------------------------------------------------*/
private function autopatch(){
/* (1) Pre-code */
$code = "{% extends 'main.twig' %}\n\n";
/* (2) For each module patch */
foreach($this->patch as $name=>$inst)
/* (3) For each block patch */
foreach($inst->patch as $block=>$temp)
$code .= "{% block $block %}{% include '$temp' %}{% endblock %}\n";
/* (4) Return code */
return $code;
}
/* (4) Renders the view
*
---------------------------------------------------------*/
public function render(){
/* (1) Get path information (core class)
---------------------------------------------------------*/
/* (1) Get core class */
$core_class = str_replace('\\', '/', get_class($this));
/* (2) Extract root DIR */
$root_path = __BUILD__.'/generic/'.dirname($core_class);
/* (3) Extract file name */
$model_path = basename($core_class).'.twig';
/* (2) Get patches
---------------------------------------------------------*/
$auto_patch = [ 'render.twig' => $this->autopatch() ];
/* (2) Setup
---------------------------------------------------------*/
/* (1) Initialize twig */
$loader = new \Twig_Loader_Chain([
new \Twig_Loader_Filesystem($root_path), // default directory templates
new \Twig_Loader_Array($auto_patch) // custom auto-patch system
]);
$twig = new \Twig_Environment($loader, [
'debug' => true,
'cache' => false,
'auto_reload' => true
]);
/* (3) Build the view and return it back
---------------------------------------------------------*/
/* (1) Render and return the view */
return $twig->render('render.twig', [ 'core' => $this, 'patch' => $this->patch ]);
}
}