0
votes

Could someone please advise me on the current methods available for using PHP in a CSS file in CakePHP 2.x

I have currently separated my stylesheet into a php file and wish to parse my data via the URL but I cannot seem to work out how to link it using

$this->Html->css('dynamic-stylesheet')  

as it always appends .css to the name.

Your help is much appreciated.

2

2 Answers

3
votes

You can generate the tag by using HtmlHelper::meta()

echo $this->Html->meta(array(
    'link' => '/css/test.php',
    'rel' => 'stylesheet',
    'type' => 'text/css'
));

Note that you need to explicitly direct it to the /css directory, as you are no longer using a helper method specifically for CSS.

1
votes

Looking at the Html helper file in the CakePHP library located at lib/Cake/View/Helper/HtmlHelper.php, check out line 427, looks like the .css extension is set automatically unless there are two slashes in the CSS file name. I GUESS that is to catch external resources?

From the aforementioned library file:

if (strpos($path, '//') !== false) {
    $url = $path;
} else {
    $url = $this->assetUrl($path, $options + array('pathPrefix' => CSS_URL, 'ext' => '.css'));

    if (Configure::read('Asset.filter.css')) {
        $pos = strpos($url, CSS_URL);
        if ($pos !== false) {
            $url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(CSS_URL));
        }
    }
}

So this..

echo $this->Html->css('http://whatever.com/css/dynamic-stylesheet.php');

Would render the dynamic-stylesheet.php file rather than defaulting to 'dynamic-stylesheet.css.

Not sure that is what this was intended for but maybe that file will help you.