This may not be the best place for this, and quite probably I need to provide more context by way of example – so I will understand if it gets judged to be ‘not a proper question’ – but here goes…
Background
When I learned some years ago of Marion’s Twig Perversion plugin I found it to be a game changer to how I write templates:
For example:
- I frequently want to loop through elements and objects and update a hash which can then be deployed in multiple ways subject to context
- The same compiled hash (which might itself be the outcome of iterating over myriad Matrix block macros) can subsequently be looped through to generate different ‘views’ of the content in the same page, such as:
- A Matrix-derived sequence of multi-section content and a table of contents for it
- Or a ‘report’ table which also generates a .csv or json of the same data.
So Twig Perversion's ability to return
a variable from a macro instead of printing markup is invaluable.
I can probably dig out better examples of how I have been using this return
ability, but here is a Gist of ‘Reporting all fields in a Craft instance’.
Oh no!… fingers crossed
A recent update to Twig seemed to undermine the on-going ability to do such ‘perverse’ things. As is so often the case, the inimitable Brandon may already have come up with a fix. Which would be great.
A way forward
But the question arises whether using Twig Perversion to do the kind of thing I’m referring to above is sensible. Is it just storing up future templating fragility?
But I’m not sure how to go about engineering what I have become accustomed to doing without creating incredibly verbose templates.
Any thoughts or suggestions welcome.