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

How do I perform simple addition in a template?

Discussion in 'XenForo Questions and Support' started by Sean Kendle, Feb 3, 2016.

  1. Sean Kendle

    Sean Kendle Member

    Summary: How do I perform simple addition in a template?

    Hi, Brogan. I'm trying to insert an ad between X threads in my forum in the threads list page.

    I created a template modification for thread_list, and with the help of another post wrote this code:

            <xen:set var="$threadPosition">0</xen:set>
            <xen:foreach loop="$stickyThreads" value="$thread" i="$i">
                <xen:include template="thread_list_item" />
                    <xen:set var="$threadPosition">$threadPosition + 1</xen:set>
            <xen:include template="ad_thread_list_below_stickies" />
            <xen:foreach loop="$threads" value="$thread" i="$i">
                <xen:include template="thread_list_item" />
    <!-- insert Ads every N threads: -->
                <xen:if is="{$i} == 3">
                    <xen:include template="ads_in_content_1" />
                    <xen:include template="ads_mobile_in_content_1" />
                <xen:if is="{$i} == 10">
                    <xen:include template="ads_in_content_2" />
                    <xen:include template="ads_mobile_in_content_2" />
            <!-- THREAD POSITION TEST: {$i} -->     
    I just spent about half an hour searching google and this site, and I can't figure out any way to add 1 to a number within a template. (Or, add two variables together within a template, generally speaking, because I assume I'll need to do that again later.)

    I have created a new variable, but would like to simply add 1 to that variable for each "sticky" and "normal" thread, since the variable $i resets for the second FOR loop, and I want to treat Sticky threads the same as normal Threads as far as inserting an ad below them.

    I've tried the bold lines above for creating an adding a variable, but trying to do this results in concatenating a string, rather than adding 1 to the number.

    Can you please help me with this?
  2. Brogan

    Brogan XenForo Moderator Staff Member

    It's off topic for this thread really but you need to use xen:calc to perform calculations in templates.
  3. Sean Kendle

    Sean Kendle Member

    Thanks. Is there a link to actual documentation explaining how that works?

    How is this off topic for a thread titled "Using variables in templates"?

    The biggest drawback for using Xenforo I've found has been a lack of centralized documentation for simple things like this. If I've missed it somehow, please direct me to it.
  4. Brogan

    Brogan XenForo Moderator Staff Member

    Because xen:calc isn't a variable, it's a XenForo tag which allows you to carry out certain PHP functions.

    I'm not aware of any specific documentation for it.
  5. Sean Kendle

    Sean Kendle Member

    I know the difference between a variable and a function. I'm trying to use a variable in a template. Variables, by nature, are not always read-only. Those are constants. Generally, in programming, it's useful to be able to change a variable's value, including simple mathematics.

    The fact that there are no posts in this forum explaining how to do that, and that you provide no Official Documentation means I have to rely on asking questions. Many of the threads here are extremely old, and you just now posted a thread about USING VARIABLES IN TEMPLATES today, so rather than exhume some 3+ year old thread, I thought I'd ask you.
  6. Brogan

    Brogan XenForo Moderator Staff Member

    I have split the posts off to a new thread as your specific question is "How do I perform simple addition in a template?".

    The answer is to use xen:calc, like so:
    {xen:calc '{$something} + 1'}
    These are the functions you can use with xen:calc
    • abs
    • ceil
    • floor
    • max
    • min
    • pow
    • round

Share This Page