简体   繁体   English

覆盖joomla template.php主体背景颜色(站点背景)

[英]override joomla template.php body background color (site background)

I am using joomla 2.5 with an artisteer template, I have written a custom component which displays and sorts a list of products, the products themselves are also a part of the component and are saved in a database. 我正在将joomla 2.5与Artisteer模板一起使用,我编写了一个自定义组件,该组件显示并排序产品列表,产品本身也是该组件的一部分,并保存在数据库中。 what I am trying to do is set a different body background for specific products or product categories according to an if condition 我想做的是根据if条件为特定产品或产品类别设置不同的身体背景

if (product_category == 1):
   <body id="background1">
   ...

but the problem is that the <body id="someID"> is loaded in the template.php before the component is loaded, so even if I set a body background in the component, the background will not change. 但是问题在于<body id="someID">在加载组件之前已加载到template.php中,因此即使我在组件中设置了主体背景,背景也不会改变。 however, if I delete the body css id decleration from the template.php and leave it as <body> instead of <body id="someID"> , then the component <body id...> declaration sets the background correctly, but then any other page which is not related to the component has a blank white background. 但是,如果我从template.php中删除body css id decleration,并将其保留为<body>而不是<body id="someID"> ,则组件<body id...>声明会正确设置背景,但是那么与该组件无关的任何其他页面的背景都是空白。

what I think I need to do is find a way to set a default background in case a background is not defined elsewhere, or somehow override the current background and make to component load the body background again, I am relativly new to php and css but with a little help and I guidence I think I could find my way :) any help would be very much appreciated!!! 我想我需要做的是找到一种方法来设置默认背景,以防万一没有在其他地方定义背景,或者以某种方式覆盖当前背景并使组件再次加载正文背景,我相对来说是php和css的新手,但是有了一点帮助,我的指导下,我想我可以找到我的方法:)任何帮助将不胜感激! thanks! 谢谢!

Try doing something like this: 尝试做这样的事情:

<?php
$doc = JFactory::getDocument();
  if(product_category == 1){
     $css = 'body {'
           .'background: url(' . JURI::root() . 'components/com_yourcomponent/file.jpg) !important;'
           .'}';
     $doc->addScriptDeclaration($css);
  }    
?>

You will need to change the path of the background to whatever suit your needs. 您将需要更改背景的路径以适合您的需要。

Hope this helps 希望这可以帮助

In template.php file, before body is loaded, you can do like this 在template.php文件中,在加载主体之前,您可以像这样

if (JRequest::getCmd('option') == 'com_mycontent'){
    echo '<body id="myid">';
}
else
    echo '<body id="anotherid">';

Hi @marko and @Lodder thank you so much for your reply! 嗨@mar​​ko和@Lodder非常感谢您的回复! Unfortunately the solutions you offered did not work because the stylesheet is loaded prior to the component so any new design changes from the templae code will not affect the template design. 不幸的是,您提供的解决方案无法正常工作,因为样式表是在组件之前加载的,因此模板文件中任何新的设计更改都不会影响模板设计。 and unforunately since the template.php cannot initialize the component veriables (Category_id for example) I cannot create a condition and check what is the category_Id and change the background accordingly... I found a simple solution which is implementing this code inside my component code: 不幸的是,由于template.php无法初始化组件Veriable(例如,Category_id),因此我无法创建条件并检查category_Id是什么,并相应地更改背景...我找到了一个简单的解决方案,该解决方案在我的组件代码中实现了此代码:

app =& JFactory::getApplication();

if ($category_id == 1) $app->setTemplate("template2"); 如果($ category_id == 1)$ app-> setTemplate(“ template2”);
else .... 否则..

$this simply sets a new template according to the category_id, I know this is not memory efficient and the loading time is longer and the worst part is that I need to create a new template for each category... but this does the trick... I'd appreciate if you could think of any better solution? $ this只是根据category_id设置了一个新模板,我知道这不是高效的内存,并且加载时间更长,最糟糕的部分是我需要为每个类别创建一个新模板...但这确实有用。 ..如果您能想到更好的解决方案,我将不胜感激。

Thank you so much guys! 十分感谢大家!

Actually, you could just use the built in page class suffix parameter in the menu item to accomplish what you want. 实际上,您可以仅使用菜单项中的内置页面类后缀参数来完成所需的操作。 If you have a custom template, add this to it: 如果您有自定义模板,请将其添加到其中:

<?php
$app = JFactory::getApplication('site');
$params =  & $app->getParams('com_content');
$pageclass = trim($params->get('pageclass_sfx'));
?>
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">

Most of the reputable template devs include code for the page class suffix, so you may not need to do anything other than add something in the parameter of the page you want to customize. 大多数信誉良好的模板开发人员都包含页面类后缀的代码,因此除了在要自定义的页面参数中添加某些内容外,您可能不需要执行任何其他操作。

By adding an ID to the body tag, this insures that you can easily and accurate override any CSS on the page. 通过将一个ID添加到body标签,可以确保您可以轻松,准确地覆盖页面上的所有CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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