| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- EN-Revision: 24249 -->
- <!-- Reviewed: no -->
- <sect3 id="zend.view.helpers.initial.json">
- <title>L'aide de vue JSON</title>
- <para>
- Quand vous créez des vues qui retournent du <acronym>JSON</acronym>, il est important de paramétrer aussi
- les en-têtes de réponse appropriés. L'aide vue <acronym>JSON</acronym> réalise exactement cela. De plus, par
- défaut, elle désactive l'éventuel layout (s'il est activé), puisque les layouts sont
- rarement utilisés dans les réponses <acronym>JSON</acronym>.
- </para>
- <para>L'aide de vue <acronym>JSON</acronym> ajoute l'en-tête suivant :</para>
- <programlisting language="text"><![CDATA[
- Content-Type: application/json
- ]]></programlisting>
- <para>
- Beaucoup de librairies <acronym>AJAX</acronym> recherche cet en-tête quand elles analysent les réponses
- pour déterminer comment le contenu doit être géré.
- </para>
- <para>L'utilisation de l'aide de vue <acronym>JSON</acronym> est très simple :</para>
- <programlisting language="php"><![CDATA[
- <?php echo $this->json($this->data) ?>
- ]]></programlisting>
- <note>
- <title>Keeping layouts and enabling encoding using Zend_Json_Expr</title>
- <para>
- Each method in the <acronym>JSON</acronym> helper accepts a second, optional argument.
- This second argument can be a boolean flag to enable or disable
- layouts, or an array of options that will be passed to
- <methodname>Zend_Json::encode()</methodname> and used internally to encode data.
- </para>
- <para>
- To keep layouts, the second parameter needs to be boolean
- <constant>TRUE</constant>. When the second parameter is an array, keeping
- layouts can be achieved by including a <code>keepLayouts</code> key
- with a value of a boolean <constant>TRUE</constant>.
- </para>
- <programlisting language="php"><![CDATA[
- // Boolean true as second argument enables layouts:
- echo $this->json($this->data, true);
- // Or boolean true as "keepLayouts" key:
- echo $this->json($this->data, array('keepLayouts' => true));
- ]]></programlisting>
- <para>
- <classname>Zend_Json::encode</classname> allows the encoding of native <acronym>JSON</acronym>
- expressions using <classname>Zend_Json_Expr</classname> objects. This option
- is disabled by default. To enable this option, pass a boolean
- <constant>TRUE</constant> to the <code>enableJsonExprFinder</code> key of
- the options array:
- </para>
- <programlisting language="php"><![CDATA[
- <?php echo $this->json($this->data, array(
- 'enableJsonExprFinder' => true,
- 'keepLayouts' => true,
- )) ?>
- ]]></programlisting>
- </note>
- </sect3>
|