0
votes

I wanted to append css stylesheet inside head tag but gets appended to body tag. Here's what i did: i have a controller 'countries' with action 'index' and my view is index.phtml

index.phtml contains:

<?php
 $this->headLink()->appendStylesheet($this->baseUrl().'/js/dojo-1.7/dojo/resources/dojo.css')
             ->appendStylesheet($this->baseUrl().'/js/dojo-1.7/dojox/grid/resources/claroGrid.css');
 echo $this->headLink();

This causes the stylesheet to be appended inside body tag. I only want these stylesheet to be appended to this action. I dont want to include this stylesheet inside my layout.phtml How can i do it?

2

2 Answers

3
votes

What you've done is correct, but you're echoing headLink() in your view just after adding the stylesheet which is what is outputting the links in the wrong place. Change your code so you have:

In index.phtml:

$this->headLink()->appendStylesheet($this->baseUrl().'/js/dojo-1.7/dojo/resources/dojo.css')
         ->appendStylesheet($this->baseUrl().'/js/dojo-1.7/dojox/grid/resources/claroGrid.css');

And then in layout.phtml (in the <head> section where you want the stylesheet links to appear):

echo $this->headLink();
2
votes

you're adding this in the incorrect place.
If you add a a style sheet in this manner from inside your .phtml you are in the body of the document.
To add a style sheet to the head of a single page add the stylesheet from your action code inside your controller:

public function someAction() {
    $this->view->headLink()->appendStylesheet($this->baseUrl().'/js/dojo-1.7/dojo/resources/dojo.css');
}

alternately if you want to add a style sheet to all the actions of a controller you can use the same code in the init() method of your controller:

public function init() {
     $this->view->headLink()->appendStylesheet($this->baseUrl().'/js/dojo-1.7/dojo/resources/dojo.css');    
}

you can use setStylesheet() if you need to override the current stylesheets or you can use prependStylesheet() or appendStylesheet().

Good Luck...