Created
March 14, 2010 12:52
-
-
Save Burgestrand/331948 to your computer and use it in GitHub Desktop.
An example of a View pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Det här är contents. | |
Kan mycket väl innehålla PHP-kod: | |
<?php | |
for ($i = 0; $i < 10; ++$i) | |
{ | |
printf('%d ', $i); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'View.php'; | |
// Den här koden finns i din controller | |
$template = View::factory('views/template.php'); // finns i din Controller_Template | |
// finns i Controller_Home | |
$template->data = 'Det här är data'; | |
$template->contents = View::factory('views/contents.php'); | |
echo $template; | |
/* End of file test.php */ | |
/* Location: ./test.php */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Det här är vår template</h1> | |
<p> | |
<?php | |
echo "Hello, world!"; | |
?> | |
</p> | |
<p> | |
Innehåll i “$contents”: | |
</p> | |
<pre><?php var_dump($contents) ?></pre> | |
<p> | |
Innehåll i “data”: | |
</p> | |
<pre><?php var_dump($data) ?></pre> | |
<p> | |
Output av “contents”: | |
</p> | |
<pre><?php echo htmlspecialchars($contents, ENT_QUOTES) ?></pre> | |
<p> | |
Output av “data”: | |
</p> | |
<pre><?php echo htmlspecialchars($data, ENT_QUOTES) ?></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class View | |
{ | |
private $_file; | |
private $_locals = array(); | |
/** | |
* Skapa en ny View med rätt fil och lokal data | |
* | |
* @param string sökväg till fil | |
* @param array variabler i View | |
*/ | |
public function __construct($file, array $data) | |
{ | |
$this->_file = $file; | |
$this->_locals = $data + $this->_locals; | |
} | |
/** | |
* Skapa en ny View och returnera den | |
* | |
* @see View::__construct | |
* @return View | |
*/ | |
public static function factory($file, array $data = array()) | |
{ | |
return new View($file, $data); | |
} | |
/** | |
* Fånga output av en View | |
* | |
* Not: variabelnamnen i argumenten är lite konstiga för att undvika | |
* variabelnamnskollisioner | |
* | |
* @param string sökväg till fil | |
* @param array View-variabler | |
* @return string | |
*/ | |
protected static function capture($view_capture_file, array $view_capture_data) | |
{ | |
// Extrahera lokala variabler | |
extract($view_capture_data, EXTR_SKIP); | |
// Fånga output | |
ob_start(); | |
include $view_capture_file; | |
return ob_get_clean(); | |
} | |
/** | |
* Rendera vår View och returnera resultatet | |
* | |
* @return string | |
*/ | |
public function render() | |
{ | |
return View::capture($this->_file, $this->_locals); | |
} | |
/** | |
* Sätt en variabel i vår View | |
* | |
* @param string variabelnamn | |
* @param mixed variabelvärde | |
* @return View | |
*/ | |
public function set($var, $value) | |
{ | |
$this->_locals[$var] = $value; | |
return $this; | |
} | |
// Magiska metoder: | |
public function __toString() | |
{ | |
return $this->render(); | |
} | |
public function __set($var, $name) | |
{ | |
$this->set($var, $name); | |
} | |
public function &__get($var) | |
{ | |
return $this->_locals[$var]; | |
} | |
public function __isset($var) | |
{ | |
return isset($this->_locals[$var]); | |
} | |
} | |
/* End of file View.php */ | |
/* Location: ./View.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment