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

Discussion in 'XenForo Development Discussions' started by Marcus, Dec 12, 2012.

  1. Marcus

    Marcus Well-Known Member

    <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?
    Sean Kendle likes this.
  2. cclaerhout

    cclaerhout Well-Known Member

        <xen:set var="$test.1">abc</xen:set>
        <xen:set var="$test.2">def</xen:set>
        <xen:foreach loop="$test" value="$item" i="$i" count="$total">
            {$i}/{$total}:{$item}<br />
    Is it what you want to do?

  3. Marcus

    Marcus Well-Known Member

    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.

    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:
     Position Title USD<br />
    loop through pizzas >
    $i title sum
    $i Sum before tax | ..
    $i Tax | ..
    $i SUM > ..
    Any ideas how to increase the $i variable?
  4. ManOnDaMoon

    ManOnDaMoon Well-Known Member

    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.
    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);
    And use the following in "your_template" :
     Position | Title | USD<br />
    <xen:foreach loop="$invoice" value="$line" i="i">
    $i | {$line.title} | {$line.value} <br />
  5. Marcus

    Marcus Well-Known Member

    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.
    Sean Kendle likes this.
  6. Chris D

    Chris D XenForo Developer Staff Member

    {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.
  7. Marcus

    Marcus Well-Known Member

    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>

Share This Page