TWIG: What is wrong with this recursive iteration - php

I've made a recursive function in TWIG using a macro. This macro is supposed to count how many times it can find description in a nested array and return this.
Can anbody help me in solving what's going wrong. The fiddle can be found at:
https://twigfiddle.com/5uskoi
The result as you can see there is 4 while it should be 6.
Thanks for your time!
Regards,
Jasper
the TWIG code:
{% macro countArray(item) %}
{% import _self as self %}
{% set total = 1 %}
{% for yesItem in item.yes.items %}
{% set total = total + self.countArray(yesItem) %}
{% endfor %}
{% for noItem in item.no.items %}
{% set total = + total + self.countArray(noItem) %}
{% endfor %}
{{ total }}
{% endmacro %}
{% from _self import countArray %}
{% for item in data.items %}
{{ countArray(item) }}
{% endfor %}
with this data:
data:
items:
- description: '1'
yes:
items:
- description: '2'
- description: '3'
no:
items:
- description: '4'
yes:
items:
- description: '5'
- description: '6'

Found the solution to this problem. I have added |trim after the macro call. This trims all the whitespace and other stuff that got returned with the number.
This caused the problem of adding them together.
The twigfiddle has been updated with this settings for further reference!
Regards,
Jasper

Related

How to run For loop in twig file in php opencart to make the results should be like below

Suppose,
$a=1,2,3,4,5,6,7,8,9,10
then
in the for loop, it should run 1,2,3,4
and then 5 then 6,7,8,9 and then 10
then exit the loop.
You could use the batch and slice Twig filters for this. If you batch the results by groups of 5, then slice the first 4 and the 5th element you are able to loop through the results in the way you want.
Example code:
{% set items = [1,2,3,4,5,6,7,8,9,10] %}
{% for batch in items|batch(5) %}
<p>item 1 through 4</p>
{% for item in batch|slice(0, 4) %}
{{ item }}
{% endfor %}
<p>5th item</p>
{% for item in batch|slice(4, 1) %}
{{ item }}
{% endfor %}
{% endfor %}

Storing HTML in a Twig Array

Ok I'm trying to add HTML code to a Twig array and ending up into problems. I'm not the most knowledgable when it comes to Twig so I'll need help. I'm getting this error when I try: Fatal error: Uncaught Twig_Error_Syntax: Arguments must be separated by a comma.
What am I doing wrong? https://pastebin.com/gEGLnCid
{% set myArray= [] %}
{% for product in products %}
You have to do it in two steps e.g.
{% set arr = [] %}
{% for i in 1..10 %}
{% set foo %}
{{ i * 10 }}
foo
bar
foobar
{{ i }}
{% endset %}
{% set arr = arr | merge([ foo, ]) %}
{% endfor %}
{% for val in arr %}
{{ val }}
{% endfor %}

Print Twig variable x times based upon randomized range

I am using Twig and Timber for a WordPress project. I have the following loop in my template that prints my custom post type titles into a HTML structure.
{% for company in companies %}
{% set dot = "<div class='company-dot'></div>" %}
{% set range = range(10, 20) %}
{{dot}}
{{random(range)}}
<div class="company">
<div class="company-dot dot-active"></div>
<p class="dot-caption">{{ company.title }}</p>
</div>
{% endfor %}
I would like to print my {{dot}} variable x amount of times based upon the number that is generated by {{random(range)}}. How can I do this?
The simplest solution would be to iterate random(range) times with a for loop:
{% for i in 0..random(range(10, 20)) %}
{{ dot }}
{% endfor %}
I don't really know Twig but my guess is that you could to the following:
{% for i in random(range) %}
{{dot}}
{% endfor %}
You already know how to use range, just use it again :
{% set dots_count = random(range) %}
{% for dot_index in range(1,dots_count) %}
{{dot}}
{% endfor %}
{{dots_count}}
Here is a fiddle : https://twigfiddle.com/ko595z

Getting value of array object in Twig file Symfony

I have following set of an array that is pass to twig file.
others ={
0:{id : 10, name: krist},
1:{id : 20, name: ryan}
}
When I retrieve back from twig file, I retrieve as follow.
{% for other in others %}
{{other.id}}
{% endfor %}
The above method always shows the error as follow:
An exception has been thrown during the rendering of a template
("Notice: Array to string conversion").
How can I fix this issue?
You can try this.
{% for other in others %}
{% for o in other %}
{{o.id}}
{% endfor %}
{% endfor %}
or
{% for other in others %}
{{other[0].id}}
{% endfor %}

Twig: Update variable located earlier in code

I need to show a value at the top of a page, which needs to be updated after I have run some Twig loops in the middle of the page.
Here's an example:
<div>Total Amount: {{ totalAmount }}</div>
{% for product in products %}
{% set totalAmount = totalAmount + product.amount %}
{% endfor %}
I could compute the value in the Symfony controller, but there are certain reasons why I would prefer doing this in the Twig template.
Is the above possible with Twig? I'm sure the Twig code is executed sequentially and I may have to find a different solution. But I'd like to see if anyone has any suggestions.
Thanks,
JB
Use blocks.
In your main template (layout):
<div>Total Amount: {% block totalAmount %}{% endblock %}</div>
In your child template:
{% for product in products %}
{% set totalAmount = totalAmount + product.amount %}
{% endfor %}
{% block totalAmount %}{{ totalAmount }}{% endblock %}

Categories