1

I am trying to activate jQuery through a view helper called within the layout.

The problem is that jQuery is already called within the layout and thus renders the include files, before it is defined in the view helper.

Here are my scripts :

Layout.phtml:

<?php echo $this->doctype(); ?>
<html>
<head>
    <?php echo $this->headTitle() ?>
    <?php echo $this->headLink()->appendStylesheet('/css/base.css') ?>
    <?php echo $this->headMeta() ?>
    <?php echo $this->headStyle() ?>
    <?php echo $this->jQuery() ?>

</head>
<body>
[...]
    <div id="droite" class="column grid_4">
        <!-- Column 2 start -->
        <?php echo $this->render('partials/droite.phtml'); ?>
        <!-- Column 2 end -->
    </div>
</body>
</html>

partials/droite.phtml:

<?=$this->rolelinks(); ?>

My_View_Helper_Rolelinks:

<?php

class My_View_Helper_Rolelinks extends Zend_View_Helper_Abstract
{

    public function rolelinks()
    {
        if (Model_User::hasIdentity()) {
            $role = Model_User::getRole();
            if ($role === 'admin') {
                return $this->view->partial('partials/droite_admin.phtml');
                return;
            }
        } else {
            return '';
        }
    }
}

partials/droite_admin.phtml:

<?php
$this->jQuery()
    ->UiEnable()
    ->addJavascriptFile('/js/jquery.ui.datepicker-fr.js')
    ->addJavascriptFile('/js/onload.js');
?>

<div id="calendar"></div>

So.

Not sure this is the right way to do it, but the main idea is to check if the user is admin to enable jQuery and display a datepicker (calendar).

Thanks in advance for your help.

ant1j
  • 305
  • 2
  • 18
  • Be careful with using ZendX_JQuery to much as there is discussion about [disconcerting](http://zend-framework-community.634137.n4.nabble.com/Discontinuing-Maintenance-of-ZendX-JQuery-Suggest-drop-for-2-0-td3221855i40.html) it. – Marcin Mar 07 '11 at 00:45
  • Even without using ZendX_JQuery, the question is to know how I can activate jquery (i.e. modify my `` elements) using a Zend_View_Helper called in a layout script. – ant1j Mar 07 '11 at 14:33

1 Answers1

0

You could try to check if jquery is already enabled in your layout to avoid double inclusion.

<head>
    <?php echo $this->headTitle() ?>
    <?php echo $this->headLink()->appendStylesheet('/css/base.css') ?>
    <?php echo $this->headMeta() ?>
    <?php echo $this->headStyle() ?>

    <?php if (! $this->jQuery()->isEnabled()) {
        echo $this->jQuery();
     } ?>
</head>
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • unfortunately for the moment, I have no inclusion at all! From what I understand, I will need to define my View_Helper's "action" (enable jQuery) within the view script itself, as the layout script is rendered afterwards ... – ant1j Mar 07 '11 at 14:15