Question

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();
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top