In a twigtemplate with Drupal 8, I have a view.
I want this view to be displayed only if it has a result and if the current user is logged in.
I tested the code below but it doesn't work :
{% if logged_in and if view > 0 %}
Try this:
{% if logged_in and view > 0 %}
You don't have to put it in parentheses.
The condition should be :
{% if logged_in and (view > 0) %}
Related
I'm using Shopware 6 and need to check for a the current active category name in a Twig condition?
Something like this:
{% if page.header.navigation.active.category == 'Books' %}
<h1>yes</h1>
{% endif %}
On the product detail page the category can be found in page.product.seoCategory. To check category by name:
{% if page.product.seoCategory.translated.name == 'Books' %}
<h1>yes</h1>
{% endif %}
On all other pages you can use the variable page.header.navigation.active:
{% if page.header.navigation.active.translated.name == 'Books' %}
<h1>yes</h1>
{% endif %}
I have a dropdown menu with 2 options
Ordered
Unordered
I want to check if the value is Unordered or Ordered
{% set viewModel = {
cards: content['cards'] ?? [],
cardListType: content.cardListType ?? ''
} %}
{{ viewModel.cardListType|json_encode() }}
Returns this to me
{"0":{"label":"Unordered","value":"unordered","selected":true}}
I want to check if the value of viewModel.cardListType is unordered and that it's selected.
I have tried
{% if viewModel.cardListType.value is unordered %}
<p>UNORDERED</p>
{% endif %}
and
{% if viewModel.cardListType.value:unordered %}
<p>UNORDERED</p>
{% endif %}
and several other variations of this.
I don't understand the documentation/ syntax for dropdowns in craft or twig.
Can someone please explain like I'm 5 how this works and how to check the value?
I am using Craft 3 if this changes the syntax
Thanks in advance
I am very new with webhook.com , But i have setup the cms and able to manage my site the issue is i want to join two strings with the if else condition
{% if link!="" %}
{% set link = object.menu_url %} menu link will look like "https://www.example.com" it is ok
{% else %}
{% set link = object.menu_id %} menu link will look like "#menulid"
{% endif %}
i just want to add # with the menu id something like {% set link = "#".object.menu_id %}
use + sign to concatinate like
{% set link = "#"+object.menu_id %}
I am trying to get a list to highlight if it is at its current page (route). However, I also have subpages within the pages of which I also want the list to be highlighted.
I have my menu if statements:
<ul class="menu">
{% if app.request.get('_route') == 'home' %}
<li class="current">Home</li>
{% else %}
<li>Home</li>
{% endif %}
{% if app.request.get('_route') == 'reports' %}
<li class="current">Reports</li>
{% else %}
<li>Reports</li>
{% endif %}
// etc etc
Now in my reports page, the route is /reports/ I have a menu that clicks to "Detail", "Summary", etc it will go to /reports/detail and /reports/summary... I want it so that when users click on those links, the main navigation is still highlighted.
I was wondering if there is an if statement condition something like this:
{% if app.request.get('_route') starts with(?) 'reports' %}
So whenever anyone goes to a route that's a sub page of /reports/, the "Reports" li in the menu will still be highlighted?
I'm not sure if twig has a function for "starts with" but you can check for containment using in
{% if 'reports' in app.request.get('_route') %}
Just to update this question with current information.
As per current (2.x) Twig documentation this is possible the way it is asked for.
To be more specific, the documentation states:
You can also check if a string starts with or ends with another string:
{% if 'Fabien' starts with 'F' %}
{% endif %}
{% if 'Fabien' ends with 'n' %}
{% endif %}
As such, the desired expression is perfectly possible:
{% if app.request.get('_route') starts with 'reports' %}
And works as expected.
There are some good choices on this thread but another option is to pass the route into an array. This is useful if you have dropdown menu items.
The items in the array would be your defined routes.
{% set routes = {
'activities':
[
'walking',
'swimming',
'running'
]
} %}
Then in your menu bar add this to your menu label class.
{% if app.request.attributes.get('_route') in routes.activities %} Do Something {% endif %}
I have a Symfony 2.1 version with a controller that returns the total of points.
I am rendering this in my twig template as follows:
{%render "AdminBundle:Reports:getExpiringPoints" with {'id':dealer.id}%}
This prints the total points. I need to check that value and print it if the rendering value is greater that 0.
Is it possible in Symfony 2.1?
i found that :
{% set x %}
{%render "AdminBundle:Reports:getExpiringPoints" with {'id':dealer.id}%}
{% endset %}
{% if x> 0 %}
//display
{% endif %}
and after you can use it.
Tell me if it works
You can check it directly in the controller or with twig :
{% if var > 0 %}
//display
{% endif %}
Twig supports all of the standard logical operators ==, !=, <, >, >=, and <=.