• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How use php in template ?

Zephyr

Well-known member
#1
Hello, how use php in template please ?
Exemple :
PHP:
<?php
 
$rand = rand(1, {$xenOptions.randomNumber});
 
if($rand = 1) {
    <style>body{background-image: {$xenOptions.background1}}</style>
}
elseif($rand = 2) {
    <style>body{background-image: {$xenOptions.background2}}</style>
}
elseif($rand = 3){
    <style>body{background-image: {$xenOptions.background3}}</style>
}
 
echo 'ok !';
Thanks :)
 

cclaerhout

Well-known member
#2
Hello, how use php in template please ?
Exemple :
PHP:
<?php
 
$rand = rand(1, {$xenOptions.randomNumber});
 
if($rand = 1) {
    <style>body{background-image: {$xenOptions.background1}}</style>
}
elseif($rand = 2) {
    <style>body{background-image: {$xenOptions.background2}}</style>
}
elseif($rand = 3){
    <style>body{background-image: {$xenOptions.background3}}</style>
}
 
echo 'ok !';
Thanks :)
You must bring a variable from php to your templates. But for a random function using the XenForo template syntax, see the code of Jake here.
 

Zephyr

Well-known member
#3
Hello, I just tried, it works in one view but not in the css?

Code:
<xen:set var="$banners.1">{$xenOptions.XenCrea_Scroll_RandomImage1}</xen:set>
<xen:set var="$banners.2">{$xenOptions.XenCrea_Scroll_RandomImage2}</xen:set>
<xen:set var="$banners.3">{$xenOptions.XenCrea_Scroll_RandomImage3}</xen:set>
<xen:set var="$banners.4">{$xenOptions.XenCrea_Scroll_RandomImage4}</xen:set>
<xen:set var="$banners.5">{$xenOptions.XenCrea_Scroll_RandomImage5}</xen:set>
<xen:set var="$banners.6">{$xenOptions.XenCrea_Scroll_RandomImage6}</xen:set>
<xen:set var="$banners.7">{$xenOptions.XenCrea_Scroll_RandomImage7}</xen:set>
 
html.Public
{
    background: url('{xen:raw '$banners.{xen:calc '({$serverTime} % 7) + 1'}'}') !important;
}

This is always the first background is displayed.
 

cclaerhout

Well-known member
#4
Hello, I just tried, it works in one view but not in the css?

Code:
<xen:set var="$banners.1">{$xenOptions.XenCrea_Scroll_RandomImage1}</xen:set>
<xen:set var="$banners.2">{$xenOptions.XenCrea_Scroll_RandomImage2}</xen:set>
<xen:set var="$banners.3">{$xenOptions.XenCrea_Scroll_RandomImage3}</xen:set>
<xen:set var="$banners.4">{$xenOptions.XenCrea_Scroll_RandomImage4}</xen:set>
<xen:set var="$banners.5">{$xenOptions.XenCrea_Scroll_RandomImage5}</xen:set>
<xen:set var="$banners.6">{$xenOptions.XenCrea_Scroll_RandomImage6}</xen:set>
<xen:set var="$banners.7">{$xenOptions.XenCrea_Scroll_RandomImage7}</xen:set>
 
html.Public
{
    background: url('{xen:raw '$banners.{xen:calc '({$serverTime} % 7) + 1'}'}') !important;
}

This is always the first background is displayed.
Since the css template are "heavily cached" (quotation from xenForo team - I forgot who), the variable $serverTime is probably not available in css template. But you probably can write several css templates and use the random code in the normal template. The <xen:require css="xxx.css" /> should accept variable, check this first.