How to auto check checkboxes using Twig template? - php

I want to auto-check check boxes in the HTML. I've managed to get this to work, but it's kinda messy in the template:
<ul>
{% for tag in tags %}
{% set selected = false %}
{% for article_tag in article.tags %}
{% if article_tag.id == tag.id %}
{% set selected = true %}
{% endif %}
{% endfor %}
<li><input type="checkbox" name="tags[]" value="{{ tag.id }}" {% if selected %}checked{% endif %}> {{ tag.name }}</li>
{% endfor %}
</ul>
So the data I'm loading in is like this (in JSON format):
[
'tags' => [
{'id'=> 1, 'name'=>'Travel'},
{'id'=> 2, 'name'=>'Cooking'},
],
'article' => {
'tags' => [
{'id'=> 1, 'name'=>'Travel'},
],
}
]
Also, I'm not using Symfony (I'm using Slim's Twig library) so not sure if Symfony has some stuff in it's framework for doing stuff with Twig. If so, it won't work for me :(

The problem is the article is an array, so or you need to cycle on it for every tags array that contain or you simply access on it of the first element as follow:
{% for article_tag in article[0].tags %}
Instead of:
{% for article_tag in article.tags %}
See the result on this working twigfiddle
Hope this help

Related

How to set a custom class to submenus in Drupal 8?

I am working on sidebar menu in a Custom Drupal 8 Theme. I am trying to set a class of sidebar__menu--submenu-1,sidebar__menu--submenu-2, sidebar__menu--submenu-3 and so on depending on the submenu's level.
So far, I was able to add the class sidebar__menu to the first level & sidebar__menu--submenu to all submenu's level. However, I want to add the 'class' sidebar__menu--submenu-(number of the level) so I can style & control the sidebar better with CSS.
Here it is my code menu.html.twig:
{{ menus.menu_links(items, attributes, 0) }}
{% macro menu_links(items, attributes, menu_level, menu_name) %}
{% import _self as menus %}
{%
set menu_classes = [
'sidebar__menu' ~ menu_name|clean_class,
]
%}
{%
set submenu_classes = [
'sidebar__menu' ~ menu_name|clean_class ~ '--submenu',
]
%}
{% if items %}
{% if menu_level == 0 %}
<ul{{ attributes.addClass('container mx-auto', menu_classes) }}>
{% else %}
<ul {{ attributes.removeClass(menu_classes).addClass(submenu_classes) }}>
{% endif %}
{% for item in items %}
{%
set classes = [
'sidebar__item',
item.is_expanded ? 'sidebar__item--expanded',
item.is_collapsed ? 'sidebar__item--collapsed',
item.in_active_trail ? 'sidebar__item--active-trail',
]
%}
<li{{ item.attributes.addClass(classes) }}>
{{ link(item.title, item.url) }}
{% if item.below %}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
Any help will be really appreciate it!
I have found the answer. First we set the classes:
{%
set submenu_classes = [
'sidebar__menu' ~ menu_name|clean_class ~ '--submenu',
'sidebar__menu--submenu-' ~ (menu_level + 1),
]
%}
Then using the logic like so:
{% if menu_level == 0 %}
<ul{{ attributes.addClass('container mx-auto', menu_classes) }}>
{% else %}
<ul{{ attributes.removeClass(menu_classes, 'sidebar__menu--submenu-' ~ (menu_level)).addClass(submenu_classes) }}>
{% endif %}

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

Twig search with multiple parameters

We are making a website with a search form. In that form users can search with text and checkboxes. With the checkboxes users can search only in the related categories.
We need to accomplish this with Craft CMS and Twig.
We tried the following method, but that didnt worked.
{% set query = craft.request.getParam('search-results') %}
{% set nietBijdragePlichtig = craft.request.getParam('plg-Nee') %}
{% if nietBijdragePlichtig == 'on' %}
{% for entry in craft.entries.section('producten').limit(null).order(asc).search('query, nietBijdragePlichtig').find() %}
<div id=”test”>test</div>
{% endfor %}
{% endif %}
and we tried:
{% set query = craft.request.getParam('search-results') %}
{% set nietBijdragePlichtig = craft.request.getParam('plg-Nee') %}
{% set alle = query ~ ' ' ~ nietBijdragePlichtig %}
{% if nietBijdragePlichtig == 'on' %}
{% for entry in craft.entries.section(‘producten’).limit(null).order(asc).search(alle).find() %}
<div id=”test”>test</div>
{% endfor %}
{% endif %}
We also tried to use multiple .search()
We tried both codes with different syntaxes.
How can we accomplish this?
Really thanks!

Find if twig array is empty of non-protected values

Given an a php array in a twig template:
object(Drupal\Core\Template\Attribute)#1208 (1) {
["storage":protected]=> array(0) { }
}
How do I check if there are no non-protected elements in the array? The idea is that I can only operate on non-protected values, so I can pretend the array is empty if only protected values are present.
So far, my check is as follows:
{% if attributes is defined and attributes is not empty %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
In its current form, this displays <div>[Content]</div>. Instead, I'd like to see: [Content]
Any help?
If this is in Drupal 8, you can pass the attributes value through render to find out, like this:
{% if attributes|render %}
<div{{ attributes }}>
{{ content }}
</div>
{% else %}
{{ content }}
{% endif %}
Extend twig
<?php
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_SimpleFilter('accessible_properties', 'get_object_vars'));
Use it inside template
{% set public_attributes = attributes is defined ? (attributes|accessible_properties) : [] %}
{% if public_attributes is not empty %}
...
{% else %}
...
{% endif %}

Is it possible to concatenate a variable to an if statement in Twig?

I am trying to concatenate a variable to an array key to get access to certain values in Twig, but no success so far.
I have a large PHP array that has for example keys like this:
$array = [
...
...
...
'test_1' => $test_1,
'test_2' => $test_2
];
I tried the following in my Twig template:
{% for i in 1..2 %}
{% if array.test_{{ i }} != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}
but that doesn't work.
Is there a way to access values like this in Twig?
Try this:
{% for i in 1..2 %}
{% if array['test_' ~ i] != 0 %}
<div>Test</div>
{% endif %}
{% endfor %}

Categories