2

I am developing a new zend framework application, and when I just hard coded the headScript section for my layout.phtml file and Bootstrap _initScript method, when looking at the source-code after not having the script loaded... a got a really weird result.

I got this:

<script type="text/javascript">
    //<!--
    /js/jquery-1.8.1.min.js    //-->

Here is my layout.phtml code:

<?php echo $this->doctype(); ?> 
<html>
    <head>
        <?php echo $this->headTitle(); ?>
        <?php echo $this->headLink(); ?>
        <?php echo $this->headScript(); ?>
    </head>
    <body>
        <?php echo $this->layout()->content; ?>
    </body> 
</html>

Here is my Bootstrap.php code:

protected function _initScript()
{
    $this->view->headScript()
       ->prependScript( "/js/jquery-1.8.1.min.js" );
}

As you can see it is very ordinary code!

Anybody could help me find out what's going on here?

1
  • With an interactive debugger you can find it out easily. Commented Aug 31, 2012 at 3:06

4 Answers 4

8

You are trying to add "file" not "script".

Change Bootstrap.php to:

protected function _initScript()
{
    $this->view->headScript()
       ->prependFile( "/js/jquery-1.8.1.min.js", $type = 'text/javascript' );
}

Also check this for headScript reference.

Any questions? :)

Sign up to request clarification or add additional context in comments.

2 Comments

Same result without $type = 'text/javascript'.
Don't bury me alive if I follow semantics :D Yes, +1 to your comment, it is not mandatory but, zend semantics (coding , conventions and stuff) requires me to write it. However, during RAPID development it is OK to miss on that part!
3

I find that when setting up the view it's usually better to get the view object, set the placeholder values and then return the view, something like:

protected function _initView()
    {
        //Initialize view
        $view = new Zend_View();  

        //set doctype for default layout
        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);

        //set css includes
        $view->headLink()->setStylesheet('/css/normalize.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');

        //add javascript files
        $view->headScript()->setFile('/javascript/modernizr.js');
        $view->headScript()->appendFile('/javascript/jquery.js');

        //add it to the view renderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                'ViewRenderer');
        $viewRenderer->setView($view);

        //Return it, so that it can be stored by the bootstrap
        return $view;
    }

I'm not sure how well the $this->view-> syntax works from the bootstrap but I suspect it has some issues.

3 Comments

+1 for putting out the logic But, as we can see in the question, he has displayed the HTML source output!
I've created a method for handling my view this way: $this->view = Zend_Layout::startMvc()->getView(); That's how I am getting the view inside the Bootstrap across the methods...
Thanks for educating. Plans out a prank for his colleagues
0

I use the Zendx_jQuery component (http://framework.zend.com/manual/1.11/en/zendx.jquery.html), I find it handy, especially if you use jQuery UI in your forms.

In Bootstrap.php :

protected function _initjQuery()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');
    $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper_');
    return $view;
}

In my layout html header :

$this->jQuery()->setLocalPath($this->baseUrl('js/libs/jquery-1.7.2.min.js'))
        ->setUiLocalPath($this->baseUrl('js/libs/jquery-ui-1.8.20.custom.min.js'))
        ->addStylesheet($this->baseUrl('css/ui-lightness/jquery-ui-1.8.20.custom.css'))
        ->enable()
        ->uiEnable();

/* I only print jquery css in the header */
<?= $this->jQuery()->setRenderMode(ZendX_JQuery::RENDER_STYLESHEETS); ?>

In my layout body bottom :

<?= $this->jQuery()->setRenderMode(ZendX_JQuery::RENDER_LIBRARY | ZendX_JQuery::RENDER_SOURCES | ZendX_JQuery::RENDER_JAVASCRIPT | ZendX_JQuery::RENDER_JQUERY_ON_LOAD); ?>

Comments

0

Try this:

<?php

$this->headTitle('Your title');
$this->headScript()->headScript()->prependFile('/js/jquery.js');
$this->headLink()->appendStylesheet('/css/yourcss.css');

echo $this->doctype();
?>

<html>
<head>
    <?php echo $this->headTitle(); ?>
    <?php echo $this->headLink(); ?>
    <?php echo $this->headScript(); ?>
</head>
<body>
    <?php echo $this->layout()->content; ?>
</body> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.