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

xen:calc and xen:set for a $i++ counter

Marcus

Well-known member
#1
<xen:set var="$i">1</xen:set> Result: 1
<xen:set var="$i">{xen:calc '{$i}+1'}</xen:set> Result: 0

I just want to have a counter that counts up in a foreach loop. Any ideas?
 

Marcus

Well-known member
#3
Thanks, this is a wonderful example on how to use that when you have an array. In my case, it's a bit different, though. I need to increase a variable for my invoice table.

Code:
Position | Title | USD
1 | Pizza Margharita | 12.50
2 | Pizza Nepal | 10.50
3 | SUM before tax |  23.00
4 | Tax | 2.3
5 | SUM | 25.30
In the template its this:
PHP:
 Position | Title | USD<br />
< loop through pizzas >
$i | title | sum
$i++ 
</loop>
$i | Sum before tax | ..
$i++
$i | Tax | ..
$i++
$i | SUM > ..
Any ideas how to increase the $i variable?
 

ManOnDaMoon

Well-known member
#4
If you need computing operations in your template, it means you did not follow the MVC pattern. XenForo is designed to use MVC, so computing tools that store values in variables are not available in templates.

What you are trying to achieve here is to build an array within your template. What I would recommend is to build the array directly in your controller action, then pass it to the View (template) in $viewParams in order to use the $i value as proposed by Cédric.
PHP:
public function yourAction()
{
    $invoice = array();
 
    // Lets assume you fetched your $pizzas somewhere above. I hope they were tasty!
    foreach ($pizzas as $pizza)
    {
        $invoiceLine = array("title" => $pizza['title'], "value" => $pizza['price']);
        $invoice[] = $pizza;
    }
 
    $invoice[] = array('title' => "SUM before tax", "value" => your_function_to_calculate_sum_bt($pizzas));
    $invoice[] = array('title' => "Tax", "value" => $taxRate);
    $invoice[] = array('title' => "SUM after tax", "value" => your_function_to_calculate_sum($pizzas, $taxRate));
 
    $viewParams = array("invoice" => $invoice);
 
    return $this->responseView('Your_View_Class', 'your_template', $viewParams);
}
And use the following in "your_template" :
Code:
 Position | Title | USD<br />
<xen:foreach loop="$invoice" value="$line" i="i">
$i | {$line.title} | {$line.value} <br />
</xen:foreach>
 

Marcus

Well-known member
#5
ManOnDaMoon, this is a good approach, I think I will go that route.

But as there is the calc function within the tempate and this function was made just for xenforo and is in use - is there one way to add a simple "1" to a variable? I am really surprised I can't do $i++ within a template.
 

Chris D

XenForo developer
Staff member
#6
{xen:calc $i + 1} should work

But... wait... why do you need to add +1 to the iteration count?

It automatically increases with each iteration. e.g. on the 3rd item in the array $i will = 3.

If you added 1 to it, $i would = 4.
 

Marcus

Well-known member
#7
I finally found the solution of doing a simple $i++.

<xen:set var="$i">{xen:calc '{$i}+1'}</xen:set> will never work, as when a variable is going to be set in the xenforo template, you have to use a different one for its value.

This works:

<xen:set var="$j">{$i}</xen:set>
<xen:set var="$i">{xen:calc '{$j}+1'}</xen:set>
 
#8
I have this code:
<li class="item"></li>
How to loop limit 5 times and <li class = "item active"> first
Results like this:
<li class="item active"></li>
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
help!!