Zend Framework 2:: why inlineScript()->captureStart() is not working in layout.phtml

StackOverflow https://stackoverflow.com/questions/23675402

  •  23-07-2023
  •  | 
  •  

문제

I include JS file of my theme using $this->inlineScript()->appendFile() in layout.phtml.
After that I try to add some inline jquery code using $this->inlineScript()->captureStart() method.
The jquery code is not showing but if I include that in action view page then jquery code is showing fine.
Can anybody guess what I am missing.
Here is my code snippets.

 echo $this->inlineScript()->appendFile($this->basePath() . '/assets/plugins/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 7',))
                ->appendFile($this->basePath() . '/assets/plugins/excanvas.min.js', 'text/javascript', array('conditional' => 'lt IE 7',))
                ->appendFile($this->basePath('/assets/plugins/jquery-1.10.2.min.js'));


        $this->inlineScript()->captureStart();
        echo <<<JS
   jQuery(document).ready(function() {
                App.init(); // initlayout and core plugins
                Index.init();
                Index.initJQVMAP(); // init index page's custom scripts
                Index.initCalendar(); // init index page's custom scripts
                Index.initCharts(); // init index page's custom scripts
                Index.initChat();
                Index.initMiniCharts();
                Index.initDashboardDaterange();
                Index.initIntro();
                Tasks.initDashboardWidget();
            });
JS;
        $this->inlineScript()->captureEnd();
도움이 되었습니까?

해결책

If you capture additional scripts after you have echo'd the inlineScript helper output, how would you expect your additional code to be output? You either need to move your inline script above the echo in your layout, or move the echo to the end:

$this->inlineScript()->appendFile($this->basePath() . '/assets/plugins/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 7',))
                     ->appendFile($this->basePath() . '/assets/plugins/excanvas.min.js', 'text/javascript', array('conditional' => 'lt IE 7',))
                     ->appendFile($this->basePath('/assets/plugins/jquery-1.10.2.min.js'));


$this->inlineScript()->captureStart();
echo <<<JS
   jQuery(document).ready(function() {
            App.init(); // initlayout and core plugins
            Index.init();
            Index.initJQVMAP(); // init index page's custom scripts
            Index.initCalendar(); // init index page's custom scripts
            Index.initCharts(); // init index page's custom scripts
            Index.initChat();
            Index.initMiniCharts();
            Index.initDashboardDaterange();
            Index.initIntro();
            Tasks.initDashboardWidget();
        });
JS;
$this->inlineScript()->captureEnd();

echo $this->inlineScript();

It works when you do it in the action because the action is rendered before the layout.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top