View/template class.
Create a new view object.
| Type | Description |
|---|---|
| string | Name of the view file |
| array | (optional) Array of view variables |
NULL
public function __construct($view, array $variables = array())
{
// Check if view file exists
if(file_exists($file = Mako::path('views', $view)))
{
$this->viewFile = $file;
}
// No view, check if template exists
elseif(file_exists($file = Mako::path('views', $view . '.tpl')))
{
$this->viewFile = Compiler::compile($file);
}
// No view or template. Throw exception
else
{
throw new RuntimeException(vsprintf("%s(): The '%s' view does not exist.", array(__METHOD__, $view)));
}
// Assign view variables
$this->vars = $variables;
}
Factory method making method chaining possible right off the bat.
| Type | Description |
|---|---|
| string | Name of the view file |
| array | (optional) Array of view variables |
mako\View
public static function factory($view, array $variables = array())
{
return new static($view, $variables);
}
Assign a view variable.
| Type | Description |
|---|---|
| string | Variable name |
| mixed | View variable |
| boolean | (optional) True to make variable available in all views |
mako\View
public function assign($key, $value, $global = false)
{
if($global === false)
{
$this->vars[$key] = $value;
}
else
{
static::$globalVars[$key] = $value; // Available to all views
}
return $this;
}
Assign a view variable by reference.
| Type | Description |
|---|---|
| string | Variable name |
| mixed | View variable |
| boolean | (optional) True to make variable available in all views |
mako\View
public function assignByRef($key, &$value, $global = false)
{
if($global === false)
{
$this->vars[$key] =& $value;
}
else
{
static::$globalVars[$key] =& $value; // Available to all views
}
return $this;
}
Include the view file and extracts the view variables before returning the generated output.
| Type | Description |
|---|---|
| callback | (optional) Callback function used to filter output |
string
public function render($filter = null)
{
if(empty($this->output))
{
extract(array_merge($this->vars, static::$globalVars), EXTR_REFS); // Extract variables as references
ob_start();
include($this->viewFile);
$this->output = ob_get_clean();
}
if($filter !== null)
{
$this->output = call_user_func($filter, $this->output);
}
return $this->output;
}
Magic setter method that assigns a view variable.
| Type | Description |
|---|---|
| string | Variable name |
| mixed | View variable |
NULL
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
Magic getter method that returns a view variable.
| Type | Description |
|---|---|
| string | Variable name |
mixed
public function __get($key)
{
if(isset($this->vars[$key]))
{
return $this->vars[$key];
}
}
Magic isset method that checks if a view variable is set.
| Type | Description |
|---|---|
| string | Variable name |
boolean
public function __isset($key)
{
return isset($this->vars[$key]);
}
Magic unset method that unsets a view variable.
| Type | Description |
|---|---|
| string | Variable name |
NULL
public function __unset($key)
{
unset($this->vars[$key]);
}
Method that magically converts the view object into a string.
string
public function __toString()
{
try
{
return $this->render();
}
catch(Exception $e)
{
Mako::exceptionHandler($e);
}
}