Accessing Context in Liquid Drops
Have you ever thought it would be nice to be able to access the context from within a custom Liquid::Drop when using the Liquid template system?
Turns out, you can.
On Drops there is a method ‘context=’ which gets set by the parser when it comes across a Drop object. From within the drop, all you need to do is access the @context variable and you’re away.
Why this isn’t advertised more is beyond me, or maybe I’m just an idiot and missed it, but I couldn’t see how to do this at all until I decided to sit down and essentially try and write it myself and delved into the code in Liquid that deals with processing variables. Turns out it’s already there and it works a charm. Nice for writing Drops that behave differently in different contexts, depending on register settings and the like.
Anyway, I thought it was useful, hope this helps someone else before they pull all their hair out.
I was halfway bald when I found this. Thank you!!
No problem, I know how you feel. Lack of documentation is the bane of my existence!