Functions in the Ajax Helper (18 of them) - use Prototype JavaScript Framework
Function Description
===== ==========================
afterRender() Includes code blocks after a view has been rendered
autoComplete() Creates an autocomplete text field
div() Creates a
element for Ajax updates
divEnd() Closes an Ajaxed
element
drag() Creates a draggable element; requires the Scriptaculous animation libraries
drop() Creates a droppable element; requires Scriptaculous
dropRemote() Creates an area that triggers an Ajax response when a draggable is dropped
onto it
editor() Creates an editor control that is swapped for the element when triggered by
the user; requires Scriptaculous
form() Creates a form element that runs in the background when submitted
isAjax() Returns true if the current request is a Prototype update
link() Creates a link to run an Ajax call
observeField() Triggers an Ajax response when the observed field is changed
observeForm() Observes a form and triggers an Ajax response when changed
remoteFunction() Creates Ajax functions to be run, usually in conjunction with a link() event
remoteTimer() Triggers an Ajax response at a specified time interval
slider() Creates a slider control; requires Scriptaculous
sortable() Makes lists or other objects sortable; requires Scriptaculous
submit() Renders a form submit button that triggers an Ajax form submission
52
HTML helper functions - charset
charset( charset[string] )
charset = null: The character set to be used in the tag
For example, the following:
=$html->charset('UTF-16');?>
will output this:
=> The default character set can be specified in the
app/config/core.php file. Change the
App.encoding setting on or near line 47 following the standard
HTML character set specifications.
(For your information, a list of registered character set values is
available from the World
Wide Web Consortium at www.w3.org/International/O-charset-lang.html.)
The App.encoding value is originally set to UTF-8.
53
HTML helper functions - css
css( path[mixed], rel[string], attributes[array], inline[bool] )
=$html->css('cake.generic.print',null,array('media'=>'print'));?>
or like so:
=$html->css('cake.generic.screen',null,array('media'=>'screen'));?>
Multiple style sheets can be called by passing an array in the path parameter:
=$html->css(array('reset','type','layout'));?>
54
HTML helper functions - div
div( class[string], text[string], attributes[array], escape[bool] )
=$html->div('tab','Home');?>
to output the following:
Home
=$html->div('story',$story['Story']['contents']);?>
uses a few less characters than does this:
=$story['Story']['contents'];?>
55
HTML helper functions - docType
docType( type[string] )
=$html->docType('xhtml-strict');?>
the document type declaration is produced as the following string:
56
HTML helper functions - image
image( path[string], attributes[array] )
=$html->image('title.jpg',array('alt'=>'My Homepage','class'=>'title');?>
outputs the following:
57
HTML helper functions - meta
meta( type[string], url[mixed], attributes[array], inline[bool] )
$html->meta() outputs tags, which can be used for
specifying a site description or other tags. It can also
output tags for RSS feeds and a site favicon.
=$html->meta('icon');?>
returns these strings:
58
HTML helper functions - nestedList
nestedList( list[array], attributes[array], itemAttributes[array], tag[string] )
$posts = array(
'Post 1'=>array(
'Jan 1, 2008',
'No Author'),
'Post 2'=>array(
'Jan 2, 2008',
'Administrator')
);
By placing this array in the list parameter of the $html->nestedList() function, the following
HTML would be returned:
Post 1
Jan 1, 2008
No Author
Post 2
Jan 2, 2008
Administrator
59
HTML helper functions - para
para
Simply put, this is a convenience function for wrapping the
paragraph tag around a chunk of
text. It can be useful in simplifying HTML escaping for content that
may contain nonalphanumeric
characters.
para( class[string], text[string], attributes[array], escape[bool] )
=$html->para(null,'This is a test for the $html->para() function',null,true);?>
will return the following when run through the $html->para() function:
This is a test for the $html->para() function
60
HTML helper functions - style
style( data[array], inline[bool] )
$styles = array(
'p_bold'=>array(
'font-size'=>'1.0 em',
'font-weight'=>'bold'
)
'p_italic'=>array(
'font-style'=>'italic'
)
);
=$html->para(null,'Paragraph Text',array('style'=>$html->styles($styleså
['p_bold'])));?>
This line would return the following HTML:
Paragraph Text
61
HTML helper functions - tableHeaders
tableHeaders( names[array], trAttributes[array], thAttributes[array] )
=$html->tableHeaders(array('1','2'),array('class'=>'row'),array('class'=>Ã¥
'header'));?>
will return the following:
1
2
62
HTML helper functions - tableCells
tableCells( data[array], oddTrAttributes[array], evenTrAttributes[array], useCount[bool] )
$html->tableCells() function:
$cells = array(
'row1'=>array(
'column1','column2','column3'
),
'row2'=>array(
'column1','column2','column3'
)
);
The actual text contained in the row keys (for example, row1 and row2) will not be displayed
in the HTML output:
column1
column2
column3
column1
column2
column3
63
HTML helper functions - addCrumb and getCrumbs
$html->addCrumb() and $html->getCrumbs functions render breadcrumbs
(for example, home->about->mailing address).
addCrumb( name[string], link[mixed], attributes[array] )
getCrumbs( separator[string], startText[string] )
64
form helpers - submit
=$form->submit('Submit',array('onSubmit'=>'return false;'));?>
will return a submit button containing the onSubmit attribute, like so:
65
form helpers - end
end
However, the $form->end() function can conveniently combine
into one string the submit button, which is typically the last form
element displayed and the closing tag.
end( options[mixed] )
=$form->end(array('label'=>'Submit Form','id'=>'submit_btn'));?>
This line will return the following output:
66
form helpers - secure
To prevent cross-site request forgery (CSRF) attacks, many
developers use the hash insertion technique. In short, the
$form->secure() function facilitates hash insertions by generating
a hidden form field containing a hash based on other fields in the
form.
secure( fields[array] )
=$form->secure(array('Post'=>array('id','name'));?>
This will output the hidden input element with a server-side-
generated hash:
The value of the hidden input element will certainly change
depending on the Security.salt value set in the app/config/core.php
file and the function’s own randomization algorithm. This hash is
accessible in the $this->data array under the ['_Token']['fields] key.
Not only can the Ajax helper simplify using Prototype, but it can
also make animation effects easier to use.
70
The JavaScript Helper
This helper is used mainly to simplify coding JavaScript.
To include this helper in the application, use this in controller (or app_controller globally):
var $helpers = array('Javascript');
Functions in the JavaScript Helper
Function Description
========= ===========================
$javascript->afterRender() Callback for after rendering; writes
cached events to the view or a temp file
$javascript->blockEnd() Ends a block of JavaScript code
$javascript->cacheEvents() Caches JavaScript events created with the event() function
$javascript->codeBlock() Wraps JavaScript code with the