What have I done this week?
Slow week. Watching ESA Winter and doing some code cleanup. I’m trying to eliminate some older PHP code that was responsible for printing HTML, because we’ve had Twig templates for a while now…
One of the trickier things to do was replace a Pagination class responsible for printing a range of page buttons. I wrote this class to accept a function which would return a URL for a given page number, which allows us to be flexible with how we handle our pagination (some pages use a
page request parameter, others use semantic URLs). This is instantiated in the controller and passed as a parameter to the page template. I was hoping to achieve the same with Twig’s macro feature, but there are a few limitations there:
- Macros cannot access variables in the global scope, so I can’t do what I wanted and override a “page” key of a parameter hash without explicitly passing it in and limiting flexibility.
- Template contexts bound via
importand their macro properties don’t seem to behave in the same way as other template variables. If you try to pass these to a template with
include, they appear to be ignored, and aren’t passed at all.
Twig macros are probably the weakest point of Twig; they are both limited and verbose. Generally I find it quicker and easier to just write a template that can be included with
include. In the end I opted to using a template string that uses Twig’s
replace filter. Our
%num% placeholder is replaced with a page number, and we can use filters to properly URL encode other parameters without PHP. It’s not perfect, but it’s helped separate our concerns so PHP is only being used for our controllers, models, and the glue in between.
Non-backpack.tf related: I dist-upgraded my Debian machine from Stretch to Buster. Some stuff broke because certain packages weren’t reinstalled (including Steam for some reason) but I managed to get it all running again. I made Roboto my default system font and everything is pretty.
What am I going to do next?
I might do some writeups on the backpack.tf internals. I think there’s some interesting stuff in here we need to talk about.