The following are a few basic examples of common usage in the Cake Toolkit.
Create a HTML div object and add it to the View.
$myDiv = $this->Html->Div();
$this->add($myDiv);
Create a HTML div object and add a HTML span object as a child.
$myDiv = $this->Html->Div();
$mySpan = $this->Html->Span();
$myDiv->add($mySpan);
Create various objects as children of each other in a sequence.
$myDiv->Span()->Em()->Strong();
IMPORTANT: When chaining objects, the last object in the chain is returned. Also, only objects of the common factory can be called.
Set a unique ID on an object, by default all objects have a unique ID assigned to them.
$myDiv->setId('myId');
Set the "text" configuration parameter on a HTML div object, this will render as text inside the element.
$myDiv = $this->Html->Div(array(
'text' => __('Hello World')
));
$myDiv->text = 'Updated';
A collection of special configuration parameters available on all objects.
$mySpan = $this->Html->Span(array(
'_id' => 'myId',
'_parent' => $myDiv,
'_children' => array(
$myButton,
'Strong' => array(
'text' => __('Hello World')
)
),
'_events' => array(
'click' => $this->Js->Alert(array(
'text' => __('Hello World')
))
)
));
IMPORTANT: The special parameters available are shortcuts to common actions on an object.
Make a copy of an object, the unique ID is automatically updated.
$myDivCopy = $myDiv->copy();
Add multiple objects as children.
$myDiv->addMany(array(
$mySpan,
'Button',
'Strong' => array(
'text' => __('Hello World')
)
));
Add objects before or after a specific child.
$myDiv->addBefore($newSpan, $mySpan);
$myDiv->addAfter($newSpan, $mySpan);
Add an object based upon a condition, which will only be added if the condition resolves to true.
$myDiv->addIf($condition === true, $mySpan);
Add an object from a callback function, any "callable" function in PHP is valid.
$myDiv->addWhile(function(&$parent, &$view, &$data, $i) {
if ($i <= 9) {
$parent->add($view->Html->Span(array(
'text' => 'I was added'
)));
}
});
Inherit the children from another object, these are not duplicated but moved to the new parent.
$myDiv->addFrom($otherDiv);
Replace or remove children.
$myDiv->replaceChild($mySpan, $otherSpan);
$myDiv->removeChild($otherSpan);
Various methods for traversing parents and children.
$parent = $myUl->getParent();
$children = $myUl->getChildren();
$firstChild = $myUl->getFirst();
$lastChild = $myUl->getLast();
$previousChild = $lastChild->getPrevious();
$nextChild = $firstChild->getNext();
$myUl->each(function(&$child, &$parent, &$view, &$data, $i) {
$child->text = 'Updated';
});
Bind a JavaScript alert event to an object.
$myButton->bind('click', $this->Js->Alert(array(
'text' => __('Hello World')
)));
Bind a JavaScript redirection event to an object.
$myButton->bind('click', $this->Js->Redirect(array(
'location' => Router::url(array(
'controller' => 'Example',
'action' => 'index'
))
)));
Load the content of an object via Ajax.
$myButton->bind('click', $this->Js->Element(array(
'node' => $myDiv
))->ajax(Router::url(array(
'controller' => 'Example',
'action' => 'index'
))));
Include a CakePHP element in a CTK based View.
$myDiv = $this->Html->Div();
$exampleElement = $this->element('example');
$myDiv->add($exampleElement);
IMPORTANT: The content of elements are returned as an object. This allows you to add elements to other objects, however, elements do not accept children.
Append a HTML script to the "script" view block.
$this->append('script', $this->Html->Script(array(
'src' => 'example.js'
)));
Use an example helper in a CTK based View.
$myDiv = $this->Html->Div();
$something = $this->ExampleHelper->something();
$myDiv->add($something);
IMPORTANT: The output of a helper method is returned as an object. Objects returned from helpers are automatically converted to their original value if passed to other helpers.