Twig Extensions on a Streamed theme

“How do they work ?”

Dear Gravists,
Have you ever worked with Twig Extensions and especially on a streamed custom theme ?
I’m trying to see if the three Twig Extensions in the Project-Space theme by @OleVik are working well and deduce what they are doing… (apparently they seem to be used as Twig filters) and I want to find a way to “vardump” their infos.

But it looks like they’re either not loaded or inactive.
Does anybody know what to “vardump” to find the available Twig Extensions ?

They’re located here : grav-theme-project-space/twig at master · OleVik/grav-theme-project-space · GitHub

Actually I could read the Twig / Grav manuals and try to recreate the whole theme from scratch… but I feel more comfortable to Stream, since I prefer to stay on the design/customization side.

Thanks for your time !

If I were ever so gracious I would have actually documented them inline. I shall endeavor to do so for the next version. But let’s look at an example:

InAnyArray, yielding the function in_array_any() for Twig, is an extremely thin wrapper for PHP’s array_intersect. We look for the first parameter - the needles - in the second parameter - the haystacks. The double exclamation marks is a dirtier way of casting the result as a boolean - always TRUE or FALSE.

It is loaded in the onTwigExtensions-event, which is a bit old-school. The theme was developed before autoloading became normalized in Grav’s ecosystem, and is itself a thin change to the Mediator theme. I would be doubtful that child-theme inherits extensions unless those are autoloaded, but it is a justly useful upgrade to the parent-theme to alleviate the need for double-inclusion.

I would think that $this->grav['twig'] has an extensions-property, but I may remember incorrectly.

1 Like

Thank you very much for your answer @OleVik. It’s always a pleasure.

So according to you, how should I make inherit my theme from your Twig Extensions in Project-Space ? I think I’m very interested in the “Url_Decode” filter. If it does transform the
/?category[]=XYZ into /category:XYZ

Take care
Medi

If your theme is a child-theme of Project Space, its PHP-file will be something like class ProjectLoft extends ProjectSpace. Given that, Project Space will already be enabled, and its autoloaded classes and exposed methods available to you. This should include the extensions registered by the parent class in the onTwigExtensions-event. So a child-theme generated by php bin/plugin devtools is pretty much ready to go in that regard.

|url_decode is, like in_array_any(), an extremely thin wrapper around PHP’s urldecode(), needed because it wasn’t already available in Twig or Grav. Its main use is to translate strings like “My Category” into My%20Category for safe transport via GET.

The form in templates/partials/tools.html.twig sends data in the /?category[]=XYZ form which templates/default.html.twig interprets using Grav’s URI.Param’s. In between the sending and receiving of this data, the theme handles the redirect into the more pleasant form. Thus, you get /projects/category:Idea/color:Blue instead of /projects/?category[]=Idea&color[]=Blue.

1 Like

Thank you very much @OleVik !
Now I understand so many new things.

Regarding this part of your answer :

I suppose something is bugging here on my side. Because I’m not getting the “conversion” from your example. The URI stays as follows : /?category[]=Idea&color[]=Blue

But with your latest updates by passing {{ page.url }} instead of the variable called {{ action }} seems to avoid adding ?redirect=true after hitting APPLY. What would you suggest me to do ? Is there something I should check in the config folder ?

I think we’re getting very close to the solution !
Thank you for your time,
Medi

After some research and rubberducksplaning my issue out loud :smile:

I finally got it working !!
While trying different work-arounds I commented out this line:
<input type="hidden" id="redirect" name="redirect" value="true">
before the submit button

And so noticing the fact that ?redirect=true was missing in the URI (like I said in the previous post)… I realized it was needed.

Thank you very much !