Keyword literals

Mechanic's Liquid implementation includes three new literals:

  • newline"\n"
  • hash{} 
  • array[] 

Use newline  to more easily navigate strings, and hash and array to quickly construct your own data structures.

Because these are literals, these keywords cannot be overwritten as variables.

Hash and array

These literals may be used for initializing variables, and then assigning within them. Learn more about the assign tag.

Why "hash"?

The term comes from Ruby, the language beneath Liquid. In Ruby (docs), "A Hash is a dictionary-like collection of unique keys and their values." In Mechanic's Liquid implementation, a hash can only have string keys.

Newline

Besides its natural uses in building strings, newline has a special purpose within these string filters:

  • newline_to_br – When used with newline, this filter will replace instances of "\r\n", and then instances of "\n".
  • replace – When used with newline as its first argument, this filter will replace instances of "\r\n", and then instances of "\n".
  • split – When used with newline, this filter will split by "\r\n" when present, and then by "\n".

Technical note: Under the hood, each of these scenarios uses the regular expression /\r?\n/.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.