Why it doesn't allow to replace the string within IF condition - twig

{% if ('Desk Clock' in product_flags) %}
{{product_flags|replace({"Office & Awards > Office > Clocks": "Office & Awards > Office > Desk Clocks"})}}
{% else ('Wall Clock' in product_flags) %}
{{product_flags|replace({"Office & Awards > Office > Clocks": "Office & Awards > Office > Wall Clocks"})}}
{% endif %}
Getting this error
Unexpected token "punctuation" of value "(" ("end of statement block" expected)
New to Twig, could you please help me out?

Related

translate english keyword to arabic in template file inside table with use of po file.not translating.translation is included in po file

tried this: added the text to po file. not translating
{% load i18n %}
{% trans "Thanks you," %}
{% trans "The Team Auraa" %}

Timber Twig - show elements if date/time is in the present/future

Two part question:
One:
I've used ACf to make some fields for linking to a Zoom meeting, including a date field and a time field, and I want a banner to only show if the date value is equal to or greater than the current date (i.e. today or future) - I've tried:
{% if post.meta('zoom_meeting_date') >= 'now'|date %}
<!-- markup here -->
{% endif %}
This is only working if the date is in the future, not if it is today.
Two: I want a 'join' button that will only show once it is within say 15mins of the start time - no idea where to start with that one!
Any help greatly appreciated!
I suggest using the same date filter without time on both sides of the comparison: |date('m/d/Y')
{% if date("2020-03-30 23:59:59.000000")|date('m/d/Y') >= 'now'|date('m/d/Y') %}
<h1>bang<h1>
{% endif %}
{% if date("2020-03-30 00:00:01.000000")|date('m/d/Y') >= 'now'|date('m/d/Y') %}
<h1>boom<h1>
{% endif %}
{{ 'now'|date }}
{{ 'now'|date('m/d/Y') }}

How to require 2 conditions

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) %}

OctoberCMS, comparing a record string to a string

I'm attempting to clean up my Titles in my October CMS Project and I'm running into an issue.
I have a set of pages set to be "Singles" and thus have the title [category]-single which is not great.
So to that end I'm trying to use the [x] in [y] function in twig for an if function as follows;
{% else if ('single' in this.page.baseFileName) %}
<title>[formatted title]</title>
{% else %}
this doesn't help and throws an "Unexpected token "name" of value "if" ("end of statement block" expected)." exception.
No variation I can think of (bracketing out the record pointer, for example) and I'm reaching an impasse on this.
Any help?
it seems you are adding extraspace to else if just remove it and use elseif
{% elseif ('single' in this.page.baseFileName) %}
{# ^ use like this #}
<title>[formatted title]</title>
{% else %}
docs : https://twig.symfony.com/doc/2.x/tags/if.html
if any doubt please comment.

How can I parse a querystring using liquid template in Azure API-Management set-body?

I hope someone sees this and know exactly what to do. I feel like I tried everything :(. To give a little background, I'm trying to create a generic rest endpoint to proxy a SOAP reporting service backend and pass the query string keyValues as parameters.
I started down the path using context.Request.OriginalUrl.Query which is: IReadOnlyDictionary<string, string[]> and loop through the keys and values. This is my preferred method but I was recieving the error:
Liquid syntax error: Object '[p_customer_name, System.String[]]' is
invalid because it is neither a built-in type nor implements
ILiquidizable
Code to produce above:
<v2:listOfParamNameValues>
{% for parameter in context.Request.OriginalUrl.Query %}
<v2:item><v2:name>{{parameter.Key}}</v2:name><v2:values>
{% for value in parameter.Value %}
<v2:item>{{value}}</v2:item>
{% endfor %}
</v2:values></v2:item>
{% endfor %}
</v2:listOfParamNameValues>
I gave up on that and began to parse the QueryString myself just so I could move on but using Split: & or Split: '&' just seem to ignore the & and Split: '&' causes an error:
One or more fields contain incorrect values: An error occurred while
parsing EntityName. Line 15, position 123.
<v2:listOfParamNameValues>{% assign parts = context.Request.OriginalUrl.QueryString | Remove-First: '?' | Split:'&' -%}
{%- for part in parts -%}
{%- assign keyValues = part | Split:'=' -%}
{%- for keyValue in keyValues -%}
{{-keyValue[0]}}:{{keyValue[1]-}}
{%- endfor -%}
{%- endfor %}</v2:listOfParamNameValues>
I'm open to any ideas, let me know if there is anything else I can add that may help.
Thank you!
There seems to be a limitation when using the Ampersand to do a split within the API Management policies inside of the set-body with liquid template.
To get around that I used an additional policy, like this:
<inbound>
<base />
<set-variable name="query_string_params" value="#(context.Request.OriginalUrl.QueryString.Substring(1).Replace("&","|"))" />
<set-body template="liquid">
{% assign parameters = context.Variables["query_string_params"] | Split: "|" %}
<parameters>
{% for p in parameters %}
<parameter>
{% assign parts = p | Split: "=" %}
<name>{{parts.first}}</name>
<value>{{parts.last}}</value>
</parameter>
{% endfor %}
</parameters>
</set-body>
</inbound>
Hope this helps!
Liquid filters are case sensitive an it seems that dotliquid uses this convention by default.
{% assign parts = context.Request.OriginalUrl.QueryString | remove_first: '?' | split:'&' -%}

Resources