Concrete5 импортирует JS или CSS на одной странице

Я проверял эту страницу в своих документах: http://documentation.concrete5.org/developers/assets/requiring-an-asset

Но ни один из вариантов не работает для меня. Никаких ошибок или чего-либо еще. Он просто игнорирует requireAsset метод.

контроллер:

<?php
namespace Application\Controller\SinglePage;

use PageController;

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

0

Решение

То, как вы это сделали, работает, но не очень удобно и не использует все опции.
Проблема возникла из-за того, что вы требовали актив в своем контроллере, который вы вообще никогда не объявляли.

Теперь он объявлен в вашем app.php, но это не обязательно. Вы также можете сделать это в контроллере, что облегчит обслуживание.

<?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');
}
}
2

Другие решения

Удалось наконец-то найти, как это сделать правильно, после долгих копаний. Вот как…

приложение / Config / app.php:

<?php

return array(
'assets' => array(

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

),
);

контроллер:

<?php
namespace Application\Controller\SinglePage;

use PageController;

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