Concrete5在单页中导入JS或CSS

时间:2016-12-15 05:02:48

标签: php concrete5 concrete5-5.7 concrete

我一直在查看有关其文档的页面: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');
  }
}

2 个答案:

答案 0 :(得分:2)

你做它的方式有效,但它不是很方便,也没有使用所有选项。 问题来自这样一个事实:你需要控制器中一个你从未真正宣布过的资产。

现在它已在你的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');
    }
}

答案 1 :(得分:1)

经过多次挖掘,管理终于找到了如何正确地做到这一点。这是怎么......

<强>应用/配置/ 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');
  }
}