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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Global macro and how to use it

Started by Artyom 9 years ago · 2 replies · 2345 views
9 years ago

Hello!

I have a lot of links in my site, some of them contains http/https and some not. I want to check every link and if it contains http then add target="_blank" to it.

I want to use macro for this, but this theme is unclear in documentation:
1) In which file should I create this macro and in which folder? It should be a special file like functions.php in WordPress.
2) How to use it globally in every twig file?

I found macros.html.twig in Antimatter:
https://github.com/getgrav/grav-theme-antimatter/blob/f672ecbefe55c7e79a6672dcd3e6bc9b4c2e27a8/templates/macros/macros.html.twig

but can't find where this file is included or used:
https://github.com/getgrav/grav-theme-antimatter/search?q=macro&type=Code&utf8=%E2%9C%93

Help =)

8 years ago

I was looking for a solution to this, and it turns out it's just our nomenclature that needs re specifying: A macro is just a function, so we just need to register a function in Twig through a plugin to make it globally available. The docs do not note in much detail (see reference), but just from the PCRE-plugin example I added a needed macro for global use - albeit in a theme because my use-case is theme-specific.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 60 16 hours ago
Support · by Anna, 3 days ago
2 66 19 hours ago
Support · by Justin Young, 19 hours ago
1 33 19 hours ago
Support · by Duc , 1 week ago
2 69 6 days ago
Support · by Colin Hume, 1 week ago
2 61 6 days ago