Jump to content

Add a dynamic CSS file with PHP


PierreC
 Share

Recommended Posts

Hi,

 

I'm currently writing an override for the CategoryController and I'm having issues adding a dynamically generated CSS file. My goal is to make subcategories icons' width adapt to the number of subcategories. In order to do that, I'm using a little "trick" that have worked in the past : I use a PHP file and declare the content as "text/css" in the header. Here is my php file dyn_categories.php :

<?php
header( 'content-type: text/css' );

$cat_count = $_GET['count'];
$width = 100/$cat_count.'%';
?>

#subcategories ul li {
    width: <?php echo $width; ?>;
}
?>

In my CategoryController override, I use this line in order to include the file :

$this->addCSS(array(
    _THEME_CSS_DIR_.'dyn_categories.php?count='.$nb_subcategories => 'all',
));

The string for the file name is good, as a simple "<?php echo _THEME_CSS_DIR_.'dyn_categories.php?count='.$nb_subcategories; ?> prints the expected path. However, the file is not included and the string "dyn_categories" cannot be found in the source of the generated page.

 

I tried to understand the addCSS and addMedia methods of FrontController and Controller, but I don't find any explanation.

 

Can anyone help me on this.

Thanks a lot

Share this post


Link to post
Share on other sites

I don't know how to remove my post, but I just misunderstood a little detail and just found the solution by myself :

$this->addCSS(array(
    _THEME_CSS_DIR_.'dyn_categories.php?count='.$nb_subcategories => 'all',
), 'all', null, false);

The last parameter of 'addCSS' is checkpath, and if set to true (its default value) the files won't be included as there is no such file as 'dyn_categories.php?count=X'

 

Sorry for the useless post

Edited by PierreC (see edit history)

Share this post


Link to post
Share on other sites

Hi welcome to the forum first,

 

and why do delete ? It is not useless post, someone could search just for that,

so thanks for sharing your solution.

 

And for your problem , probably could done in different ways. I was thinking that module

blockreinsurense have something similar

 

https://github.com/PrestaShop/blockreinsurance/blob/master/blockreinsurance.tpl

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More