简体   繁体   中英

Joomla dynamic stylesheet using template parameters in a css.php file

I am trying create a template with a dynamic css.php file. I have spent the entire night googling for a solution to calling the joomla object class in a php file being used as a css file. I know Ive seen this done before, i just never paid attention to how it was accomplished. This is what I have so far.

Note - I do not want to use addstyledecloration as it is too cumbersome when dealing with more than a few params *

index.php:

<?php                               
defined('_JEXEC') or die;
require($this->baseurl.'templates/'.$this->template.'/includes/config.php');
?>

<!DOCTYPE html>
<html lang="en" xmlns:fb="http://ogp.me/ns/fb#">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <jdoc:include type="head" />
</head>

config.php:

<?php
defined('_JEXEC') or die;
//joomla configuration
JLoader::import('joomla.filesystem.file');
JHtml::_('jquery.framework', false);
JHtml::_('bootstrap.framework');
$app = JFactory::getApplication();
$config = JFactory::getConfig();
$doc = JFactory::getDocument();
$template_path = $this->baseurl.'/'.'templates'.'/'.$this->template;
$jui_path = $this->baseurl.'/media/jui';

$doc->addStyleSheet($jui_path.'/css/bootstrap.min.css');
$doc->addStyleSheet($jui_path.'/css/bootstrap-responsive.min.css');
$doc->addScript($jui_path.'/js/bootstrap.min.js');
$doc->addStyleSheet($template_path.'/css/template.css');
$doc->addStyleSheet($template_path.'/includes/template-css.php');
$doc->addScript($template_path.'/js/template.js');
?>

template-css.php:

<?php
header("Content-type: text/css");
?>

body {background-color: #000;}
body {background-color: <?php $this->params->get('body') ?>;}

You have to load the whole joomla framework as you are in a request outside of your Joomla environment. How to load Joomla can be found here JFactory failing to import . Now you can select the template parameters trough a database select or load more classes to get the template object (not sure how to do this, don't have my dev laptop around).

You must first add a color picker in your templatedetails.xml which contains the template parameters
<field name="themecolor" type="color" description="Color picker" label="Template Color"/> Then you must declare the variable in your config.php file

$themecolor = $this->params->get('themecolor');
$_SESSION['themecolor'] = $themecolor;

Now you will be able to use this variable in your css.php file

body {background-color: <?php echo  $_SESSION['themecolor']; ?>}

You can read more about template parameters here https://docs.joomla.org/Understanding_Joomla!_templates#Parameters

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM