TB Liquid
TB Liquid is a simple gem that helps manage liquid tags within your Twice Baked application. Specifically, TB Liquid provides reusable code to handle expiring content when a liquid tag has changed.
Use Case
Say you have a SpudPage
object that contains your custom liquid tag called widget
. The widget
tag is based on your Widget
data model. Any time a change is made to your data, you'll need to send a message to SpudPage
asking it to re-process it's content. TB Liquid helps faciliate this.
Code Sample
In this example, :widget
represents the liquid tag name and :name
represents the model property used to fetch the liquid tag value. This can be any column or method on your model object.
class Widget < ActiveRecord::Base
acts_as_spud_liquid_tag :widget, :name
end
class SpudPagePartial < ActiveRecord::Base
acts_as_spud_liquid_content
def postprocess_content
# handle liquid parsing here
end
end
How it Works
The acts_as_spud_liquid_tag
method adds an after_update
hook to your model. The hook does a few things.
- Find any
SpudLiquidTag
object with matching tag name and value properties. - Call
postprocess_content
on any objects attached to any found tags. - That's it!
The key is that the model attached to SpudLiquidTag
must implement a postprocess_content
method. Look at the SpudPagePartial
model in TB CMS for an example implementation.