Search code examples
phpperformanceoptimizationseosmarty

How to convert all css links in inline styles using smarty - seo speed optimization


I am trying to make my css links inline ( for a speed optimization ). Here is a sample with a pure php:

echo '<style>';
foreach ($module_css_files as $css_lib) { 
    include(DIR_FS_ROOT. 'htdocs/css/' . $css_lib);
} //foreach
echo '</style>';

But in smarty this idea not work - when I try to

{include file="mysite/htdocs/style.css" } 

each time smarty return white screen no error in php log. If try to include a normal .tpl or small javascript file this is not a problem but if try with css smarty just crashed. Please help!


Solution

  • There are some possible explanations:

    Depending of your configuration, Smarty may be trying to generate the parsed file at htdocs/css/templates_c. If the folder does not exist or is not writable, that will produce an error

    Smarty would try to parse the css as a Smarty template, so i.e.

    .test {overflow:hidden}
    

    would also produce an error. You can try the following:

    add {literal} to your css between comments so it doesn't break the css file if you want to call it instead of using it inline:

    /*{literal}*/
    .test {overflow:hidden}
    ...
    
    /*{/literal}*/
    

    or, if you're using smarty 3, you can just make sure that there is at least one space after every opening bracket so smarty doesn't confuses it with a template instruction

    .test { overflow:hidden}
    

    The best option, however, would be to read the css file contents with php and pass them to smarty as a variable, just to avoid the unnecessary processing time it may take to read and parse the css as a template.