Concrete5 import JS or CSS in single page

593 views Asked by At

I've been checking this page out on their docs: http://documentation.concrete5.org/developers/assets/requiring-an-asset

But none of the options are working out for me. No errors or anything. It just ignores the requireAsset method.

Controller:

<?php
namespace Application\Controller\SinglePage;

use PageController;

class MyAccount extends PageController
{
  public function view()
  {
    $this->requireAsset('javascript', 'js/my_account');
  }
}
2

There are 2 answers

0
adamj On BEST ANSWER

Managed to finally find how to do it properly, after much digging. Here's how...

application/config/app.php:

<?php

return array(
    'assets' => array(

      'foobar/my-account' => array(
            array(
                'javascript',
                'js/my_account.js'
            ),
        ),

    ),
);

Controller:

<?php
namespace Application\Controller\SinglePage;

use PageController;

class MyAccount extends PageController
{
  public function view()
  {
    $this->requireAsset('javascript', 'foobar/my-account');
  }
}
1
Nour Akalay On

the way you did it works but it's not very handy and doesn't make use of all the options. The problem came from the fact that you were requiring an asset in your controller that you had never really declared in the first place.

Now it is declared in your app.php but it doesn't have to be. You can do it in the controller as well which will make things easier to maintain.

<?php
namespace Application\Controller\SinglePage;

use PageController;
use AssetList;
use Asset;

class MyAccount extends PageController
{
    public function view()
    {
        $al = AssetList::getInstance();

        // Register (declare) a javascript script. here I called it foobar/my-script which is the reference used to request it
        $al->register(
            'javascript', 'foobar/my-script', 'js/my_account.js', array('version' => '1.0', 'position' => Asset::ASSET_POSITION_FOOTER, 'minify' => true, 'combine' => true)
        );

        // Register (declare) a css stylesheet. here I called it foobar/my-stylesheet which is the reference used to request it
        $al->register(
            'css', 'foobar/my-stylesheet', 'css/my_account.css', array('version' => '1.0', 'position' => Asset::ASSET_POSITION_HEADER, 'minify' => true, 'combine' => true)
        );

        // Gather all the assets declared above in an array so you can request them all at once if needed
        $assets = array(
            array('css', 'foobar/my-stylesheet'),
            array('javascript', 'foobar/my-script')
        );

        // Register the asset group that includes all your assets (or a subset as you like). here I called it foobar/my-account which is the reference used to request it
        $al->registerGroup('foobar/my-account', $assets);

        // require the group so all the assets are loaded together
        $this->requireAsset('foobar/my-account');

        // Alternatively you can call only one of them
        // $this->requireAsset('javascript', 'foobar/my-script');
    }
}