1. 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 ?

Discussion in 'XenForo Development Discussions' started by Zephyr, Apr 2, 2013.

  1. Zephyr

    Zephyr Well-Known Member

    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 :)
     
  2. cclaerhout

    cclaerhout Well-Known Member

    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 likes this.
  3. Zephyr

    Zephyr Well-Known Member

    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.
     
  4. cclaerhout

    cclaerhout Well-Known Member

    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.
     
  5. Zephyr

    Zephyr Well-Known Member

Share This Page