Adding

Adding is a most powerful concept in Agile Toolkit. Here is the simple form of adding:

$new_object = $parent -> add('ClassName');

There are some rules:

Arguments for add()

All arguments for add() except the first are optional. When Controllers, Models and Helpers are added, only first argument is used. Views can use 4 arguments.

$new_view_object = $parent -> add($class, $short_name, $spot, $template );

$parent — must be an existing object. You can only add View into View, but you can add Controller and Model anywhere.

$class — name of the class. There is a convention on naming classes. 'Form_UserDetails' would be example class name.

$short_name — overrides generation of short_name. Always use "null".

$spot — defines where rendered version of Views template will be inserted. You can add multiple objects into same spot. Existing contents of the spot will be erased when object is being rendered. Default value is 'Content'.

$template — overrides View's default template. If array is used, then first argument is file name of template (such as "view/banner"). Second argument is optional and defines which region to use within template. If not specified "_top" is used, which means all contents of template file will be used. If $template is not array, but a string, then it specifies a region in parent's template which will be cloned and used by the view

init() method

Anytime when you add anything, Agile Toolkit creates an object, links it with api and parent by setting $obj->owner and $obj->api properties. For views it also initialize template. When it's all done, init() method is called.

This is true for any object in Agile Toolkit, therefore you should never use object constructors but should use init() method instead. There are several important notes:

class Form_ContactForm extends Form { function init(){ parent::init(); $this->add('Disclaimer'); $this->addField('line','name'); $this->addField('line','surname'); $this->addSubmit('Send'); } }

Other forms of adding

There are large amount of wrappers for add() functions. For example when you add field to a form, you would use addField('line','name'). This function is a wrapper for add(), however it transforms type into class as well as doing few other things. Wrappers for add() start with "add" in most cases. However there are some functions which do not use this convention such as js(), exception(), $grid->addColumn() (does not create new object). In your code, always try to follow the convention and call function 'addSomething' if it adds new object and returns new object.

Other Examples

$admin=$this->add('Model_User')->addCondition('admin',true);

Adds model to current object. add() returns instance of a new object making it possible for you to chain-call methods of a newly added object.

$menu=$this->api->add('Menu',null,'Menu');

Adds menu inside api, and use spot 'Menu' inside api's template for output.

$view=$page->add('View',null,'BusinessInfo','BusinessInfo'); $view->template->set($data);

The region <?BusinessInfo?> inside page's template will be replaced by a view. The template chunk however will be cloned by the view, so just adding it like this will have no effect on resulting output. Second line however will place contents of associative array into template of a view.

Working with existing object

$this->hasElement($short_name) return object of child if found or false if not found.

$this->getElement($short_name) similar to hasElement() but will produce exception if not found. Use this when chaining.

$this->destroy() remove current object from parent. For view this will remove it from rendered page.

Properties: $this->name name unique on the system. $this->short_name is a short name. $this->template reference to SMlite template for current view. $this->owner reference to object whose add() was used(parent). $this->api refers to Api class of a parent object.

Continue to "Adding Objects"