【问题标题】:Using css variables with php in joomla 3 template在 joomla 3 模板中使用 css 变量和 php
【发布时间】:2014-09-07 20:28:19
【问题描述】:

我的想法是在 joomla 中使用模板参数来设置背景、文本或按钮的颜色。 我想定义一个 CSS 类,而不是每次都使用 style="background-color:<?php echo $buttoncolor ?>;"因为我不想写无数的模板覆盖。

我相信这可能是非常有用的功能。

我的 templateDetails.xml 中的参数看起来像

<field name="buttoncolor" type="color" default="#309000"
                    label="TPL_BUTTON_COLOR_LABEL"
                    description="TPL_BUTTON_COLOR_DESC" />

我在 CSS-Tricks找到的如何在css中使用php变量的想法

要在我的 index.php 中链接到这个文件的 css 中使用 php 变量

<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/style.php" type="text/css"  />

我的 style.php 看起来像:

<?php 
header("Content-type: text/css; charset: UTF-8");
$buttoncolor = $this->params->get("buttoncolor");
?>

.buttoncolor {background-color: <?php echo $this->params->get('buttoncolor'); ?>;}

通常我在 index.php 中将变量设置为模板参数,例如

<?php 
//parameter
$app = JFactory::getApplication();
$doc = JFactory::getDocument(); 
$params = $app->getParams();
$buttoncolor = $this->params->get('buttoncolor'); 
?>

不幸的是,我收到以下错误: 致命错误:在 style.php 中不在对象上下文中时使用 $this

非常感谢任何想法、解决方法和帮助!

【问题讨论】:

    标签: php css templates joomla3.0


    【解决方案1】:

    请尝试删除$this。您的代码必须是:

      $app = JFactory::getApplication();
      $template = $app->getTemplate(true);
      $params = $template->params;
      $buttoncolor = $params->get("buttoncolor");
    

    Joomla 有一个特定的方式来设置css style。你必须这样做:

      $document = JFactory::getDocument();
      $style = ".buttoncolor {background-color: ".$buttoncolor."}"; 
      $document->addStyleDeclaration($style);
    

    祝你好运!

    【讨论】:

    • 您好,非常感谢您的解决方案。如果我理解一切正确,那么就没有理由包含 style.php 它运行良好,我从没想过解决方案会这么简单。再次感谢。
    • 当然这种方式不需要额外的文件包含。不客气!
    猜你喜欢
    • 2012-03-27
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多