0

I have some views in ZF2 that must include js and css files. I can do so in view:

$this->headscript()->appendScript(...

But, when I get view result using ajax I need to get from server array with view html, css includes and js includes. Like this:

array(
    'html' => '<div id="timepicker-wrap"></div>',
    'js_include' => array('/js/timepicker.js'),
    'js_eval' => '$("#timepicker-wrap").timepicker()',
    'css_include' => array('/css/timepicker.css'),
);

After I give this data to client-side my js library will include html, js and css.

The problem is to get all js and css files and code from headscript in server-side.

Am I right that I have to extend HeadScript, HeadStyle and InlineScript classes and add functions that returns all js and css? Or I have to do something else?

2 Answers 2

2

You can bypass the layout when using Ajax quite easily, one example:

SomeController.php

public function myAction()
{
    if($this->getRequest()->isXmlHttpRequest()) {
        $this->layout('application/layout/ajax-layout');
    }

    return new ViewModel();
}

application/layout/ajax-layout.phtml

<?php echo $this->content ?>

When you try and access somecontroller/my/ using an Ajax request, the base layout, which includes your head/js/etc will not be rendered.

it will be rendered when you view with a normal request though.

There's a couple of other ways of doing this, but this is probably the simplest :)

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

Comments

0

You can get all js and css from headScript, headStyle and InlineScript using getContainer() method

$this->headScript()->getContainer()->getValue()

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.