now the random function works just fine, there was a problem with my installation of GRAV !
{{ random(config.brain.properties) }}
---
now the random function works just fine, there was a problem with my installation of GRAV !
{{ random(config.brain.properties) }}
---
Oh well, still not finished… (base.html.twig)
{% import "macros/macros.html.twig" as m %}
<div class="flex-container" id="header">
<div class="flex-box">
{{ m.hour_based_greeting() }}
</div>
</div>
und (macros/macros.html.twig)
{% macro hour_based_greeting() %}
{% set hour = "now"|date("G") %}
{% if (hour < 7) %}
Einen {{ random(config.brain.properties) }}en Morgen!
{% elseif (hour < 12) %}
Einen {{ random(config.brain.properties) }}en Morgen!
{% elseif (hour < 16) %}
Einen {{ random(config.brain.properties) }}en Nachmittag!
{% elseif (hour < 22) %}
Einen {{ random(config.brain.properties) }}en Abend!
{% else %}
Einen {{ random(config.brain.properties) }}e Nacht!
{% endif %}
{% endmacro %}
ERROR: Grav renders a random number … like 2122154669 !!! …
Where is the mistake ?
Well, let me guess. You get as output something like “Einen 134561e Nacht!”? This is because hour is a string (obviously date returns a string). You have to cast it into a number. See my snippet above or
{% set hour = "now"|date("G")|number_format %}
---
i’m sorry, this is not the solution to the problem … still doesn’t work !
Ok, there are more problems. First use config.brain.adjectives
instead of config.brain.properties
. Second pass the config variable to the macro, since a macro lives in its own scope.
Tested with the below snippet, which works:
{% macro hour_based_greeting(config) %}
{% set hour = "now"|date("G") %}
{% if (hour < 7) %}
Einen {{ random(config.brain.adjectives) }}en Morgen!
{% elseif (hour < 12) %}
Einen {{ random(config.brain.adjectives) }}en Morgen!
{% elseif (hour < 16) %}
Einen {{ random(config.brain.adjectives) }}en Nachmittag!
{% elseif (hour < 22) %}
Einen {{ random(config.brain.adjectives) }}en Abend!
{% else %}
Einen {{ random(config.brain.adjectives) }}e Nacht!
{% endif %}
{% endmacro %}
{{ dump(_self.hour_based_greeting(config)) }}
---
*Output:* Einen wohltuenden Abend!
Thanks. Now I’ve got it!
Passing in the config variable did it ! Thanks.
this is the resulting website (as far as i did it) …
Have a look at the bottom line, it uses the macro we have developed. Try refreshing the browser and focus on the text. It changes !!!