views:

691

answers:

1

I would like to write an internal style sheet to a view in Zend Framework as such

<head>
   <style type="text/css" media="all"> 
      body{ background: #FFFFFF; }
   </style>
</head>

I understand that I can write an external style sheet using $this->view->headLink()->appendStylesheet('style.css');

However I cannot find a way to write an internal style sheet. Any ideas?

+9  A: 

What you are looking for is called the HeadStyle view helper. Its manual documentation can be found here.

The HeadStyle helper's API is consistent will all the Head* view helpers, and works as such (the following assumes you are in a viewscript):

// Putting styles in order: 
// These methods assume the a string argument containing the style rules.

// place at a particular offset:
$this->headStyle()->offsetSetStyle(100, $customStyles);

// place at end:
$this->headStyle()->appendStyle($finalStyles);

// place at beginning
$this->headStyle()->prependStyle($firstStyles);

// Or capturing a block of styles

<?php $this->headStyle()->captureStart() ?>
body {
    background-color: <?php echo $this->bgColor ?>;
}
<?php $this->headStyle()->captureEnd() ?>

Note that you do not include the <style> tags in any of this input. That is generated by the helper itself. Then, in your layout, simply echo the helper where you'd like its output:

<head>
    <?php echo $this->headLink() ?>
    <?php echo $this->headStyle() ?>
</head>
jason
Thanks. $this->headStyle()->captureStart() and $this->headStyle()->captureEnd() was just what I was looking for.
Marcel Tjandraatmadja