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 %}
Related
I'm looping over a collection of blog posts (Twig for loop) which appearance depends on tags given.
Here a quick example: I want to display all blog posts that have the tags "foo" and "bar".
It seems pretty easy to check if a post has one of the tags.
However it seems that checking if both tags are contained by a blog post isn't trivial.
What I want to accomplish is what the array.every() method in javascript does.
That's my current solution which works as expected but feels kinda fiddly and overcomplicated:
{% set given_tags_array = data.tags|split(',') %}
{% for post in posts %}
{% set post_categories_array = post.categories|map(category => category.name) %}
{% set bool_buffer_array = [] %}
{# push comparison result in bool array #}
{% for tag in given_tags_array %}
{% set bool_buffer_array = bool_buffer_array|merge([tag in post_categories_array]) %}
{% endfor %}
{# only display posts where all tags match #}
{% if false in bool_buffer_array %}
{% else %}
{# post data goes here #}
{% endif %}
{% endfor %}
As you can see inside the posts loop I'm checking if every given tag (i.e. "foo" and "bar") is part of the post categories array. I'm pushing the comparison result (boolean) to an empty array to check for any false values afterwards.
Why an array? I tried using a simple boolean variable but if any of the given tags is in the post categories array it resolves to true, which isn't exactly what I want.
So something like that doesn't work for me unfortunately:
{% for post in posts %}
{% set post_categories_array = post.categories|map(category => category.name)|sort|join('') %}
{% if given_tags_array|filter(given_tag => given_tag in post_categories_array) %}
{# post data goes here #}
{% endif %}
{% endfor %}
With this method I'm always doing an or comparison instead of an and comparison...
So...am I missing something and is there a simpler way to do that twig only?
Using the code you've already provided:
{% set temp = given_tags_array|filter(given_tag => given_tag in post_categories_array) %}
The filter filter returns a new array, this means temp should contain as many elements as your given_tags_array, if they are all inside the post_categories_array.
So if I'm not mistaken you could change your check to the following
{% for post in posts %}
{% set post_categories_array = post.categories|map(category => category.name)|sort|join('') %}
{% set temp = given_tags_array|filter(given_tag => given_tag in post_categories_array) %}
{% if temp|length == given_tags_array|length %}
{# display post #}
{% endif %}
{% endfor %}
In a CraftCMs site I'm trying to loop through a couple categories, then loop through entries within each of those categories, but without duplicating the entry if it is in both categories.
Here's my base code:
{% set selectedCategories = craft.categories()
.id([12605, 12619])
.all()
%}
{% set articleAuthor = entry.id %}
{% for category in selectedCategories %}
{% set articles = craft.entries()
.section('articles')
.relatedTo([
'and', {articleAuthor}, {category}])
.all() %}
{% if articles %}
{% for article in articles %}
// entry data here
{% endfor %}
{% endif %}
{% endfor %}
This works, but if an entry is in both categories it shows in both sections. I want to limit it to showing in whatever may be the first listed section. What am I missing?
In this scenario you can create a variable and check whether a duplicate entry is found or not.
{% set existingIds = [] %}
{% for article in articles %}
{% if entry.id not in existingIds %}
{% set existingIds = existingIds|merge([entry.id]) %}
{% endif %}
{% endfor %}
I'm trying to show specific content if a user is part of a list in HubSpot.
Psuedo:
If contact_id is part_of_this_list then do this
The contact ID at the moment is being obtained from the query string. I'm trying to check if the user is part of said list, but it's not working.
Approach:
{% set id_querystring = request.query_dict.id %}
{% set registration_list_id = "6136" %} <!-- id of the list I'm checking -->
{% if id_querystring in registration_list_id %}
contact is part of list
{% else %}
contact is not part of list
{% endif %}
i use this code:
{% set list_variable = request_contact.list_memberships|pprint %}
{% if "id of the list you are checking" in list_variable %} ← without the quotes
yes, you are in the list
{%else%}
No, you are not in the list
{%endif%}
I'm trying to strip some text from a value and afterwards create an array from it with the stripped values.
I'm having troubles to strip the text value.
I can only do this on the frontend since I have no access to the backend (SaaS platform)
In below example value.value (originally a textarea) returns the following text:
[185047078]1x something - Type 1
[415533322]1x something - something
[152890667]1x something 500x500 mm
I want to strip the text so I have [185047078], [415533322], [152890667] left or without the brackets.
Normally in JS you would do something like:
hide_ids = txt.match(/[^\]\[]+(?=\])/g)
However it need to be done in Twig.
Afterwards I want to push the values into an array hide_ids.
{% set hide_ids = [] %}
{% if product.custom %}
{% for custom in product.custom %}
{% if 'Some title' in custom.title %}
{% for value in custom.values %}
{% set hide_this_id = value.value %}
{% if hide_this_id matches '{/[^\]\[]+(?=\])/g}' %}
{% set hide_ids = hide_ids | merge([hide_this_id]) %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% set hidden = false %}
{% if id in hide_ids %}
{% set hidden = true %}
{% endif %}
What is the equivalent of match in Twig? I also tried replace but I just can't get that text stripped.
Any help greatly appreciated!
You could go with some simple string functions.
{% for id in ids %}
{{ id | split(']', 2)[0] | replace({'[': '',}) }}
{% endfor %}
demo
split is the explode of twig. This will separate your string in chased based on the ] character. The 2nd parameter (2) ensures there will only be maximum 2 parts in the array.
replace is just str_replace
If you wanted to solve this with regex you would either need to write a function/filter with preg_match or install an extension like this
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 %}