I have a a Jekyll website and I would like to create a day counter, starting from 1 and then every day it should add +1 to the counter.
So today it would say:
Day 1
Tomorrow it should say
Day 2
The day after:
Day 3
is that possible to do in jekyll?
You just need to calculate the difference between two dates. There is some discussion about that here: https://stackoverflow.com/a/34615552/1645925
My favourite answer states:
{% assign today = site.time | date: '%s' %}
{% assign start = '20-01-2014 04:00:00' | date: '%s' %}
{% assign secondsSince = today | minus: start %}
{% assign hoursSince = secondsSince | divided_by: 60 | divided_by: 60 %}
{% assign daysSince = hoursSince | divided_by: 24 %}
Hours: {{hoursSince}}
Days: {{daysSince}}
I believe site.time refers to the server time that your site is hosted on but you can use 'now' to use the users time.
I assume you want a web page that tells you: today is day number 16 of the month.
Because Jekyll is a static site generator, all logic will only be executed during generation of the site. When you do not update/rebuild your website (daily), the counter does not increment. I think using javascript is the only solution for this problem.
Given my assumption, the answer is: No, Jekyll does not do this. You should use javascript for this.
Related
I'm trying to find the date and time difference between two unix values. In HuBL, I have currently:
{% set event_date_and_time = table_data_dict.event_time|unixtimestamp %}
{% set current_time_and_date = local_dt|unixtimestamp %}
{% if current_time_and_date >= event_date_and_time %}
event is live
{% else %}
event is not live
{% endif %}
table_data_dict.event_time is the value from the date and time field in the database.
Running the following:
Event: {{ event_date_and_time }} <br>
Current: {{ current_time_and_date }}
Produces the following results:
Event: 1596623700000
Current: 1596620020929
Test scenarios:
Test 1:
The date and time I have set in the database (what event_date_and_time is) is 10th August 2020 12pm.
The if statement echos event is not live - which is correct.
Test 2:
The date and time now, in the database is set to a day before todays date (so 4th august 2020 12:00pm) - the event is essentially live.
The if statement echos event is live - which again, is correct.
Test 3:
Now, currently for me, it's 11:00am (5th Aug 2020). If, in the database, I change the date and time to 5th August 2020 11:10am. It produces the following results:
At 10:55am, when I check the if statement, it will say event is not live (correct, it's live at 11am).
Now, when I check at 11am (when the event is live) it says event is not live.
When I continue to wait and check again at 11:05am, it still says event is not live.
Unsure why? I have cleared cache, it's not a caching issue, unsure why it's performing like so?
I want my project to be filter by the score and I am using a elasticsearch. now, the score have two variables: matchingScore and personalityScore. this two is should be added in the twig to get the actual score. Now, I dont know how to sort an integer since this is not an array and the sorting in the twig is required an array. This is the sample code:
{% for provider in matches | sort matchingScore[provider.id] + personalityScore[provider.id]('desc') %}
{% if matchingScore[provider.id] + personalityScore[provider.id] >= score_criteria %}
Please Help me. thanks.
I'm developing a site with Drupal 8. I made a custom content type, with a lot of fields; in particular I have a field called "field_categoria_del_libro_presen" that is linked to a taxonomy term. It's possible to specify more than one value. I used kint() to obtain the structure of the node. Here:
field_categoria_del_libro_presen
→array(2)
target_id
1
"1"
2
"4"
I attached the screenshot too.
screenshot
How can I get the value "1" and "4" that represent the terms of my taxonomy?
Thank you so much in advance.
Regards.
Valentina
ANOTHER QUESTION UNSOLVED:
how can I get the length of the array?
I found a solution. So I answer my question.
{{ node.field_categoria_del_libro_presen.0.target_id}}
{{ node.field_categoria_del_libro_presen.1.target_id}}
According to your self provided answer I guess you're talking about how to get the value in a twig template. Well generally speaking if you want to get the length of an array you can use the "length" filter. It looks like this:
{{ someArray|length }}
And regarding your original question, if the number of selected values varies and you want to display them all I would suggest using a for loop, it looks like this:
{% for arrayElement in someArray %}
{{ arrayElement.someKey }}
{% endfor %}
For more information look here: http://twig.sensiolabs.org/doc/tags/for.html
and here:
http://twig.sensiolabs.org/doc/filters/length.html
I am trying to add a query string to a link in my twig file for the purpose of jumping to today's agenda in jQuery FullCalendar, but the months are based on a 0-11 structure rather than the standard 01-12.
I am using the following string to generate the query in my twig template:
View Today's Agenda
But using the "date_modify" and "-1 month" will not work if it's January as it will render "12" instead for the previous December. So, if it's January it needs to show as "0".
Is the way of doing this in Twig? It's not practical for me to render it in my Controller PHP as I'd have to do it many times due to my extensive range of pages in my CRM.
Thanks in advance
Michael
Try:
&m={{ ("now"|date("m"))-1 }}
I want to be able to display the text "OK" if a date is less than 2 years old, and if it's older than 2 years old I want to display "Over 2 years old". How can I do this using Twig?
this problem is explained on twig documentation about date function
{% if date(mydate) > date('-2 years') %}
OK
{% else %}
Less than 2 years old
{% endif %}