{% set event_date = event.schedule|date('d-m-y') %}
{% set nowdate = "now"|date('d-m-y') %}
{% if event_date < nowdate %}
view some 1
{% else %}
view some 2
{% endif %}
If change event_date bigger or lower than nowdate, have one result: view some 2.
Why doesn't work?
You are in a string comparison situation. For better approach this, use the U filter as follow:
{% set event_date = event.schedule|date('U') %}
{% set nowdate = "now"|date('U') %}
{% if event_date < nowdate %}
view some 1
{% else %}
view some 2
{% endif %}
{{ event_date }}
Live testing in this fiddle.
Hope this help
You're comparing strings. d-m-y is not a usable format for comparing dates. Try Y-m-d, where the parts are in order of importance.
Even better, you could compare two \DateTime objects instead of strings. I assume event.schedule is one, you just need to pass one for the current date to twig.
In addition to the date filter, Twig defines a date() function which has been designed precisely to compare dates.
Related
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
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") ?
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 want to check whether a certain word exists in a string. However, there's not other operator that exists which can do the job except containment operator. The following doesn't work to my utter frustration.
{% set deduction = 'Less Withholding Tax Thereon' %}
{% if 'Witholding' in deduction %}
{% set test = "with" %}
{% else %}
{% set test = "out" %}
{% endif %}
{{ test }}
Please see fiddle with various tests all amounting to same result
https://twigfiddle.com/00odoi
So, here's my situation, I have a beginning date and an ending date, with Twig I want to be able to loop through all days in the so called period so that I could print out every day. Of course, that's just for understanding how to do it, the goal is to get them into a chart. Anyway, I have the following code (with what are my vars) :
{% set start_year = date(start) | date('d-m-Y') %}
{% set end_year = date(end)| date('d-m-Y') %}
{% for i in start_year..end_year %}
{{ i }}
{% endfor %}
My start var is 01-01-2003 and my end var is 10-05-2014. The values don't matter as they could change, but that's the format I have.
This actually prints out 0 1 which I don't understand at all. If anyone has an idea either how to do this or how to fix what I'm doing, it would be really nice. Thanks.
You cannot define a range of exact dates (neither as range in php) but you can create a range of seconds with step of a 24 hours second which is 86400; if you use date('U') it will convert date string to seconds since the Unix Epoch (same as Time() in php)
{% set start_date = '01-06-2014' %}
{% set end_date = '05-06-2014' %}
{% for x in range(start_date|date('U'), end_date|date('U'), 86400 ) %}
{{ x|date('d/m/Y') }}<br>
{% endfor %}
Tip
Pay attention the format of date to use - as separator not / because it will lead to totally different result
{% set pd_begdate = header_data.pd_begdate %}
{% set pd_enddate = header_data.pd_enddate ~ ' 23:59:59' %}
{% for key, count_item in range(pd_begdate|date('z'), pd_enddate|date('z'), 1)|slice(column_offset, column_count, 'preserve_keys') %}
{% set item = pd_begdate|date_modify('+'~ key ~' day') %}
<td class="tr5 td9"><p class="p9 ft0">{{ item|date("M") }}<br>{{ item|date("d") }}<br>{{ item|date("D") }}<br>{{ key + 1 }}</p></td>
{% endfor %}
This solution works for day light savings.
I had trouble with the solution when I tried to iterate over an entire year - after about 8 months, I found it would drop a day.
But based on the solution How to add n-days to twig date format within a for-loop?, I found that this would work:
{% set yearstart = "01-01-2003"|date('m-d-Y') %}
{% set eachday = "" %}
{% for i in range(0,365) %}
{% set eachday = yearstart|date_modify("+" ~ i ~ "day")|date('m-d-y') %}
{% endfor %}