page.collection() is empty despite having child pages - twig

I'm using Masonry theme and trying to show all posts in a collection. Masonry does this normally and I had it working previously, but now page.collection is empty despite having pages. I've made sure that the children are routable and visible. I even reinstalled the theme, to no avail. Any ideas? At this point I haven't actually modified any code that would affect that page logic relating to the collections.
At the top of the file is:
{% set collection = page.collection() %}
I used
{{dump(collection)}}
But nothing shows up. What gives?

I changed page.collection to page.children instead. It accomplishes the same thing without having to add extra header content on my page. Thanks to HungTran for pointing out the correct way though.

If collection() is indeed a method, then you would use this in Twig instead:
{% set collection = page.collection %}
Can you try that. I'm not certain it will work, but let us know the result.

Related

How to run an object's method in twig without rendering what it returns [duplicate]

I have seen the twig documentation about do tag, but I don't understand its use/useful.
The docs says the follow:
The do tag works exactly like the regular variable expression ({{ ...
}}) just that it doesn't print anything:
and show an example:
{% do 1 + 2 %}
What to solve exactly this tag ?
Good question! I found a link on GitHub to when this was proposed which might add some further info:
Sometimes you want to do things, or call some stuff, and ignore the output. For example if you use a |shift filter to remove some data from an array, doing {{ arr|shift }} will output the removed item, which is not always desirable.
Of course it's possible to do {% set null = arr|shift %}, which won't
output anything, but it also looks weird.
The example in the docs is poor as it explains nothing at all, as you pointed out.

Cloning Shopware 6 CMS Element

Shopware 6 | Cloning CmsElement and get null as data,
and in the Shopware 6 Forum: https://forum.shopware.com/discussion/67124/custom-product-box-no-data-in-the-storefront
I found this answer, but as I am still learning Shopware 6, I don't know if this is relevant for me. I have cloned the CMS Product Box Element, as I need to change the layout and add more flags/badges, but when I do a dump I get element.data = null. Would I also need to create a resolver too, or should I have just tried to extend the element with {% sw_extends '#Storefront/storefront/element/cms-element-product-box.html.twig' %}? But if I just extend I can't make the changes I want to the component.
Any suggestions or hints on how to solve the problem would be appreciated. Thanks.
I figured out it was much simpler than creating a new element. In the end, I found which components were being included and found that to change the layout and add a new badge I just needed to {% sw_extends '#Storefront/storefront/component/product/card/badges.html.twig' %} and for the parts of the product-box, just find the corresponding component and make the changes I needed to make in my plugin.

what exactly is haswidgets and widgets in bolt (or twig rather )?

I was just going through the Default theme of bolt CMS, and i came actoss the following lines of code:
{% if haswidgets('aside_top') %}
{{ widgets('aside_top') }}
{% else %}
I googled twig haswidgets and also twig widgets , but i could't find anything.
can somebody explain what these two methods are ? and what exactly do they do ?
They are a feature of Bolt. Extensions can push content to specific places in the backend and also to some places in the frontend, as long as the theme supports that. They are called widgets. the haswidgets() and widgets() twig functions are for checking and displaying them.
You can find more info here https://docs.bolt.cm/3.1/templating/widgets and here https://docs.bolt.cm/3.1/extensions/intermediate/widgets

Passing string variables through include in twig and slim framework

I am using Slim Framework and Twig.
I want to apply the DRY by using partials. I have a form that is reused in several views with different variables such as titles and route (url) names.
I am struggling on how to make it work on url names.
For example, there is this link using the ´urlFor´ helper with parameter as follows inside a view:
The link
So that is the link I want to pass to the partial template since, it is different in each view, I want to use the partial form. I have tried several approaches but it does not work. I don't know how to pass this string containing ' inside.
For example, I have tried this partial call inside the parent view like this:
{% include 'partials/partial.php' with {'theUrl': "urlFor('route.name', {parameter: value})"} %}
And inside the partial like this:
Show more
It does not work because in the browser's url I see the following:
http://myproject.dev/pages/urlFor('route.name',%20%7Bparameter:%201%7D)
It looks like it is not being escaped properly. Any ideas how to fix this when passing route names with urlFor()?
Without the greatest possibility for testing i think i found the problem. You are simply adding the function inside a string and that does not give the parser an oportunity to resolve the method, because it's basicly just a string.
{% include 'partials/partial.php' with {'theUrl': "urlFor('route.name',{parameter: value})"} %}
Should be
{% include 'partials/partial.php' with {'theUrl': urlFor('route.name',{parameter: value})} %}

How do I display a block in Twig if there is data passed to it?

I have a page in a project I'm working on where I'm having a bit of a problem with.
The page displays two sets of results. The first set of results displays the information for the a project that has just been created. The second set of results displays all the reviews and revisions for this project. This is where I'm having the problem.
When the project is created, there are no reviews created. However, because I have set the page to display reviews related to the selected project, it is expecting to retrieve reviews for the selected project. As there are no reviews to retrieve (because they haven't been added yet) the page fails and gives an ambiguous 500 error.
Now, I have seen somewhere that Twig allows if statements.But, I can't seem to work out how to make this work in my project.
Does anyone know how you would use an if statement in Twig in conjunction with a MySQL result?
I've "Twigged" it (I'm here all week)
{% if reviews == null %}
Stuff to display if the result is null
{% else %}
Stuff to display if the result isn't null
{% endif %}
The loop is then inserted inside this if statement.

Resources