diff --git a/Tests/layout.html b/Tests/layout.html new file mode 100644 index 0000000..48fba3a --- /dev/null +++ b/Tests/layout.html @@ -0,0 +1,7 @@ + +
+ Text + + + + diff --git a/Tests/outputhtmlTest.php b/Tests/outputhtmlTest.php new file mode 100644 index 0000000..76d1583 --- /dev/null +++ b/Tests/outputhtmlTest.php @@ -0,0 +1,56 @@ + */ + +/** Test the outputhtml.php file */ +class test_outputhtml extends PHPUnit_Framework_TestCase +{ + /** Entry null */ + public function testlayout1 () + { + $this->expectOutputString(""); + $output = new outputhtml (); + print ($output->out ("")); + } + + public function testlayout2 () + { + $this->expectOutputString("data"); + $output = new outputhtml (); + print ($output->out ("data")); + } + + public function testlayout3 () + { + $this->expectOutputString("data"); + $output = new outputhtml (); + print ($output->out ("data", "title")); + } + + public function testlayout4 () + { + $this->expectOutputString(" + + Text + + +\n"); + $output = new outputhtml (); + print ($output->out ("data", "title", FALSE, FALSE, "Tests/layout.html" )); + } + + public function testlayout5 () + { + $this->expectOutputString(" + + Text + VARIABLE + +\n"); + $output = new outputhtml (); + $variable = array ("var"=>"VARIABLE"); + print ($output->out ("data", "title", FALSE, FALSE, "Tests/layout.html", + null, $variable)); + } +} diff --git a/outputhtml.php b/outputhtml.php index e04d313..02842fc 100644 --- a/outputhtml.php +++ b/outputhtml.php @@ -17,8 +17,8 @@ class outputhtml extends output @param string|null $viewMethod Method in the class in views @param string|null $layout Layout file in views @param array|null $replacement Replace the {key}=>value - @param array|null $variable PHP variables send to the view (can be - processed by foreach, if...) */ + @param array|null $variable PHP variables send to the view and to layout + (can be processed by foreach, if...) */ public function out ($data, $title = FALSE, $viewClass = FALSE, $viewMethod = FALSE, $layout = FALSE, $replacement = array(), @@ -40,7 +40,9 @@ class outputhtml extends output if ($layout !== FALSE) { - $layoutPage = file_get_contents ("views/$layout.html"); + if (! file_exists ($layout) && file_exists ("views/$layout.html")) + $layout = "views/$layout.html"; + $layoutPage = $this->layoutVariables ($layout, $variable); $resView = str_replace ("{content}", $resView, $layoutPage); } @@ -53,4 +55,20 @@ class outputhtml extends output return $resView; } + + /** Get the layout and provide it the variables. The variables will be push in + global to the layout (they can be used like $XX) + @param string the layout file to load + @param array $variables The variables array to push to the layout + @return string the Layout with variables interpreted */ + private function layoutVariables ($layout, $variables) + { + if (! file_exists ($layout)) + throw new Exception (sprintf (_("Layout File '%s' does not exists"), + $layout), 404); + extract ($variables); + ob_start(); + require ($layout); + return ob_get_clean (); + } }