Custom block template asset not loaded

Permalink
When I use another template than the main template. The JavaScript for the slider is not loaded. When I don't select another template. The swiper js is loaded. Since this file is registered via the Assetlist I expected the JavaScript would also be available for template files of this block.

The custom block has two templates the default view.php and /templates/content/view.php

In the controller I register the assets on start and call them in registerViewAssets.

public function on_start()
{
   //register assets
   $al = \Concrete\Core\Asset\AssetList::getInstance();
   $al->register(
      'javascript', 'swiper', 'blocks/swiper/swiper.jquery.min.js',
      array('version' => '3.0.7', 'minify' => false, 'combine' => true)
   );
   $al->registerGroup('swiper', array(
      array('js', 'swiper')
   ));
}
public function registerViewAssets()
{
   $this->requireAsset('javascript', 'jqeury');


Via the network panel in Chrome I can see that the js is not loaded when I use the template file. Is there something wrong with the code or dit I not understand the concept of Asset registering?

Thanks

View Replies:
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi rge,

Do you have block cache enabled?

Have you tried registering the assets in registerViewAssets() instead of on_start()?

"Ah, interesting. I think if a block is cached it doesn't run on_start() or the view() method.

In the meantime you can probably register the assets in the registerViewAssets() before running requireAsset() and that would probably fix it. In the grand scheme of things we should probably run on_start() regardless of whether the block is cached or not."
https://www.concrete5.org/community/forums/5-7-discussion/5.7-docume...
rge replied on at Permalink Reply
Thanks for the reply. Caching is not enabled at this moment. I have moved the asset registering and require to registerViewAssets. And fixed ('js' => 'javascript') in the registering.

After this I cleared the cache, refreshed the block. Unfortunately the JavaScript is still not loaded. But this is only the case when I select a template for the block. When the main view.php is used the JS is loaded.

public function registerViewAssets()
{
        //check if function is triggered
        echo "string";
   //register assets
   $al = \Concrete\Core\Asset\AssetList::getInstance();
        $al->register(
      'javascript', 'swiper', 'blocks/swiper/swiper.jquery.min.js',
      array('version' => '3.0.7', 'minify' => false, 'combine' => true)
   );
   $al->registerGroup('swiper', array(
      array('javascript', 'jquery'),
      array('javascript', 'swiper')
       ));
   $this->requireAsset('swiper');


To test if the function is called I echo "string". This works.
rge replied on at Permalink Reply
I Found the problem. Because the js file was in the js folder. It got loaded for the main view.php. The asset registering was wrong:
- the path was incorrect
- pkg name was not defined

public function registerViewAssets()
    {   
       $pkg = 'package_handle';
       //register assets
       $al = \Concrete\Core\Asset\AssetList::getInstance();
       $al->register(
          'javascript', 'swiper', 'blocks/swiper/asset/swiper.jquery.min.js',
          array('version' => '3.0.7', 'minify' => false, 'combine' => true),
          $pkg
       );
       $al->register(
          'css', 'swiper', 'blocks/swiper/css/swiper.css',
          array('version' => '3.0.7', 'minify' => false, 'combine' => true),
          $pkg
       );
shahroq replied on at Permalink Reply
shahroq
Hi,
Have the same problem and couldn't figure it out. I have something like this at package main controller file and it's not working:
$al = AssetList::getInstance();
$al->register('css', 'my_package_css', 'css/my_package_css.css', array(), 'my_package_handle');
rge replied on at Permalink Reply
The path is wrong should start with: blocks/blockname/foldername/file.css
shahroq replied on at Permalink Reply
shahroq
The css i want to include is at packages/my_package/css/*.css
rge replied on at Permalink Reply
The path is wrong should start with: blocks/blockname/foldername/file.css
WillemAnchor replied on at Permalink Reply
WillemAnchor