5

I'm creating joomla template using blank joomla starter template. The blank joomla template has a file called template.css.php that combines and compresses multiple template css files into one single file output for lowering http request. This template.css.php can be accessed directly from browser (direct access) and will return compressed css code output.

For some reasons I need to call my template parameters from the inside of template.css.php but when I try to get template parameters, the file template.css.php is no longer available for direct access. It will return a blank page.

Any idea how to get this work?

Here is the modified version of my template.css.php file :

<?php
    /* modified code START */

    require_once ( '../../../includes/defines.php' );
    require_once ( '../../../includes/framework.php' );

    $app            = JFactory::getApplication();
    $doc            = JFactory::getDocument();
    $params = $app->getParams();
    $pageclass      = $params->get('pageclass_sfx');

    /* modified code END */

    // initialize ob_gzhandler to send and compress data
    ob_start ("ob_gzhandler");
    // initialize compress function for whitespace removal
    ob_start("compress");
    // required header info and character set
    header("Content-type:text/css; charset=UTF-8");
    // cache control to process
    header("Cache-Control:must-revalidate");
    // duration of cached content (1 hour)
    $offset = 60 * 60 ;
    // expiration header format
    $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s",time() + $offset) . " GMT";
    // send cache expiration header to broswer
    header($ExpStr);
    // begin function compress
    function compress($buffer) {
        // remove comments
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
        // remove tabs, spaces, new lines, etc.
        $buffer = str_replace(array("\r\n","\r","\n","\t",'  ','    ','    '),'',$buffer);
        // remove unnecessary spaces
        $buffer = str_replace('{ ', '{', $buffer);
        $buffer = str_replace(' }', '}', $buffer);
        $buffer = str_replace('; ', ';', $buffer);
        $buffer = str_replace(', ', ',', $buffer);
        $buffer = str_replace(' {', '{', $buffer);
        $buffer = str_replace('} ', '}', $buffer);
        $buffer = str_replace(': ', ':', $buffer);
        $buffer = str_replace(' ,', ',', $buffer);
        $buffer = str_replace(' ;', ';', $buffer);
        $buffer = str_replace(';}', '}', $buffer);

        return $buffer;
    }

    // uikit.css
    require('uikit.min.css');
    require('uikit/slider.css');
    require('uikit/slidenav.css');

    // template css

    require('template.css');

    // load system stylesheets for system messages and default buttons
    require('../../../media/system/css/system.css');
    require('../../system/css/system.css');
    require('../../system/css/general.css');


    ?>
  • 1
    You've got my upvote simply because you're using UIKit :) – Lodder Aug 18 '15 at 13:33
  • lol thanks @Lodder. Any idea for my question? – webchun Aug 18 '15 at 13:35
  • Is this template something you wish to release to the public or is it for your own website? If for the public, I would suggest you don't combine and minify all the CSS as users may want to change some of the styling. – Lodder Aug 18 '15 at 13:36
  • It's for public/client. User will be still able to modify the css files. The template.css.php only combines css files into one file and compresses the output. So when user modify the source css files then template.css.php will be changed too. – webchun Aug 18 '15 at 13:41
  • Then that means each time the template is loaded in the front-end, all the CSS must be combined and minified. I may also prevent users from being able to using caching but can't confirm that – Lodder Aug 18 '15 at 13:50
1

This will work for you with Joomla 3.

<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', '../../../..' );//point to joomla root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$app = JFactory::getApplication('site');
$app->initialise();
$template = $app->getTemplate(true);
$params = $template->params;

header("Content-Type:text/css; charset=UTF-8");
header("Cache-Control:must-revalidate");
$offset = 60 * 60 ; // 1 hour
$ExpStr = "Expires: " . gmdate("D, d M Y H:i:s",time() + $offset) . " GMT";
header($ExpStr);
ob_start("compress");
function compress($buffer) {
 $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
 $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  '), '', $buffer);
 $buffer = str_replace(array(', ', ' {'), array(',', '{'), $buffer);
 return $buffer;
}
?>
body {background-color: <?php echo $params->get("templateBackgroundColor"); ?>;}
<?php ob_end_flush(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.