[SOLVED] Does anyone understand Twig macros?

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
User avatar
Gumboots
Registered User
Posts: 971
Joined: Fri Oct 11, 2019 1:59 am

Re: [SOLVED] Does anyone understand Twig macros?

Post by Gumboots »

It doesn't work? Why not? I just tested it. Seems to work perfectly.
🇺🇦 Слава Україні! 🇺🇦 Героям слава! 🇺🇦
User avatar
cabot
Jr. Style Validator
Posts: 1198
Joined: Sat Jan 07, 2012 4:16 pm

Re: [SOLVED] Does anyone understand Twig macros?

Post by cabot »

Well, it's probably because I'm messing around with my translator. :lol:
And perhaps also because I prefer the flexibility of the function (which does nothing here).
User avatar
Gumboots
Registered User
Posts: 971
Joined: Fri Oct 11, 2019 1:59 am

Re: [SOLVED] Does anyone understand Twig macros?

Post by Gumboots »

I tested it with this in overall_header.html:

Code: Select all

		{% include ['language/' ~ S_USER_LANG ~ '/posting_editor_incl.twig', 'language/en-gb/posting_editor_incl.twig'] %}
Renamed the files, to keep the old macros under the original file names, and added "_incl" to the new files that have the DEFINE's in them.

Then in posting_editor.html it's just:
<!-- IF S_LINKS_ALLOWED and S_MAGIC_URL_CHECKED -->
<div>
{# @Gumbootz: custom language string. #}
{{ definition.GB_URLPAR_DISABLED }}
</div>
<!-- ENDIF -->
<!-- IF S_BBCODE_ALLOWED and S_BBCODE_CHECKED -->
<div>
{# @Gumbootz: custom language string. #}
{{ definition.GB_BBCODE_DISABLED }}
</div>
<!-- ENDIF -->
<!-- IF S_SMILIES_ALLOWED and S_SMILIES_CHECKED -->
<div>
{# @Gumbootz: custom language string. #}
{{ definition.GB_SMILEY_DISABLED }}
</div>
<!-- ENDIF -->
It works. Same tests as before: English, Estonian and Romanian, with no files for Estonian.
🇺🇦 Слава Україні! 🇺🇦 Героям слава! 🇺🇦
User avatar
Gumboots
Registered User
Posts: 971
Joined: Fri Oct 11, 2019 1:59 am

Re: [SOLVED] Does anyone understand Twig macros?

Post by Gumboots »

cabot wrote: Sat Apr 12, 2025 11:33 amAnd perhaps also because I prefer the flexibility of the function (which does nothing here).
The Twig docs say the function is cooler and groovier, so I suppose I might as well use it. A couple of extra brackets aren't going to kill me. I'll edit the code now...

ETA: Yup, that works too. We now have more ways of doing it than anyone needs. :lol:
🇺🇦 Слава Україні! 🇺🇦 Героям слава! 🇺🇦
User avatar
Gumboots
Registered User
Posts: 971
Joined: Fri Oct 11, 2019 1:59 am

Re: [SOLVED] Does anyone understand Twig macros?

Post by Gumboots »

Just thought: for extra diapers in emergencies it really should have ignore_missing thrown into the mix, just in case some muppet loses their en-gb file.

This is where the Twig docs lead to fun and games. They don't give you the required syntax for adding it to an include function that has a list of templates to check for. They give the syntax for when you are only checking for one file...

Code: Select all

{{ include('sidebar.html.twig', ignore_missing: true) }}
...but they do not give you the syntax for when you are checking against a list of files. This, by implication, might lead you to try several variations on the syntax they gave as an example. White pages of death ensue. Giggles all round.

As it turns out (and I found this by trial and error) the correct syntax is this:

Code: Select all

{{ include(['language/' ~ S_USER_LANG ~ '/styleStrings.twig', 'language/en-gb/styleStrings.twig'], ignore_missing = true) }}
That will simply skip the include if you are somehow clever enough to lose all your styleStrings files. No white pages of death. Just missing text. Much less indigestion for muppets. :D
🇺🇦 Слава Україні! 🇺🇦 Героям слава! 🇺🇦
User avatar
Gumboots
Registered User
Posts: 971
Joined: Fri Oct 11, 2019 1:59 am

Re: [SOLVED] Does anyone understand Twig macros?

Post by Gumboots »

Aha. Just thought of something to test. Using the DEFINE option introduces a hazard for beginners, because it necessitates using correct PHP syntax for escaping apostrophes. Not a problem once you know about it, but will kill the whole site if you miss one.

This works:

Code: Select all

{% DEFINE GB_URLPAR_DISABLED = '<strong>You have disabled automatic parsing of URL\'s.</strong>' %}
This will give you a white page of death:

Code: Select all

{% DEFINE GB_URLPAR_DISABLED = '<strong>You have disabled automatic parsing of URL's.</strong>' %}
So in that respect the macros option may be safer for some people, even though it has other drawbacks.
🇺🇦 Слава Україні! 🇺🇦 Героям слава! 🇺🇦

Return to “phpBB Custom Coding”