开发者

Zend_Framework -- How do I put Javascript in the layout?

I need a method of inserting javascript which is controller/action specific into a layout. That javascript needs to go inside the <head> of the document, far from where normal content is placed. I already have an infrastructure in place which allows use of multiple views per page, and the Zend_Layout I already have takes full advantage of this:

<?php
     $script = $this->layout()->script开发者_StackOverflow中文版;
     if (!is_null($script)) : ?>
<script type="text/javascript"> // <![CDATA[
  <?php echo $script; ?>
// ]]>
</script>
<?php endif; ?>

However, I'd like the script output to be automatically selected, just like the normal view is automatically placed into $this->layout()->content of the layout by default. I understand this facility is provided by the ViewRenderer class. Basically, what I'd like to do is check for an instance of /VIEWPATH/scripts/CONTROLLER/ACTION.js.php, and render it as the script named output segment if it exists.

I could relatively simply create a Zend_Controller_Plugin which would automatically do that in post dispatch, but then controllers would have no way of setting values on the script's view. I also would need some way of replicating how the ViewRenderer controller plugin is inflecting the controller and action names.

Ideally I'd just somehow tack this on to the ViewRenderer helper, but it doesn't seem to support that kind of thing.

Am I going about this entirely wrong? Is there some mechanism for embedding page specific Javascript built into the framework? (I can't be the only person with this problem....)

Billy3


Extending my comment

Here is the doc for what are you looking for:

http://framework.zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial.headscript

You can use captureStart() and create your scripts dynamically inside each related view. With this approach you don't need to create *.js.php files.


I think there is no build in mechanism. Iam using an small controller plugin like this:

class My_Controller_Plugin_JavaScript extends Zend_Controller_Plugin_Abstract

{

/**
 * preDispatch
 * Check controller name, and include javaScript library
 *
 * @param Zend_Controller_Request_Abstract $request
 * @return void
 */
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    $layout = Zend_Layout::getMvcInstance();
    $view = $layout->getView();
    $controller = $request->getControllerName();

    $jsFile = $controller . '-lib.js';
    $jsPath = $view->baseUrl() .
               '/js/' . $controller .
               '/';

    $sPath = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR;
    $sPath .= $controller . DIRECTORY_SEPARATOR . $jsFile;

    if (file_exists($sPath)) { // load as last js (offset 100)
        $view->headScript()->offsetSetFile(
            100,
            $jsPath . $jsFile


           );
        }
    }
}

It adds an js file by controller name. Layout iam echoing it

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

You could extend it to use action to. Iam sure there are better ways, but it works!


Zend Framework had view helpers to add javascript file(first snippet) + text javascript(second snippet) you could add javascript files

<?php
    $this->headScript()->appendFile($this->baseUrl("js/jquery-1.4.2.min"))
        ->appendFile($this->baseUrl("js/jquery-ui-1.8.2.custom.min"));
    $this->headScript()->appendScript("js/dummy.js");
    echo $this->headScript();

?>

then in some where else , you could add

    <?php $this->headScript()->captureStart() ?>
// start jquery functions 
    var action = '<?php echo $this->baseUrl ?>'; 
    $('foo_form').action = action;
// end jquery functions 
    <?php $this->headScript()->captureEnd() ?>

The following assumptions are made:

The script will be appended to the stack. If you wish for it to replace the stack or be added to the top, you will need to pass 'SET' or 'PREPEND', respectively, as the first argument to captureStart(). The script MIME type is assumed to be 'text/javascript'; if you wish to specify a different type, you will need to pass it as the second argument to captureStart(). If you wish to specify any additional attributes for the tag, pass them in an array as the third argument to captureStart().to captureStart().

source : http://framework.zend.com/manual/en/zend.view.helpers.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜