Table of Contents

assign

API Package Subpackage Class Method Reference Last reviewed Doc status
API Home Package Joomla.Framework Subpackage Application Class JView Method assign Reference assign() Never Work in Progress

Assigns variables to the view.

The first parameter can be either an object, an associative array, or a string. If it is an object, the object’s properties will be assigned to the view object. If it is an associative array, the array’s data will be assigned to the object. If it is a string, the second parameter will be assigned to the property with the name of the first parameter.

The method will not permit data to be assigned to properties that begin with an underscore because these are either private properties for JView or private variables within the template script itself.

This method is generally used to push data into the template. In the view class, variables are assigned to the template, and then that data is retrieved using $this→{variable_name} inside the template.

This method returns true if the data was successfully assigned, or false otherwise.

Syntax

boolean assign ( $var1, $var2 )

$var1 mixed is either an object, an associative array or a string. See above for more information.
$var2 mixed is the value to assign to the specified property if $var1 is a string.

Examples

Example - Assign by Name and Value

$view->assign('var1', 'something');
$view->assign('var2', 'else');
echo $view->var1;
echo $view->var2;

and

Example - Assign by Associative Array

$ary = array('var1' => 'something', 'var2' => 'else');
$view->assign($obj);
echo $view->var1;
echo $view->var2;

and

Example - Assign by Object

$obj = new stdClass;
$obj->var1 = 'something';
$obj->var2 = 'else';
$view->assign($obj);
echo $view->var1;
echo $view->var2;

will all produce:

Result

somethingelse

SCUSSION~~