How Liquid methods execute logic, and the categories of methods available in Marketpath Liquid.
Methods are the mechanism by which templates are able to execute logic while rendering a page. All methods start with '{%' and end with '%}', and the commands between those characters determine how the template is processed.
Some methods are used to control what is or isn't output as part of the page (such as conditionals, comments, and includes). Some methods are used to store or modify information in objects. Some methods are used for repetition. And some methods are just shortcuts intended to make your job easier as a template developer.
Most methods take arguments written as name:value pairs after the method name. Two prefixes change where an argument comes from, and both are worth recognising when reading someone else’s template.
A reference argument puts an ampersand in front of an argument name, which reads the name from a variable instead of using it literally. This lets a template decide an argument name while it runs:
{% var headername = "X-Custom-Username" %} {% set_header &headername:session["Username"] %}
A variable argument puts an asterisk in front of a variable name, which supplies the method’s arguments from that variable instead of writing them out. This is useful when the arguments differ between page loads and spelling out every combination with {% if %} would be impractical:
Which form is allowed depends on the part of the method call, not on the method as a whole:
• An assignment target accepts the reference form, as in {% blog_posts &listname = limit:3 %}. • An argument name accepts the reference form, as in {% set_header &headername:value %}. • A whole argument list accepts the variable form, as in {% create_dictionary settings = *settings %}. • A list of names accepts the variable form, as in {% unset_dictionary settings *keys %}. • Argument values and positional values accept neither, apart from {% cycle %} and the cookie name on {% set_cookie %}.
Methods that take a single value, such as {% redirect %} and {% set_title %}, accept neither form. Object shortcut methods such as {% article %} accept the reference form on their assignment target but not the variable form.
One difference matters when combining the variable form with ordinary arguments. Methods that build a dictionary reject the same argument name twice, so you cannot expand a variable and then override one of its entries alongside it. The object list methods collect repeated names instead of rejecting them.
See the examples on the set_header and create_dictionary pages for each form in context.
Browse method categories below. Object shortcut and list shortcut methods are also documented on their object pages under Objects.
ExampleHow to use the increment and decrement methodsUse the increment and decrement methods to add or subtract 1 from a variable and output the new value.
To update a variable without outputting, use set with the plus/minus filter instead of the increment/decrement tags. You may need to use the to_number and default filters to ensure the variable is a number before adding or subtracting if you do not already know that it is a number.