requireAsset in custom template

Permalink 1 user found helpful
Hi,

I'm trying to use the built-in lightbox (magnific popup) in a custom template.

If I try loading the js using requireAsset directly in the view.php file, it seem to work only when the cache is disabled.

Is this a bug or are we not supposed to use rquirestAsset in custom templates?

Thanks!

Jordi

jordif
 
PJSAndo replied on at Permalink Reply
Hello Jordi,

I'm trying to implement Magnific-popup but am struggling to get started...

Could you outline the steps I need to take for a newbie?

Cheers
jordif replied on at Permalink Reply
jordif
Hi,

are you trying to use it in a theme or in a custom template for a block?

Jordi
andrew replied on at Permalink Reply
andrew
If you want to use an asset in a view but want to enable block caching as well, you'll have to employ registerViewAssets() in your controller. When blocks are cached they sidestep the view() method entirely – but they'll still call registerViewAssets().

I'd recommend checking out the version used in the Feature block controller. There we register assets for a block type that's cached, and we actually require some additional assets for a specific custom template

public function registerViewAssets()
    {
        $this->requireAsset('css', 'font-awesome');
        if (is_object($this->block) && $this->block->getBlockFilename() == 'hover_description') {
            // this isn't great but it's the only way to do this and still make block
            // output caching available to this block.
            $this->requireAsset('javascript', 'bootstrap/tooltip');
            $this->requireAsset('css', 'bootstrap/tooltip');
        }
    }
MrKDilkington replied on at Permalink Reply
MrKDilkington
@Andrew

Conditionally requiring assets based on the custom template is very useful.

Thank you for the information.
PJSAndo replied on at Permalink Reply
Thanks I'll check that out.