Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lib/lib.php
- <?php
- class lib {
- protected function root() {
- return dirname(__FILE__);
- }
- protected function render($file_name, $variables_array = null) {
- if($variables_array)
- extract($variables_array);
- require($this->root() . '/../views/' . $file_name . '.php');
- }
- }
- // index.php
- <?php
- require('lib/lib.php');
- class Application extends lib {
- public function run() {
- $var = '1st variable to use in a template';
- $var2 = '2nd variable to use in a template';
- $this->render('example', compact('var', 'var2'));
- }
- }
- $app = new Application();
- $app->run();
- // views/example.php
- //some html here
- <?php echo $var; echo $var2 ?>
- //html...
- // lib/lib.php
- <?php
- class lib {
- public $variables_array = array();
- protected function root() {
- return dirname(__FILE__);
- }
- protected function render($file_name) {
- if(isset($this->variables_array)) {
- extract($this->variables_array); // Include external variables to use in a template
- }
- require_once($this->root() . "/../views/$file_name.php");
- }
- }
- ?>
- include('lib/lib.php');
- class Application extends lib {
- public function run() {
- return $this->render('example');
- }
- }
- $app = new Application();
- $app->variables_array=array('var'->'var text 1', 'var2'=>'var text 2');
- $app->run();
- ?>
- // views/example.php
- //some html here
- <?php echo $var; echo $var2 ?>
- //html...
Add Comment
Please, Sign In to add comment