How to convert time zone in django template
{% now 'h' as h %}
{% now 'i' as i %}
{% now 's' as s %}
{% now 'T' as T %}
{{ h }}:{{ i }}:{{ s }} {{T}}
T returns UTC. but I want it to change UTC + 5:30. How to do it?
Related
I'm creating a master events table, where almost all of the same columns are in every table, but a few tables are missing one or two columns. In those cases I'd like to replace those columns with null whenever the column doesn't exist in the table. When I run the code below, every cell in the output table is NULL.
Assume columns 1 and 2 are in every table, but column 3 is in table 1 and 2 but not 3.
{{ config(schema='MYSCHEMA', materialized='table') }}
{% set tables = ['table1', 'table2', 'table3'] %}
{% set possible_columns = ['col1', 'col2', 'col3'] %}
{% for table in tables %}
{%- set table_columns = adapter.get_columns_in_relation( ref(table) ) -%}
select
{% for pc in possible_columns %}
{% if not loop.last -%}
{% if pc in table_columns %}
{{ pc }},
{% else %}
null as {{ pc }},
{%- endif %}
{% else %}
{% if pc in table_columns %}
{{ pc }}
{% else %}
null as {{ pc }}
{%- endif %}
{% endif %}
{%- endfor %}
from
{{ ref(table) }}
{% if not loop.last -%}
union all
{%- endif %}
{% endfor %}
I'd recommend using dbt_utils.union_relations for this. It does exactly what you describe -- it creates a superset of columns from all the tables and fills in nulls where appropriate.
{{ dbt_utils.union_relations(
relations=[
ref('table1'),
ref('table2'),
ref('table3'),
],
include = ['col1', 'col2', 'col3']
) }}
BUT, if you want to roll your own...
The problem with your code is this line:
{% if pc in table_columns %}
adapter.get_columns_in_relation returns a list of Column objects, not a list of strings. To compare pc to the name of a column, you could use:
{% set cols = [] %}
{% for col in table_columns %}
{% do cols.append(col.name) %}
{% endfor %}
...
{% if pc in cols %}
You can also remove a bunch of redundant code by pushing down the if not loop.last block to just the comma, so this all becomes:
{% for table in tables %}
{%- set table_columns = adapter.get_columns_in_relation( ref(table) ) -%}
{% set cols = [] %}
{% for col in table_columns %}
{% do cols.append(col.name) %}
{% endfor %}
select
{% for pc in possible_columns %}
{% if pc in cols %}
{{ pc -}}
{% else %}
null as {{ pc -}}
{%- endif %}{%- if not loop.last -%},{% endif %}
{%- endfor %}
...
I want to show banner on product page using date metafield and I am looking for the liquid code
My metafields are below:
{{ product.metafields.timer_test.image.value }}
{{ product.metafields.timer_test.fr-date.value }}
{{ product.metafields.timer_test.to-date.value }}
How can I make the liquid code on this base?
You can turn all the dates into timestamps (number of seconds), then compare them and see if the "now" timestamp is between the start and end. Just make sure the values in the metafields are properly formatted, for example "Aug 2, 2022".
You can test it here.
{% assign dateStart = product.metafields.timer_test.fr-date.value | date: '%s' %}
{% assign dateEnd = product.metafields.timer_test.to-date.value | date: '%s' %}
{% assign nowTimestamp = 'now' | date: '%s' %}
{% if dateStart < nowTimestamp and dateEnd > nowTimestamp %}
<img src="{{ product.metafields.timer_test.image.value }}" class="banner">
{% endif %}
I'm really puzzled and can't understand why my {{ time }} returns an empty value.
I'm trying to calculate the difference between the time currently and a booking time in Shopify. For the booking I have the date (first line_item.property) and the time (second one) coming separately. I want to add one to another and then compare it to the current time, but before I can do the math I have tried to display the different variables I have created.
{{ now }} & {{ date }} work fine but for {{ time }} it returns an empty value. On the other hand if instead of assigning it a value name I just display the line.item.property it works just fine.
Can you help me understand what I am doing wrong here?
{% assign today = 'now' | date: '%s' %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign date = property.last | date: '%s' %}
{% endfor %}
{% for property in line_item.properties offset:1 %}
{% assign time = property.last | date: '%s' %}
{% endfor %}
{%- endfor -%}
{{ now }} - {{ date }} - {{ time }}
{% endfor %}
By using plus: 0 you can convert the string to integer, this will enable math operation for your comparison.
{% assign today = 'now' | date: '%s' | plus: 0 %}
check with {% unless line_item.properties == empty %} to see if properties exist.
{% assign date = property.last | date: '%s' %} , the variable property.last must follow a well-formatted dates for date to work. liquid-date-format
{% for property in line_item.properties offset:1 %}
{% assign time = property.last | date: '%s' %}
{% endfor %}
problem with offset:1, if the array only has 1 line_item.properties this will not run at all. Hence time is empty; or it exist but property.last does not have a date format.
{% assign today = 'now' | date: '%s' | plus: 0 %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% unless line_item.properties == empty %}
{% for property in line_item.properties %}
{% if forloop.index == 1 %}
{% assign date = property.last | date: '%s' | plus: 0 %}
{% if date == 0 %}
{% comment %}Opp! Not A Date, Terminate loop{% endcomment %}
{% break %}
{% endif %}
{% else %}
{% assign time = property.last | date: '%s' | plus: 0 %}
{% unless time == 0 %}
{% assign date = date - time %}
{% endunless %}
{% endif %}
{% endfor %}
{% endunless %}
{% endfor %}
{{today - date}}
Thanks Charles that really unblocked me.
Posting here the final version I went with as I needed to compare exact time and not simply the date of the order.
{% assign now_date = 'now' | date: '%s' | plus: 0 %}
{% assign now_time = 'now' | date: '%s' | plus: 0 %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign booking_date = property.last | date: '%s' | plus: 0 %}
{% endfor %}
{% for property in line_item.properties offset:1 limit:1 %}
{% assign booking_time = property.last | date: '%s' | plus: 0 %}
{% endfor %}
{% assign date_to_compare = booking_date | plus: '86400' %}
{% unless now_date > date_to_compare %}
{% if now_time <= booking_time or now_date <= booking_date %}SEANCE A VENIR{% endif %}
{% endunless %}
{%- endfor -%}
{% endfor %}
I am working a shopify shop but not trying to sell products but rather appointments.
I am using a shopify extension called Sesami to do so.
In the customer/account.liquid page I want to show the next coming appointment (or appointments) and a different information to people who don't have any future appointment coming.
I have been trying to do this with the following code :
{% if customer.orders != blank %}
{% for order in customer.orders %}
{%- for line_item in order.line_items -%}
{% for property in line_item.properties limit:1 %}
{% assign today_date = 'now' | date: '%s' %}
{% assign pre_date = property.last | date: '%s' %}
{% if pre_date >= today_date %}
{{ line_item.product.title | link_to: line_item.product.url }}
{% for property in line_item.properties limit:1 %}
{% if property != blank %}
{{ property.last | date: format: 'abbreviated_date' }}
{{ 'customer.orders.at' | t }}
{% endif %}
{%- endfor -%}
{% for property in line_item.properties offset:1 %}
{{ property.last }}
{%- endfor -%}
{{ line_item.image | img_url: 'small' | img_tag }}
{{ order.fulfillment_status_label }}
{% endif %}
{% endfor %}
{%- endfor -%}
{% endfor %}
{% else %}
Content for people with no booking at all
{% endif %}
But the problem is that the forloop stays open and therefore shows the content I am hoping to display to people with no upcoming appointment multiple times based on the total number of past appointments.
I imagine there is a much simpler way to do this and am hoping you can help me find it !
Thanks a lot,
Julien
Consider using {% break %} when you'd like the loop to stop it's current iteration.
https://shopify.github.io/liquid/tags/iteration/#break
{% set counter = 0 %}
{% for remain_todolist in remain_todolists %}
{% if counter == countRem_todolist %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}]
{% else %}
['{{ remain_todolist.Project_Name }}', {{ remain_todolist.Remaining_Todos }}],
{% endif %}~
{% do counter++ %}//Showing Error
{% endfor %}
Volt Increment statement is showing error
"Unknown expression 279"
What i am doing wrong ?
Have you tried without the do keyword?
Anyway, Volt loops already have counters available to use (see Loop Context), here's a version using it:
{% for list in remain_todolists %}
['{{ list.Project_Name }}', {{ list.Remaining_Todos }}]{{ loop.last ? '' : ',' }}
{% endfor %}
As #cvsguimaraes suggest you can use Loop context which should be better in your case.
I don't think that do statement is valid in Volt. If you need counter value outside loop you can increment value just like that:
{% set counter = counter + 1 %}