tags:

views:

21

answers:

1

From what I can see, CakePHP makes it easy to link to a CSS file in a view with the following:

echo $html->css('my-css-filename',null,array(),FALSE);

But what if I don't want to exclusively use hardcoded files? How can I get it to create a style tag with some dynamically generated rules in e.g.

<style type="text/css" media="all">p {font-size:1.5em}</style>

I am trying to do this in a view file, I'd like the CSS to be placed in the head tag, and I'm using CakePHP 1.2.7

+1  A: 

I'd just pass the variable from the controller to the view as you normally would.

In your controller,

function test() {
 // do some stuff
 $dyn-css = 'p { font-size: 1.5em }';
 $this->set( 'dyn-css', $dyn-css );
}

Then in your layout file:

<head>
 <?php echo $dyn-css; ?>
 // other stuff
</head>
Travis Leleu
Thanks - this would do the job (would have to add <style> tag into layout as well) - although I had hoped CakePHP would have had a more elegant way to do this, like it has with the javascript helper.
Richard