Guest User

Untitled

a guest
Apr 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. // lib/lib.php
  2. <?php
  3.  
  4. class lib {
  5.  
  6. protected function root() {
  7. return dirname(__FILE__);
  8. }
  9.  
  10.  
  11. protected function render($file_name, $variables_array = null) {
  12.  
  13. if($variables_array)
  14. extract($variables_array);
  15.  
  16. require($this->root() . '/../views/' . $file_name . '.php');
  17. }
  18. }
  19.  
  20. // index.php
  21. <?php
  22.  
  23. require('lib/lib.php');
  24.  
  25.  
  26. class Application extends lib {
  27.  
  28. public function run() {
  29.  
  30. $var = '1st variable to use in a template';
  31. $var2 = '2nd variable to use in a template';
  32.  
  33. $this->render('example', compact('var', 'var2'));
  34. }
  35.  
  36. }
  37.  
  38. $app = new Application();
  39.  
  40. $app->run();
  41.  
  42. // views/example.php
  43. //some html here
  44. <?php echo $var; echo $var2 ?>
  45. //html...
  46.  
  47. // lib/lib.php
  48. <?php
  49.  
  50. class lib {
  51.  
  52. public $variables_array = array();
  53.  
  54. protected function root() {
  55. return dirname(__FILE__);
  56. }
  57.  
  58. protected function render($file_name) {
  59. if(isset($this->variables_array)) {
  60. extract($this->variables_array); // Include external variables to use in a template
  61. }
  62.  
  63. require_once($this->root() . "/../views/$file_name.php");
  64. }
  65. }
  66.  
  67. ?>
  68.  
  69. include('lib/lib.php');
  70.  
  71.  
  72. class Application extends lib {
  73.  
  74. public function run() {
  75. return $this->render('example');
  76. }
  77.  
  78. }
  79.  
  80. $app = new Application();
  81. $app->variables_array=array('var'->'var text 1', 'var2'=>'var text 2');
  82. $app->run();
  83.  
  84. ?>
  85.  
  86. // views/example.php
  87. //some html here
  88. <?php echo $var; echo $var2 ?>
  89. //html...
Add Comment
Please, Sign In to add comment