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 · 721 views
11 years ago

I want to create a helper function [in php] for writing a greeting according to the date and time! Should I create a plugin or is there an opportunity to insert php code elsewhere ?? Thanks!

11 years ago

Hi @timomue,

if you want to do it in PHP then Shortcodes will help you. The only thing you need is to add either a block or inline shortcode via

PHP
/ Create block shortcode
$block = new Shortcodes\BlockShortcode('myshortcode', [$shortcode, 'block']);

// Create inline shortcode
$inline = new Shortcodes\InlineShortcode('myshortcode', [$shortcode, 'inline']);

where $shortcode is a callable (see the developer section https://github.com/Sommerregen/grav-plugin-shortcodes/#for-developers ).

Otherwise you may want to use Twig (process.twig) and set up a macro. As long as it has no performance implications, that's ok.

11 years ago

is there a way to check the date and time in twig ??

11 years ago

Thank you, i am going to create a twig macro... How is a macro structured?

11 years ago

See for example http://twig.sensiolabs.org/doc/advanced.html#filters .

You may also do it in pure Twig with the below snippet (probably needs some improvments though)

TWIG

{% set hour = "now"|date("g")|number_format %}
{% set m = "now"|date("A") %}

{% if m == "AM" %}
   {% if hour == 12 %}
      <p>Good Evening!</p>
   {% elseif hour < 4 %}
      <p>Good Evening!</p>
   {% elseif hour > 3 %}
      <p>Good Morning!</p>
   {% endif %}
{% else %}
   {% if hour == 12 %}
      <p>Good Afternoon!</p>
   {% elseif hour < 6 %}
      <p>Good Afternoon!</p>
   {% elseif hour > 5 %} 
      <p>Good Evening!</p>
   {% endif %}
{% endif %}
---
11 years ago

how to call a macro from within a twig file ?

11 years ago
TWIG
{% set adjective = random(config.brain.adjectives) %}

Is there a problem?

11 years ago

twig's output is a string full of random numbers !?!?!?

11 years ago

Hi @timomue,

ok it really depends on what type is your variable in config.brain.adjectives. According to the docs If it is an array like

TWIG
{{ random(['apple', 'orange', 'citrus']) }}

it will for example return "orange". If you have a string instead {{ random('ABC') }} it will output maybe "C". I don't know what you are looking for but try using an array in your config viz.

YAML

brain:
   adjectives: ["Bland", "Minty", "Sweet"]
---
11 years ago

Can you provide an example? I guess your variable config.brain.adjectives is empty. Where did you put this into? Check it with {{ dump(config.brain.adjectives) }}. You should see a value in the debug bar.

11 years ago

my yaml file "config/brain.yaml"

YAML

adjectives:
  - "wunderschön"
  - "angenehm"
  - "wohltuend"
---
11 years ago

maybe grav has a problem finding and rendering my yaml files ?!?!?!?!

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 1117 9 years ago