1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.dojo.view.dojo">
4 <title>dojo() View Helper</title>
7 The <methodname>dojo()</methodname> view helper is intended to simplify setting up
8 the Dojo environment, including the following responsibilities:
14 Specifying either a <acronym>CDN</acronym> or a local path to a Dojo install.
18 <listitem><para>Specifying paths to custom Dojo modules.</para></listitem>
19 <listitem><para>Specifying <command>dojo.require</command> statements.</para></listitem>
20 <listitem><para>Specifying dijit stylesheet themes to use.</para></listitem>
23 <para>Specifying <command>dojo.addOnLoad()</command> events.</para>
28 The <methodname>dojo()</methodname> view helper implementation is an example of a
29 placeholder implementation. The data set in it persists between view
30 objects and may be directly echoed from your layout script.
33 <example id="zend.dojo.view.dojo.usage">
34 <title>dojo() View Helper Usage Example</title>
37 For this example, let's assume the developer will be using Dojo from
38 a local path, will require several dijits, and will be
39 utilizing the Tundra dijit theme.
43 On many pages, the developer may not utilize Dojo at all. So, we
44 will first focus on a view script where Dojo is needed and then on the
45 layout script, where we will setup some of the Dojo environment and
50 First, we need to tell our view object to use the Dojo view helper
51 paths. This can be done in your bootstrap or an early-running
52 plugin; simply grab your view object and execute the following:
55 <programlisting language="php"><![CDATA[
56 $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
60 Next, the view script. In this case, we're going to specify
61 that we will be using a FilteringSelect -- which will consume a
62 custom store based on QueryReadStore, which we'll call
63 'PairedStore' and store in our 'custom' module.
66 <programlisting language="php"><![CDATA[
67 <?php // setup data store for FilteringSelect ?>
68 <div dojoType="custom.PairedStore" jsId="stateStore"
69 url="/data/autocomplete/type/state/format/ajax"
70 requestMethod="get"></div>
72 <?php // Input element: ?>
73 State: <input id="state" dojoType="dijit.form.FilteringSelect"
74 store="stateStore" pageSize="5" />
76 <?php // setup required dojo elements:
77 $this->dojo()->enable()
78 ->setDjConfigOption('parseOnLoad', true)
79 ->registerModulePath('custom', '../custom/')
80 ->requireModule('dijit.form.FilteringSelect')
81 ->requireModule('custom.PairedStore'); ?>
85 In our layout script, we'll then check to see if Dojo is enabled,
86 and, if so, we'll do some more general configuration and assemble
90 <programlisting language="php"><![CDATA[
91 <?php echo $this->doctype() ?>
94 <?php echo $this->headTitle() ?>
95 <?php echo $this->headMeta() ?>
96 <?php echo $this->headLink() ?>
97 <?php echo $this->headStyle() ?>
98 <?php if ($this->dojo()->isEnabled()){
99 $this->dojo()->setLocalPath('/js/dojo/dojo.js')
100 ->addStyleSheetModule('dijit.themes.tundra');
104 <?php echo $this->headScript() ?>
106 <body class="tundra">
107 <?php echo $this->layout()->content ?>
108 <?php echo $this->inlineScript() ?>
114 At this point, you only need to ensure that your files are in the
115 correct locations and that you've created the end point action for
116 your FilteringSelect!
121 <title>UTF-8 encoding used by default</title>
124 By default, Zend Framework uses <acronym>UTF-8</acronym> as its default encoding, and,
125 specific to this case, <classname>Zend_View</classname> does as well. Character encoding
126 can be set differently on the view object itself using the
127 <methodname>setEncoding()</methodname> method (or the <property>encoding</property>
128 instantiation parameter). However, since <classname>Zend_View_Interface</classname> does
129 not define accessors for encoding, it's possible that if you are using a custom view
130 implementation with the Dojo view helper, you will not have a
131 <methodname>getEncoding()</methodname> method, which is what the view helper uses
132 internally for determining the character set in which to encode.
136 If you do not want to utilize <acronym>UTF-8</acronym> in such a situation, you will
137 need to implement a <methodname>getEncoding()</methodname> method in your custom view
142 <sect3 id="zend.dojo.view.dojo.declarative">
143 <title>Programmatic and Declarative Usage of Dojo</title>
146 Dojo allows both <emphasis>declarative</emphasis> and
147 <emphasis>programmatic</emphasis> usage of many of its features.
148 <emphasis>Declarative</emphasis> usage uses standard <acronym>HTML</acronym> elements
149 with non-standard attributes that are parsed when the page is
150 loaded. While this is a powerful and simple syntax to utilize, for
151 many developers this can cause issues with page validation.
155 <emphasis>Programmatic</emphasis> usage allows the developer to
156 decorate existing elements by pulling them by ID or <acronym>CSS</acronym> selectors
157 and passing them to the appropriate object constructors in Dojo.
158 Because no non-standard <acronym>HTML</acronym> attributes are used, pages continue to
163 In practice, both use cases allow for graceful degradation when
164 javascript is disabled or the various Dojo script resources are
165 unreachable. To promote standards and document validation,
166 Zend Framework uses programmatic usage by default; the various view
167 helpers will generate javascript and push it to the
168 <methodname>dojo()</methodname> view helper for inclusion when rendered.
172 Developers using this technique may also wish to explore the option
173 of writing their own programmatic decoration of the page. One
174 benefit would be the ability to specify handlers for dijit events.
178 To allow this, as well as the ability to use declarative syntax,
179 there are a number of static methods available to set this behavior
183 <example id="zend.dojo.view.dojo.declarative.usage">
184 <title>Specifying Declarative and Programmatic Dojo Usage</title>
187 To specify declarative usage, simply call the static
188 <methodname>setUseDeclarative()</methodname> method:
191 <programlisting language="php"><![CDATA[
192 Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
196 If you decide instead to use programmatic usage, call the static
197 <methodname>setUseProgrammatic()</methodname> method:
200 <programlisting language="php"><![CDATA[
201 Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
205 Finally, if you want to create your own programmatic rules, you
206 should specify programmatic usage, but pass in the value '-1';
207 in this situation, no javascript for decorating any dijits used
211 <programlisting language="php"><![CDATA[
212 Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
217 <sect3 id="zend.dojo.view.dojo.themes">
218 <title>Themes</title>
221 Dojo allows the creation of themes for its dijits (widgets). You may
222 select one by passing in a module path:
225 <programlisting language="php"><![CDATA[
226 $view->dojo()->addStylesheetModule('dijit.themes.tundra');
230 The module path is discovered by using the character '.' as a
231 directory separator and using the last value in the list as the name
232 of the <acronym>CSS</acronym> file in that theme directory to use; in the example
233 above, Dojo will look for the theme in
234 '<filename>dijit/themes/tundra/tundra.css</filename>'.
238 When using a theme, it is important to remember to pass the theme
239 class to, at the least, a container surrounding any dijits you are
240 using; the most common use case is to pass it in the body:
243 <programlisting language="html"><![CDATA[
244 <body class="tundra">
248 <sect3 id="zend.dojo.view.dojo.layers">
249 <title>Using Layers (Custom Builds)</title>
252 By default, when you use a <command>dojo.require</command> statement, dojo will make a
253 request back to the server to grab the appropriate javascript file.
254 If you have many dijits in place, this results in many requests to
255 the server -- which is not optimal.
259 Dojo's answer to this is to provide the ability to create
260 <emphasis>custom builds</emphasis>. Builds do several things:
266 Groups required files into <emphasis>layers</emphasis>; a layer
267 lumps all required files into a single JS file. (Hence the name
274 "Interns" non-javascript files used by dijits (typically,
275 template files). These are also grouped in the same JS file as
282 Passes the file through ShrinkSafe, which strips whitespace and
283 comments, as well as shortens variable names.
289 Some files can not be layered, but the build process will create a
290 special release directory with the layer file and all other files.
291 This allows you to have a slimmed-down distribution customized for
292 your site or application needs.
296 To use a layer, the <methodname>dojo()</methodname> view helper has a
297 <methodname>addLayer()</methodname> method for adding paths to required layers:
300 <programlisting language="html"><![CDATA[
301 $view->dojo()->addLayer('/js/foo/foo.js');
305 For more information on creating custom builds, please <ulink
306 url="http://dojotoolkit.org/reference-guide/dojo/index.html#package-system">refer
307 to the Dojo build documentation</ulink>.
311 <sect3 id="zend.dojo.view.dojo.methods">
312 <title>Methods Available</title>
315 The <methodname>dojo()</methodname> view helper always returns an instance of
316 the dojo placeholder container. That container object has the
317 following methods available:
323 <methodname>setView(Zend_View_Interface $view)</methodname>: set a view instance
329 <para><methodname>enable()</methodname>: explicitly enable Dojo integration.</para>
333 <para><methodname>disable()</methodname>: disable Dojo integration.</para>
338 <methodname>isEnabled()</methodname>: determine whether or not Dojo integration
345 <methodname>requireModule($module)</methodname>: setup a
346 <command>dojo.require</command> statement.
352 <methodname>getModules()</methodname>: determine what modules have been
359 <methodname>registerModulePath($module, $path)</methodname>:
360 register a custom Dojo module path.
366 <methodname>getModulePaths()</methodname>: get list of registered module paths.
372 <methodname>addLayer($path)</methodname>: add a layer (custom build) path to
379 <methodname>getLayers()</methodname>: get a list of all registered layer paths
386 <methodname>removeLayer($path)</methodname>: remove the layer
387 that matches <varname>$path</varname> from the list of registered
388 layers (custom builds).
394 <methodname>setCdnBase($url)</methodname>: set the base <acronym>URL</acronym>
395 for a <acronym>CDN</acronym>; typically, one of the
396 <constant>Zend_Dojo::CDN_BASE_AOL</constant> or
397 <constant>Zend_Dojo::CDN_BASE_GOOGLE</constant>, but it only needs
398 to be the <acronym>URL</acronym> string prior to the version number.
404 <methodname>getCdnBase()</methodname>: retrieve the base <acronym>CDN</acronym>
411 <methodname>setCdnVersion($version = null)</methodname>: set
412 which version of Dojo to utilize from the <acronym>CDN</acronym>.
418 <methodname>getCdnVersion()</methodname>: retrieve what
419 version of Dojo from the <acronym>CDN</acronym> will be used.
425 <methodname>setCdnDojoPath($path)</methodname>: set the relative
426 path to the <filename>dojo.js</filename> or <filename>dojo.xd.js</filename>
427 file on a <acronym>CDN</acronym>; typically, one of the
428 <constant>Zend_Dojo::CDN_DOJO_PATH_AOL</constant> or
429 <constant>Zend_Dojo::CDN_DOJO_PATH_GOOGLE</constant>, but it only
430 needs to be the path string following the version number.
436 <methodname>getCdnDojoPath()</methodname>: retrieve the last path segment of the
437 <acronym>CDN</acronym> url pointing to the <filename>dojo.js</filename> file.
443 <methodname>useCdn()</methodname>: tell the container to
444 utilize the <acronym>CDN</acronym>; implicitly enables integration.
450 <methodname>setLocalPath($path)</methodname>: tell the container
451 the path to a local Dojo install (should be a path relative
452 to the server, and contain the <filename>dojo.js</filename> file itself);
453 implicitly enables integration.
459 <methodname>getLocalPath()</methodname>: determine what local
460 path to Dojo is being used.
466 <methodname>useLocalPath()</methodname>: is the integration
467 utilizing a Dojo local path?
473 <methodname>setDjConfig(array $config)</methodname>: set
474 dojo or dijit configuration values (expects assoc array).
480 <methodname>setDjConfigOption($option, $value)</methodname>: set
481 a single dojo or dijit configuration value.
487 <methodname>getDjConfig()</methodname>: get all dojo or dijit configuration
494 <methodname>getDjConfigOption($option, $default = null)</methodname>: get a
495 single dojo or dijit configuration value.
501 <methodname>addStylesheetModule($module)</methodname>: add a
502 stylesheet based on a module theme.
508 <methodname>getStylesheetModules()</methodname>: get stylesheets
509 registered as module themes.
515 <methodname>addStylesheet($path)</methodname>: add a local
516 stylesheet for use with Dojo.
521 <para><methodname>getStylesheets()</methodname>: get local Dojo stylesheets.</para>
526 <methodname>addOnLoad($spec, $function = null)</methodname>: add
527 a lambda for <command>dojo.onLoad</command> to call. If one argument is passed,
528 it is assumed to be either a function name or a javascript
529 closure. If two arguments are passed, the first is assumed
530 to be the name of an object instance variable and the second
531 either a method name in that object or a closure to utilize
538 <methodname>prependOnLoad($spec, $function = null)</methodname>:
539 exactly like <methodname>addOnLoad()</methodname>, excepts prepends to
540 beginning of onLoad stack.
546 <methodname>getOnLoadActions()</methodname>: retrieve all
547 <command>dojo.onLoad</command> actions registered with the container. This will
548 be an array of arrays.
554 <methodname>onLoadCaptureStart($obj = null)</methodname>:
555 capture data to be used as a lambda for <command>dojo.onLoad()</command>.
556 If <varname>$obj</varname> is provided, the captured JS code will be considered
557 a closure to use with that Javascript object.
563 <methodname>onLoadCaptureEnd($obj = null)</methodname>: finish
564 capturing data for use with <command>dojo.onLoad()</command>.
570 <methodname>javascriptCaptureStart()</methodname>:
571 capture arbitrary javascript to be included with Dojo JS
572 (onLoad, require, etc. statements).
578 <methodname>javascriptCaptureEnd()</methodname>: finish capturing javascript.
584 <methodname>__toString()</methodname>: cast the container to a
585 string; renders all <acronym>HTML</acronym> style and script elements.