# String Filters These filters provide string manipulation functions for Liquid templates. They allow you to modify, format, and transform strings in various ways, such as encoding, case conversion, and hashing. Use these filters to prepare and display text content dynamically. ## append Adds the specified string to the end of another string. **Input** ```liquid {{ "/my/fancy/url" | append: ".html" }} ``` **Output** ```text /my/fancy/url.html ``` --- ## base64_encode Creates a base64 representation of a string. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | base64_encode }} ``` **Output** ```text VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw== ``` --- ## camelcase Creates a camelcase representation of a string. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | camelcase }} ``` **Output** ```text theQuickBrownFoxJumpsOverTheLazyDog ``` --- ## capitalize Makes the first character of a string capitalized and converts the remaining characters to lowercase. **Input** ```liquid {{ "title" | capitalize }} ``` **Output** ```text Title ``` --- ## date Converts a timestamp into another date format. The format for this syntax is the same as [strftime](http://strftime.net/). **Input** ```liquid {{ article.published_at | date: "%a, %b %d, %y" }} ``` **Output** ```text Fri, Jul 17, 15 ``` You can set the timezone like this: **Input** ```liquid {{ article.published_at | date: "%a, %b %d, %y", 'Europe/Helsinki' }} ``` --- ## downcase Makes each character in a string lowercase. **Input** ```liquid {{ 'DownCaseMe' | downcase }} ``` **Output** ```text downcaseme ``` --- ## escape Escapes a string by replacing characters with escape sequences (so that the string can be used in a URL, for example). It doesn’t change strings that don’t have anything to escape. **Input** ```liquid {{ "Have you read 'James & the Giant Peach'?" | escape }} ``` **Output** ```text Have you read 'James & the Giant Peach'? ``` --- ## escape_once Escapes a string without changing existing escaped entities. It doesn’t change strings that don’t have anything to escape. **Input** ```liquid {{ "1 < 2 & 3" | escape_once }} ``` **Output** ```text 1 < 2 & 3 ``` --- ## handle Creates a handle from the given string. **Input** ```liquid {{ "The quick brown fox" | handle }} ``` **Output** ```text "the-quick-brown-fox" ``` --- ## hmac_sha1 Calculates SHA-1 hash from the input with using HMAC. Secret is passed as argument for the filter. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | hmac_sha1:'secret' }} ``` **Output** ```text 198ea1ea04c435c1246b586a06d5cf11c3ffcda6 ``` --- ## hmac_sha256 Calculates SHA-256 hash from the input with using HMAC. Secret is passed as argument for the filter. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | hmac_sha256:'secret' }} ``` **Output** ```text 54cd5b827c0ec938fa072a29b177469c843317b095591dc846767aa338bac600 ``` --- ## json Returns the JSON representation of the object. Strings are quoted automatically. **Input** ```liquid {{ product.description | json }} ``` **Output** ```text "Fascinating<\/strong>It works just I expected it to work :)" ``` --- ## lstrip Removes all whitespace (tabs, spaces, and newlines) from the left side of a string. It does not affect spaces between words. **Input** ```liquid {{ " So much room for activities " | lstrip }}! ``` **Output** ```text So much room for activities ! ``` --- ## md5 Calculates MD5 hash from the input. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | md5 }} ``` **Output** ```text 9e107d9d372bb6826bd81d3542a419d6 ``` --- ## newline_to_br Replace each newline (\n) with html break. **Input** ```liquid {% capture string_with_newlines %} Hello there {% endcapture %} {{ string_with_newlines | newline_to_br }} ``` **Output** ```text
Hello
there
``` --- ## prepend Adds the specified string to the beginning of another string. **Input** ```liquid {{ "apples, oranges, and bananas" | prepend: "Some fruit: " }} ``` **Output** ```text Some fruit: apples, oranges, and bananas ``` --- ## remove Removes every occurrence of the specified substring from a string. **Input** ```liquid {{ "the quick brown fox jumps over the lazy dog" | remove: "the" }} ``` **Output** ```text quick brown fox jumps over lazy dog ``` --- ## remove_first Removes only the first occurrence of the specified substring from a string. **Input** ```liquid {{ "the quick brown fox jumps over the lazy dog" | remove_first: "the" }} ``` **Output** ```text quick brown fox jumps over the lazy dog ``` --- ## replace Replaces every occurrence of the first argument in a string with the second argument. **Input** ```liquid {{ "the quick brown fox jumps over the lazy dog" | replace: "the", "foo" }} ``` **Output** ```text foo quick brown fox jumps over foo lazy dog ``` --- ## replace_first Replaces only the first occurrence of the first argument in a string with the second argument. **Input** ```liquid {{ "the quick brown fox jumps over the lazy dog" | replace_first: "quick", "slow" }} ``` **Output** ```text The slow brown fox jumps over the lazy dog ``` --- ## rstrip Removes all whitespace (tabs, spaces, and newlines) from the right side of a string. It does not affect spaces between words. **Input** ```liquid {{ " So much room for activities " | rstrip }}! ``` **Output** ```text So much room for activities! ``` --- ## sha1 Calculates SHA-1 hash from the input. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | sha1 }} ``` **Output** ```text 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 ``` --- ## sha256 Calculates SHA-256 hash from the input. **Input** ```liquid {{ 'The quick brown fox jumps over the lazy dog' | sha256 }} ``` **Output** ```text d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592 ``` --- ## split Divides a string into an array using the argument as a separator. `split` is commonly used to convert comma-separated items from a string to an array. **Input** ```liquid {% assign beatles = "John, Paul, George, Ringo" | split: ", " %} {% for member in beatles %} {{ member }} {% endfor %} ``` **Output** ```text John Paul George Ringo ``` --- ## strip Removes all whitespace (tabs, spaces, and newlines) from both the left and right sides of a string. It does not affect spaces between words. **Input** ```liquid {{ " So much room for activities " | strip }}! ``` **Output** ```text So much room for activities! ``` --- ## strip_html Removes any HTML tags from a string. **Input** ```liquid {{ "Have you read Ulysses?" | strip_html }} ``` **Output** ```text Have you read Ulysses? ``` --- ## strip_newlines Removes any newline characters (line breaks) from a string. **Input** ```liquid {% capture string_with_newlines %} Hello there {% endcapture %} {{ string_with_newlines | strip_newlines }} ``` **Output** ```text Hellothere ``` --- ## truncate Shortens a string down to the number of characters passed as an argument. If the specified number of characters is less than the length of the string, an ellipsis (…) is appended to the string and is included in the character count. **Input** ```liquid {{ "Ground control to Major Tom." | truncate: 20 }} ``` **Output** ```text Ground control to... ``` **Custom ellipsis** `truncate` takes an optional second argument that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (…), but you can specify a different sequence. The length of the second argument counts against the number of characters specified by the first argument. For example, if you want to truncate a string to exactly 10 characters, and use a 3-character ellipsis, use 13 for the first argument of `truncate`, since the ellipsis counts as 3 characters. **Input** ```liquid {{ "Ground control to Major Tom." | truncate: 25, ", and so on" }} ``` **Output** ```text Ground control, and so on ``` **No ellipsis** You can truncate to the exact number of characters specified by the first argument and avoid showing trailing characters by passing a blank string as the second argument. **Input** ```liquid {{ "Ground control to Major Tom." | truncate: 20, "" }} ``` **Output** ```text Ground control to Ma ``` --- ## truncatewords Shortens a string down to the number of words passed as an argument. If the specified number of words is less than the number of words in the string, an ellipsis (…) is appended to the string. **Input** ```liquid {{ "Ground control to Major Tom." | truncatewords: 3 }} ``` **Output** ```text Ground control to... ``` **Custom ellipsis** `truncatewords` takes an optional second argument that specifies the sequence of characters to be appended to the truncated string. By default this is an ellipsis (…), but you can specify a different sequence. **Input** ```liquid {{ "Ground control to Major Tom." | truncatewords: 3, "--" }} ``` **Output** ```text Ground control to-- ``` **No ellipsis** You can avoid showing trailing characters by passing a blank string as the second argument. **Input** ```liquid {{ "Ground control to Major Tom." | truncatewords: 3, "" }} ``` **Output** ```text Ground control to ``` --- ## upcase Makes each character in a string uppercase. It has no effect on strings which are already all uppercase. **Input** ```liquid {{ "The quick brown fox jumps over the lazy dog" | upcase }} ``` **Output** ```text THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG ``` --- ## xml_safestring Converts any special characters from the string making it valid to be used in xml. **Input** ```liquid {{ "This is a test & example string" | xml_safestring }} ``` **Output** ```text This is a test & example string ```