Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Creating Helper Function

Started by Muut Archive 11 years ago · 26 replies · 723 views
11 years ago

now the random function works just fine, there was a problem with my installation of GRAV !

TWIG

{{ random(config.brain.properties) }}
---
11 years ago

Oh well, still not finished... (base.html.twig)

HTML
{% 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)

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 ?

11 years ago

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

TWIG

{% set hour = "now"|date("G")|number_format %}
---
11 years ago

i'm sorry, this is not the solution to the problem .... :( still doesn't work !

11 years ago

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:

TWIG

{% 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!
11 years ago

Thanks. Now I've got it!

Passing in the config variable did it ! Thanks.

11 years ago

this is the resulting website (as far as i did it) ...

http://timomue.com

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 !!!!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1349 9 years ago
Archive · by Muut Archive, 9 years ago
2 934 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2946 9 years ago
Archive · by Muut Archive, 9 years ago
3 1118 9 years ago