LiquidJS also provides a mechanism similar to Shopify Drops, allowing template authors to incorporate custom functionality in resolving variable values.
Drop for JavaScriptDrop interface is implemented differently in LiquidJS compared to built-in filters and other template functionalities. Since LiquidJS runs in JavaScript, custom Drops need to be reimplemented in JavaScript anyway. There’s no compatibility between JavaScript classes and Ruby classes.
Basic Usage
1 | import { Liquid, Drop } from 'liquidjs' |
As shown above, besides reading properties from context scopes, you can also call methods. You only need to create a custom class inherited from Drop.
Async MethodsLiquidJS is fully async-friendly. You can safely return a Promise in your Drop methods or define your methods in Drop as
async.
liquidMethodMissing
For cases when there isn’t a fixed set of properties, you can leverage liquidMethodMissing to dynamically resolve the value of a variable name.
1 | import { Liquid, Drop } from 'liquidjs' |
liquidMethodMissing supports Promise, meaning you can make async calls within it. A more useful case can be fetching the value dynamically from the database. By using Drops, you can avoid hardcoding each property into the context. For example:
1 | import { Liquid, Drop } from 'liquidjs' |
valueOf
Drops can implement a valueOf() method, the return value of which can be used to replace itself in the output. For example:
1 | import { Liquid, Drop } from 'liquidjs' |
toLiquid
toLiquid() is not a method of Drop, but it can be used to return a Drop. In cases where you have a fixed structure in the context that cannot change its values, you can implement toLiquid() to let LiquidJS use the returned value instead of itself to render the templates.
1 | import { Liquid, Drop } from 'liquidjs' |
Of course, you can also return a PersonDrop instance in the toLiquid() method and implement this functionality within PersonDrop:
1 | import { Liquid, Drop } from 'liquidjs' |
toLiquid()vs.valueOf()Difference
valueOf()is typically used to define how the current variable should be rendered, whiletoLiquid()is often used to convert an object into a Drop or another scope provided to the template.valueOf()is a method exclusive to Drops; whereastoLiquid()can be used on any scope object.valueOf()is called when the variable itself is about to be rendered, replacing itself; whereastoLiquid()is called when its properties are about to be read.
Special Drops
LiquidJS itself implements several built-in drops to facilitate template writing. This part is compatible with Shopify Liquid, as we need templates to be portable.
blank
Useful to check whether a string variable is false, null, undefined, an empty string, or a string containing only blank characters.
1 | {% unless author == blank %} |
empty
Useful to check if an array, string, or object is empty.
1 | {% if authors == empty %} |
emptyimplementationFor arrays and strings, LiquidJS checks their
.lengthproperty. For objects, LiquidJS callsObject.keys()to check whether they have keys.
nil
nil Drop is used to check whether a variable is not defined or defined as null or undefined, essentially equivalent to JavaScript == null check.
1 | {% if nonexistent == nil %} |
Other Drops
There are still several Drops for specific tags, like forloop, tablerowloop, block, which are covered by respective tag documents.