Concatenate in an element acf fields or an objects - object

I'm trying to concatenate a Twig variable and an ACF options field but it doesn't work.
I have two footers and I want to select the correct one according to the correct page.
{% if post.slug == "page1" %}
{% set pageType = "pages" %}
{% elseif post.slug == "page2" %}
{% set pageType = "otherspages" %}
{% endif %}
<footer id="footer">
<h2>{{ options.footer_~pageType~_titre }}</h2>
<div>{{ options.footer_~pageType~_container }}<div>
</div>
The ACF fields are called footer_page_titre or footer_otherpage_titre depending on the footer I want to display
Thank you

Try constructing the field name first, for example with the twig format filter which formats a given string by replacing the placeholders, similar to sprintf, and then access the field value by in the options data array.
Construct the field name:
{% set footer_title = "footer_%s_title"|format(pageType) %}
Access the value by array key
<h2>{{ options[footer_title] }}</h2>

Related

How to calculate the composite's elements and get total of them?

Hi I have a composite that has a QTY field and a TOTAL PRICE field and the table below would calculate and display the subtotal. It works properly when there is only one row of composite, but when I add more items, the subtotal field displays two subtotals instead of one as a whole. I want the subtotal field to display 24 instead of 4 and 20. How can twig solve this implementation? In my SUBTOTAL, I have
{% for item in data.item %}
{% set total_qty = (item.qty)|number_format(2,'.',',') %}
{% set per_price = (item.total)|number_format(2,'.',',') %}
{% set net_cost = (total_qty * per_price )|number_format(2,'.',',') %}
{{ net_cost }}
{% endfor %}
Here is the screenshot to give you better understanding
Don't output the net cost inside the for loop.
First create the sum, then display it after the loop.
Also don't use number_format before the final result.
{% set net_cost = 0 %}
{% for item in data.items %}
{% set net_cost = nest_cost + item.qty * item.total %}
{% endfor %}
{{ net_cost|number_format(2, '.', ',') }}
demo

RainLab.Blog Post List: List Sub-Categories under a specific Top-Level Category

I'm working on an October CMS project which use a blog where I need to seperate blog posts in two major categories. When listing blog posts using the RainLab.Blog plugin's Post List component, I need to list categories that are sub-categories under a specific top-level category and exclude other categories.
In the TWIG template, I want to iterate through and list out the categories that belong to "Birds" and not "Sealife".
In the default Post List component, categories are listed like this:
{% for category in post.categories %}
{{ category.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
I would like to change this to something like this:
{% for category in post.categories %}
{# if category is a sub-category of "Birds"... #}
{{ category.name }}{% if not loop.last %}, {% endif %}
{# endif #}
{% endfor %}
So I would like the post to be listed as "Shorebirds" and "Hummingbirds" but not as "Corals" as this is a category that is not a direct child of "Birds".
I came across this stack overflow question, but it avoids rendering posts that do not match the criteria all together. I still want to fetch and render posts that are in other categories, but only list the categories if they match.
If it's totally fine to hardcode category then you can simply compare categories parent's slug or id to hardcoded value.
Here I am using the slug to compare parent, you can also use id it's totally up to you.
{% for category in post.categories %}
{% set parentCat = category.getParent().first() %}
{% if parentCat.slug == 'birds' %}
<!-- here we are ^ comparing ^ please replace value as per your need -->
{{ category.name }}
{% if not loop.last %}, {% endif %}
{% endif %}
{% endfor %}
now it should only show a categories which have a parent category having given slug.
if any doubt please comment

How can i query entries by a field that are within a matrix?

Each entry has a matrix field with fields including start date, end date, location etc
I need to list and display only the entries after now.
I am able to filter them by
{% set queryEntries = craft.entries({
section: ['events'],
}) %}
{% for entry in queryEntries.all() %}
{% if entry.eventDetails.dateFrom|date("U") > "now"|date("U") %}
{{ entry.title }}
{% endif %}
{% endfor %}
This isn't great though as when the events list grows it won't be very performant and I have entries that I don't want.
Am I am able to filter the queryEntries by entry.eventDetails.dateFrom|date("U") > "now"|date("U") ?

Drupal 8, Twig - how to output fields from a custom view template, and check for taxonomy terms

I want to output the values of a taxonomy field that's rendered from a view:
<h4>{{ label }}</h4><!-- Does show -->
<p>{{ content.field_event_type }}</p><!-- Doesn't show -->
<p>
{{ content.field_date }}<!-- Doesn't show -->
<br>{{ content.field_location }}<!-- Doesn't show -->
</p>
Read event details
Also, field_event_type is a taxonomy field. I'd like to be able to check if it has a given taxonomy, as well as output the values as above. For example:
{% if content.field_event_type.has_term("webinar") %}
<p>Is webinar</p>
{% endif %}
But I can't find anything to do this.
Try this
{% set tid = node.yourTaxonomyField.target_id %}
{% if tid %}
{{ drupal_field('field_desired_field', 'taxonomy_term', tid) }}
{% endif %}

Set a property value in an existing twig object

I would like to define a twig object that contains two properties :
The first one is a short text
The second one is a large text (so, i need to use {% set %}{% endset %} to keep readability of the template)
I first tried like this :
{% block principal %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': ''} %}
{% set a_form_help.help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
<p>And several lines of tips for this field</p>
{% endset %}
{% endblock %}
But Twig complains about the use of the dot in the second {% set %} tag (for setting the large text content in the help_content property).
I've found this message (Twig - Why does it not allow us to set object / array values?) and then done this workaround by using a temporary variable:
{% block principal %}
{% set tmp_help_content %}>
<h1>This is rules for the field</h1>
<p>A looonng text that will be contained in a collapsed block</p>
{% endset %}
{% set a_form_help = {'help_toggle_label': 'See the big form field tip', 'help_content': tmp_help_content} %}
{{ dump(a_form_help) }}
{% endblock %}
I've used a a temporary variable since using the merge() filter would give the same result.
Is this workaroud a good practice or is there a better/cleaner way ?
Thanks in advance for any tip|advice ! :-)
There is no clean way to do it in twig.
Is this workaroud a good practice or is there a better/cleaner way ?
It is not a good practice. As twig is a templating language its responsability is only to display data, not to structure it. Your View Object should have been created elsewhere

Resources