String Filters

String filters provide means to perform common and useful operations on strings.


append

Adds the specified string to the end of another string.

Input

 {{ "/my/fancy/url" | append: ".html" }}

Output

 /my/fancy/url.html

base64_encode

Creates a base64 representation of a string.

Input

 {{ 'The quick brown fox jumps over the lazy dog' | base64_encode }}

Output

 VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==

camelcase

Creates a camelcase representation of a string.

Input

 {{ 'The quick brown fox jumps over the lazy dog' | camelcase }}

Output

 theQuickBrownFoxJumpsOverTheLazyDog

capitalize

Makes the first character of a string capitalized and converts the remaining characters to lowercase.

Input

 {{ "title" | capitalize }}

Output

 Title

date

Converts a timestamp into another date format. The format for this syntax is the same as strftime.

Input

 {{ article.published_at | date: "%a, %b %d, %y" }}

Output

 Fri, Jul 17, 15

You can set the timezone like this:

Input

 {{ article.published_at | date: "%a, %b %d, %y", 'Europe/Helsinki' }}

downcase

Makes each character in a string lowercase.

Input

 {{ 'DownCaseMe' | downcase }}

Output

 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

 {{ "Have you read 'James & the Giant Peach'?" | escape }}

Output

 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

 {{ "1 < 2 & 3" | escape_once }}

Output

 1 &lt; 2 &amp; 3

handle

Creates a handle from the given string.

Input

 {{ "The quick brown fox" | handle }}

Output

 "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

 {{ 'The quick brown fox jumps over the lazy dog' | hmac_sha1:'secret' }}

Output

 198ea1ea04c435c1246b586a06d5cf11c3ffcda6

hmac_sha256

Calculates SHA-256 hash from the input with using HMAC. Secret is passed as argument for the filter.

Input

 {{ 'The quick brown fox jumps over the lazy dog' | hmac_sha256:'secret' }}

Output

 54cd5b827c0ec938fa072a29b177469c843317b095591dc846767aa338bac600

json

Returns the JSON representation of the object. Strings are quoted automatically.

Input

 {{ product.description | json }}

Output

 "<strong>Fascinating<\/strong><br\/>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

 {{ "          So much room for activities          " | lstrip }}!

Output

 So much room for activities          !

md5

Calculates MD5 hash from the input.

Input

 {{ 'The quick brown fox jumps over the lazy dog' | md5 }}

Output

 9e107d9d372bb6826bd81d3542a419d6

newline_to_br

Replace each newline (\n) with html break.

Input

 {% capture string_with_newlines %}
 Hello
 there
 {% endcapture %}

 {{ string_with_newlines | newline_to_br }}

Output

 <br />
 Hello<br />
 there<br />

prepend

Adds the specified string to the beginning of another string.

Input

 {{ "apples, oranges, and bananas" | prepend: "Some fruit: " }}

Output

 Some fruit: apples, oranges, and bananas

remove

Removes every occurrence of the specified substring from a string.

Input

 {{ "the quick brown fox jumps over the lazy dog" | remove: "the" }}

Output

 quick brown fox jumps over lazy dog

remove_first

Removes only the first occurrence of the specified substring from a string.

Input

 {{ "the quick brown fox jumps over the lazy dog" | remove_first: "the" }}

Output

 quick brown fox jumps over the lazy dog

replace

Replaces every occurrence of the first argument in a string with the second argument.

Input

 {{ "the quick brown fox jumps over the lazy dog" | replace: "the", "foo" }}

Output

 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

 {{ "the quick brown fox jumps over the lazy dog" | replace_first: "quick", "slow" }}

Output

 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

 {{ "          So much room for activities          " | rstrip }}!

Output

           So much room for activities!

sha1

Calculates SHA-1 hash from the input.

Input

 {{ 'The quick brown fox jumps over the lazy dog' | sha1 }}

Output

 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12

sha256

Calculates SHA-256 hash from the input.

Input

 {{ 'The quick brown fox jumps over the lazy dog' | sha256 }}

Output

 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

 {% assign beatles = "John, Paul, George, Ringo" | split: ", " %}

 {% for member in beatles %}
    {{ member }}
 {% endfor %}

Output

   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

 {{ "          So much room for activities          " | strip }}!

Output

 So much room for activities!

strip_html

Removes any HTML tags from a string.

Input

 {{ "Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html }}

Output

 Have you read Ulysses?

strip_newlines

Removes any newline characters (line breaks) from a string.

Input

 {% capture string_with_newlines %}
 Hello
 there
 {% endcapture %}

 {{ string_with_newlines | strip_newlines }}

Output

 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

 {{ "Ground control to Major Tom." | truncate: 20 }}

Output

 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

 {{ "Ground control to Major Tom." | truncate: 25, ", and so on" }}

Output

 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

 {{ "Ground control to Major Tom." | truncate: 20, "" }}

Output

 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

 {{ "Ground control to Major Tom." | truncatewords: 3 }}

Output

 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

 {{ "Ground control to Major Tom." | truncatewords: 3, "--" }}

Output

 Ground control to--

No ellipsis You can avoid showing trailing characters by passing a blank string as the second argument.

Input

 {{ "Ground control to Major Tom." | truncatewords: 3, "" }}

Output

 Ground control to

upcase

Makes each character in a string uppercase. It has no effect on strings which are already all uppercase.

Input

 {{ "The quick brown fox jumps over the lazy dog" | upcase }}

Output

 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

 {{ "<title>This is a test & example string</title>" | xml_safestring }}

Output

 <title>This is a test &#38; example string</title>