the best way to include js file in zend framework

后端 未结 2 1482
后悔当初
后悔当初 2021-01-12 11:15

I want to include external js file in my php script. I am following zend framework.Right now I am adding js file in controller\'s init function like this.

pu         


        
相关标签:
2条回答
  • 2021-01-12 11:56

    In above solution the 'path/to/file.js' script will be included in the header twice. There is no need for echo before calling prependFile. Such behaviour can lead to duplicating javascript controls.

    <!-- layout.phtml --> <head> <?php echo $this->headScript()->prependFile( $this->baseUrl('path/to/file.js')) // this will echo out the whole prependFile queue

    0 讨论(0)
  • 2021-01-12 11:59

    JavaScript (and images, CSS, flash movies, etc) belong to the view layer so configure them there.

    For globally included files, add them to your layout, eg

    <!-- layout.phtml -->
    <head>
        <?php echo $this->headScript()->prependFile(
            $this->baseUrl('path/to/file.js')) ?>
        <?php echo $this->headLink()->prependStylesheet(
            $this->baseUrl('path/to/file.css')) ?>
    
    <!-- snip -->
    
        <?php echo $this->inlineScript()->prependFile(
            'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js') ?>
    </body>
    

    Your view scripts can then add assets to the helpers which are echoed out in the layout. As the layout uses the prepend*() methods, the global files will be displayed first, eg

    <?php // views/scripts/index/index.phtml
    $this->inlineScript()->appendFile($this->baseUrl('path/to/script.js'));
    
    0 讨论(0)
提交回复
热议问题