Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at docs.zendframework.com
ZendX_JQuery View Helpers - ZendX_JQuery
Zend Framework provides jQuery related View Helpers through its Extras Library. These can be enabled in two ways, adding jQuery to the view helper path:
Or using the ZendX_JQuery::enableView(Zend_View_Interface $view) method that does the same for you.
Handling deployment of CDN or a local path jQuery Core and UI libraries.
Handling » $(document).ready() events.
Specifying additional stylesheet themes to use.
The jQuery() view helper implementation, like its dojo() pendant, follows the placeholder architecture implementation; the data set in it persists between view objects, and may be directly echo'd from your layout script. Since views specified in a Zend_Layout script file are rendered before the layout itself, the jQuery() helper can act as a stack for jQuery statements and render them into the head segment of the html page.
Contrary to Dojo, themes cannot be loaded from a CDN for the jQuery UI widgets and have to be implemented in your pages stylesheet file or loaded from an extra stylesheet file. A default theme called Flora can be obtained from the jQuery UI downloadable file.
Example #1 jQuery() View Helper Example
In this example a jQuery environment using the core and UI libraries will be needed. UI Widgets should be rendered with the Flora thema that is installed in 'public/styles/flora.all.css'. The jQuery libraries are both loaded from local paths.
To register the jQuery functionality inside the view object, you have to add the appropriate helpers to the view helper path. There are many ways of accomplishing this, based on the requirements that the jQuery helpers have. If you need them in one specific view only, you can use the addHelperPath method on initialization of this view, or right before rendering:
If you need them throughout your application, you can register them in your bootstrap file using access to the Controller Plugin ViewRenderer:
Now in the view script we want to display a Date Picker and an Ajax based Link.
$this->layout()->content; is printed behind the
on the onLoad stack and can be printed within the head segment of the html document.
that operate in the global namespace, Prototype for example. The Zend Framework jQuery View Helper makes usage of
the noConflict mode very easy. If you want to run Prototype and jQuery side by side you can call
ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode(); and all jQuery helpers will operate in the No Conflict
Example #2 Building your own Helper with No Conflict Mode
To make use of the NoConflict Mode in your own jQuery helper, you only have to use the static method
ZendX_JQuery_View_Helper_JQuery::getJQueryHandler() method. It returns the variable jQuery is operating
in at the moment, either
$ or $j
Since there are no online available themes to use out of the box, the implementation of the UI library themes
is a bit more complex than with the Dojo helper. The jQuery UI documentation describes for each component
what stylesheet information is needed and the Default and Flora Themes from the downloadable archive give hints
on the usage of stylesheets. The jQuery helper offers the function
function to include the dependant stylesheets whenever the helper is enabled and rendered. You can optionally
merge the required stylesheet information in your main stylesheet file.
The jQuery() view helper always returns an instance of the jQuery placeholder container. That container object has the following methods available:
enable(): explicitly enable jQuery integration.
disable(): disable jQuery integration.
isEnabled(): determine whether or not jQuery integration is enabled.
setVersion(): set the jQuery version that is used. This also decides on the library loaded from the Google Ajax Library CDN
getVersion(): get the current jQuery that is used. This also decides on the library loaded from the Google Ajax Library CDN
useCdn(): Return true, if CDN usage is currently enabled
useLocalPath(): Return true, if local usage is currently enabled
setLocalPath(): Set the local path to the jQuery Core library
getLocalPath(): If set, return the local path to the jQuery Core library
uiEnable(): explicitly enable jQuery UI integration.
uiDisable(): disable jQuery UI integration.
uiIsEnabled(): determine whether or not jQuery UI integration is enabled.
setUiVersion(): set the jQuery UI version that is used. This also decides on the library loaded from the Google Ajax Library CDN
getUiVersion(): get the current jQuery UI that is used. This also decides on the library loaded from the Google Ajax Library CDN
useUiCdn(): Return true, if CDN usage is currently enabled for jQuery UI
useUiLocal(): Return true, if local usage is currently enabled for jQuery UI
setUiLocalPath(): Set the local path to the jQuery UI library
getUiLocalPath(): If set, get the local path to the jQuery UI library
setView(Zend_View_Interface $view): set a view instance in the container.
onLoadCaptureEnd(): Stop capturing
addStylesheet($path): Add a stylesheet file that is needed for a jQuery view helper to display correctly.
getStylesheets(): Get all currently registered additional stylesheets.
getOnLoadActions(): Return all currently registered onLoad statements.
setRenderMode($mask): Render only a specific subset of the jQuery environment via ZendX_JQuery::RENDER_ constants. Rendering all elements is the default behaviour.
getRenderMode(): Return the current jQuery environment rendering mode.
setCdnSsl($bool): Set if the CDN Google Ajax Library should be loaded from an SSL or a Non-SSL location.
These are quite a number of methods, but many of them are used for internally by all the additional view helpers and during the printing of the jQuery environment. Unless you want to build your own jQuery helper or have a complex use-case, you will probably only get in contact with a few methods of these.
ZendX_JQuery::RENDER_LIBRARY: Renders jQuery Core and UI library
ZendX_JQuery::RENDER_STYLESHEETS: Renders jQuery related stylesheets
ZendX_JQuery::RENDER_JQUERY_ON_LOAD: Render jQuery onLoad statements
ZendX_JQuery::RENDER_ALL: Render all previously mentioned blocks, this is default behaviour.
For an example, if you would have merged jQuery Core and UI libraries as well as other files into a single large file as well as merged stylesheets to keep HTTP requests low on your production application. You could disallow the jQuery helper to render those parts, but render all the other stuff with the following statement in your view:
Prior to 1.8 the methods setCdnVersion(), setLocalPath() setUiCdnVersion() and setUiLocalPath() all enabled the view helper upon calling, which is considered a bug from the following perspective: If you want to use the any non-default library option, you would have to manually disable the jQuery helper aftwards if you only require it to be loaded in some scenarios. With version 1.8 the jQuery helper does only enable itsself, when enable() is called, which all internal jQuery View helpers do upon being called.
The jQuery UI Library offers a range of layout and form specific widgets that are integrated into the Zend Framework via View Helpers. The form-elements are easy to handle and will be described first, whereas the layout specific widgets are a bit more complex to use.
The method signature for all form view helpers closely resembles the Dojo View helpers signature, helper($id, $value, $params, $attribs). A description of the parameters follows:
$id: Will act as the identifier name for the helper element inside a form. If in the attributes no id element is given, this will also become the form element id, that has to be unique across the DOM.
$value: Default value of the element.
$attribs: HTML Attributes of the Form Helper
The following UI widgets are available as form view helpers. Make sure you use the correct version of jQuery UI library to be able to use them. The Google CDN always offers you the latest released version.
autoComplete($id, $value, $params, $attribs): The AutoComplete View helper is part of jQuery UI since version 1.8 and creates a text field and registeres it to have auto complete functionality. The completion data source has to be given as jQuery related parameters 'url' or 'data' as described in the jQuery UI manual.
colorPicker($id, $value, $params, $attribs): ColorPicker is still a ZendX_JQuery element for legacy reason, but was removed from jQuery UI completly.
datePicker($id, $value, $params, $attribs): Create a DatePicker inside a text field. This widget is available since jQuery UI 1.5 and can therefore currently be used with the Google CDN. Using the 'handles' option to create multiple handles overwrites the default set value and the jQuery parameter 'startValue' internally inside the view helper.
slider($id, $value, $params, $attribs): Create a Sliding element that updates its value into a hidden form field. Available since jQuery UI 1.5.
spinner($id, $value, $params, $attribs): Create a Spinner element that can spin through numeric values in a specified range. This element was removed from the 1.6 jQuery UI release and has not been re-released yet.
Example #3 Showing jQuery Form View Helper Usage
In this example we want to simulate a fictional web application that offers auctions on travel locations. A user may specify a city to travel, a start and end date, and a maximum amount of money he is willing to pay. Therefore we need an autoComplete field for all the currently known travel locations, a date picker for start and end dates and a spinner to specify the amount.
You can see the use of jQuery UI Widget specific parameters. These all correspond to those given in the jQuery UI docs and are converted to JSON and handed through to the view script.
You can then directly call the helper to render AutoComplete Output in your Controller
There is a wide range of Layout helpers that the UI library offers. The ones covered by Zend Framework view helpers are Accordion, Dialog, Tabs. Dialog is the most simple one, whereas Accordion and Tab extend a common abstract class and offer a secondary view helper for pane generation. The following view helpers exist in the jQuery view helpers collection, an example accompanies them to show their usage.
tabPane($id, $content, $options): Add a new pane to a tab container with the given $id. The given $content is shown in this tab pane. To set the title use $options['title']. If $options['contentUrl'] is set, the content of the tab is requested via ajax on tab activation.
tabContainer($id, $params, $attribs): Render a tab container with all the currently registered
panes. This view helper also offers to add panes with the following syntax:
$this->tabContainer()->addPane($id, $label, $content, $options).
accordionPane($id, $content, $options): Add a new pane to the accordion container with the given $id. The given $content is shown in this tab pane. To set the title use $options['title'].
accordionContainer($id, $params, $attribs): Render an accordion container with all the currently registered
panes. This view helper also offers to add panes with the following syntax:
$this->accordionContainer()->addPane($id, $label, $content, $options).
Example #4 Showing the latest news in a Tab Container
For this example we assume the developer already wrote the controller and model side of the script and assigned an array of news items to the view script. This array contains at most 5 news elements, so we don't have to care about the tab container getting to many tabs.