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
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
Hi,
are you trying to use it in a theme or in a custom template for a block?
Jordi
are you trying to use it in a theme or in a custom template for a block?
Jordi
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
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'); } }
@Andrew
Conditionally requiring assets based on the custom template is very useful.
Thank you for the information.
Conditionally requiring assets based on the custom template is very useful.
Thank you for the information.
Thanks I'll check that out.
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