0

I have just started a project with cakePHP 2.0. I am using an element to render a sidebar menu. I would like to specify the css and JS files for the menu in the menu element and not add them in my head tag (in case I'd like to conditionally render different sidebars). For some reason my scripts and CSS are not being output.. any ideas why?

Layouts/Default.ctp

<head>
<?php
echo $this->fetch('css');
echo $this->fetch('script');
?>
</head>
....
<div id="leftcolumn">
<?php  echo $this->element('sidebar/menu'); ?>
</div>

Elements/sidebar/menu.ctp

$this->Html->script('menu', array('inline' => false));
$this->Html->css('menu', null, array('inline' => false));
<div class="sidebarmenu"><ul><li>Menu Item</li></ul></div>

CSS and javascript are in webroot/css/menu.css and webroot/js/menu.js respectively.

If I put the Html->script and Html->css declarations in a view file or home.ctp or default.ctp they get added to the css and script blocks and are output just fine. When they are declared in the element file menu.ctp they don't work. Is there something I'm missing?

4

2 に答える 2

2
// css
$this->start('css');
echo $this->Html->css('additionalstyle');
echo $this->Html->css('anotherstyle');
echo $this->end()

// javascript
$this->start('script')
echo $this->Html->script('jquery');
$this->end()

このコードを view.ctp に追加します

http://book.cakephp.org/2.0/en/views.html

于 2013-02-17T16:10:54.100 に答える
1
echo $this->Html->script('menu', array('inline' => false));
echo $this->Html->css('menu', null, array('inline' => false));

問題が発生したと思います。主な問題は、cssとjsの呼び出しの順序です。

echo $this->fetch('css');
echo $this->fetch('script');

この2つの線は、頭の中で、つまり要素を呼び出す前にエコーしています。したがって、この問題には2つの解決策があります。

  1. 頭から上を切り取りfetch()、bodyタグの終わりの前に追加して、すべてのDOMが使用可能になった後にスクリプトとcssをロードするようにします。すなわち

    ... ... echo $ this-> fetch('css'); echo $ this-> fetch('script');

  2. "inline" => trueieを作る

    echo $this->Html->script('myel', array('inline' => true));
    echo $this->Html->css('mycss', null, array('inline' => true));
    

これで問題は解決すると思います。

于 2012-04-15T15:37:41.443 に答える