mako\View


Description


View/template class.


Class methods


Toggle source

public __construct($view, $variables = array ( ))


Create a new view object.


Parameters

Type Description
string Name of the view file
array (optional) Array of view variables
Return value

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;
}

Toggle source

public static factory($view, $variables = array ( ))


Factory method making method chaining possible right off the bat.


Parameters

Type Description
string Name of the view file
array (optional) Array of view variables
Return value

mako\View

public static function factory($view, array $variables = array())
{
	return new static($view, $variables);
}

Toggle source

public assign($key, $value, $global = false)


Assign a view variable.


Parameters

Type Description
string Variable name
mixed View variable
boolean (optional) True to make variable available in all views
Return value

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;
}

Toggle source

public assignByRef($key, &$value, $global = false)


Assign a view variable by reference.


Parameters

Type Description
string Variable name
mixed View variable
boolean (optional) True to make variable available in all views
Return value

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;
}

Toggle source

public render($filter = NULL)


Include the view file and extracts the view variables before returning the generated output.


Parameters

Type Description
callback (optional) Callback function used to filter output
Return value

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;
}

Toggle source

public __set($key, $value)


Magic setter method that assigns a view variable.


Parameters

Type Description
string Variable name
mixed View variable
Return value

NULL

public function __set($key, $value)
{
	$this->vars[$key] = $value;
}

Toggle source

public __get($key)


Magic getter method that returns a view variable.


Parameters

Type Description
string Variable name
Return value

mixed

public function __get($key)
{
	if(isset($this->vars[$key]))
	{
		return $this->vars[$key];
	}
}

Toggle source

public __isset($key)


Magic isset method that checks if a view variable is set.


Parameters

Type Description
string Variable name
Return value

boolean

public function __isset($key)
{
	return isset($this->vars[$key]);
}

Toggle source

public __unset($key)


Magic unset method that unsets a view variable.


Parameters

Type Description
string Variable name
Return value

NULL

public function __unset($key)
{
	unset($this->vars[$key]);
}

Toggle source

public __toString()


Method that magically converts the view object into a string.

Return value

string

public function __toString()
{
	try
	{
		return $this->render();
	}
	catch(Exception $e)
	{
		Mako::exceptionHandler($e);
	}
}