### Install trmnl-liquid Gem (Bash)
Source: https://github.com/usetrmnl/trmnl-liquid/blob/main/README.md
Provides the command to install the `trmnl-liquid` gem directly using the `gem` command, useful when not using Bundler.
```bash
gem install trmnl-liquid
```
--------------------------------
### TRMNL Liquid Environment Setup
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
This Ruby code snippet demonstrates how to build and configure the TRMNL Liquid environment, which is necessary for using the custom filters.
```ruby
require 'trmnl/liquid'
# Build the TRMNL Liquid environment
environment = TRMNL::Liquid.build_environment
```
--------------------------------
### Include Optional ActionView Gem (Ruby)
Source: https://github.com/usetrmnl/trmnl-liquid/blob/main/README.md
Demonstrates how to include the `actionview` gem as an optional peer dependency. The `trmnl-liquid` gem can leverage ActionView helpers if this gem is present.
```ruby
# optional peer dependency
gem "actionview", "~> 8.0"
```
--------------------------------
### Define and Render Custom Templates
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Demonstrates defining reusable template chunks using the `{% template %}` tag and rendering them with the `{% render %}` tag. Templates are stored in an in-memory file system.
```liquid
{% template say_hello %}
Why hello there, {{ name }}!
{% endtemplate %}
{% template user_card %}
{{ user.name }}
Age: {{ user.age }}
{% endtemplate %}
{% render "say_hello", name: "General Kenobi" %}
{% render "user_card", user: current_user %}
```
```ruby
require 'trmnl/liquid'
environment = TRMNL::Liquid.build_environment
template = <<~LIQUID
{% template my_template %}Hello, {{ name }}{% endtemplate %}
{% render 'my_template', name: 'world' %}
{% render 'my_template', name: name %}
LIQUID
result = Liquid::Template.parse(template, environment: environment).render("name" => "George")
# => "Hello, world\nHello, George"
```
--------------------------------
### Build TRMNL Liquid Environment
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Builds a configured Liquid environment with TRMNL filters, tags, and a custom file system. This is the main entry point for using the gem. It can be customized further with a block.
```ruby
require 'trmnl/liquid'
# Basic usage - build environment with TRMNL filters and tags
environment = TRMNL::Liquid.build_environment
# Parse and render a template
markup = "Hello {{ count | number_with_delimiter }} people!"
template = Liquid::Template.parse(markup, environment: environment)
rendered = template.render("count" => 1337)
# => "Hello 1,337 people!"
# Advanced usage - customize the environment further
environment = TRMNL::Liquid.build_environment do |env|
env.error_mode = :strict
# Add additional custom filters or tags
end
```
--------------------------------
### Add trmnl-liquid Gem to Gemfile (Bash)
Source: https://github.com/usetrmnl/trmnl-liquid/blob/main/README.md
Provides the command to add the `trmnl-liquid` gem to your application's Gemfile using Bundler.
```bash
bundle add trmnl-liquid
```
--------------------------------
### Parse Liquid Template with TRMNL Environment (Ruby)
Source: https://github.com/usetrmnl/trmnl-liquid/blob/main/README.md
Demonstrates how to parse a Liquid template using the TRMNL::Liquid.build_environment for custom filters and tags. This approach is safer than global registration and is compatible with Liquid gem v5.6.0 and later.
```ruby
require 'trmnl/liquid'
markup = "Hello {{ count | number_with_delimiter }} people!"
environment = TRMNL::Liquid.build_environment # same arguments as Liquid::Environment.build
template = Liquid::Template.parse(markup, environment: environment)
rendered = template.render(count: 1337)
# => "Hello 1,337 people!"
```
--------------------------------
### Define and Render Reusable Template Chunks (Liquid)
Source: https://github.com/usetrmnl/trmnl-liquid/blob/main/README.md
Illustrates the usage of the `{% template %}` tag to define reusable markup sections and the `{% render %}` tag to include them within a Liquid template. This allows for modular template design.
```liquid
{% template say_hello %}
Why hello there, {{ name }}!
{% endtemplate %}
{% render "say_hello", name: "General Kenobi" %}
```
--------------------------------
### Include Optional Internationalization Gems (Ruby)
Source: https://github.com/usetrmnl/trmnl-liquid/blob/main/README.md
Shows how to include optional peer dependencies for internationalization, specifically `rails-i18n` and `trmnl-i18n`, which are required for filters like `number_to_currency` and `l_word`.
```ruby
# optional peer dependencies
gem "rails-i18n", "~> 8.0"
gem "trmnl-i18n", github: "usetrmnl/trmnl-i18n", branch: "main" # recommended for the latest changes
```
--------------------------------
### Number Formatting with `number_to_currency`
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Formats numbers as currency with customizable unit, delimiter, separator, and precision. It supports locale-specific formatting.
```liquid
{{ 10420 | number_to_currency }}
{{ 152350.69 | number_to_currency: "£" }}
{{ 1234.57 | number_to_currency: "£", ".", "," }}
{{ 567 | number_to_currency: "sv" }}
{{ 99.99 | number_to_currency: "€", ".", ",", 2 }}
```
--------------------------------
### Number Formatting with `number_with_delimiter`
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Formats numbers with thousands separators. It accepts optional arguments for the thousands delimiter, decimal separator, and precision.
```liquid
{{ 1234 | number_with_delimiter }}
{{ 1234 | number_with_delimiter: "." }}
{{ 1234.57 | number_with_delimiter: " ", "," }}
{{ 1000000 | number_with_delimiter }}
```
--------------------------------
### Liquid Template Definition and Rendering in Ruby
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Defines a Liquid template string with various components like stats cards and user rows, then renders it with provided data using the TRMNL Liquid gem. This demonstrates the core templating and rendering process.
```ruby
require "liquid"
require "trmnl_liquid"
environment = TRMNL::Liquid.build_environment
markup = <<~LIQUID
{% template stats_card %}
{% for user in high_value %}
{% render "user_row", user: user %}
{% endfor %}
{{ dashboard_url | qr_code: 8, "h" }}
LIQUID
data = {
"total_users" => 15420,
"revenue" => 284350.99,
"users" => [
{"name" => "Alice", "balance" => 2500},
{"name" => "Bob", "balance" => 750},
{"name" => "Charlie", "balance" => 1200}
],
"dashboard_url" => "https://app.usetrmnl.com/dashboard"
}
template = Liquid::Template.parse(markup, environment: environment)
html = template.render(data)
puts html
```
--------------------------------
### Collection Filtering with `find_by`
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Finds the first object in a collection that matches a given key-value pair. An optional fallback value can be provided if no match is found.
```liquid
{{ collection | find_by: 'name', 'Ryan' }}
{{ collection | find_by: 'name', 'Unknown', 'Not Found' }}
```
--------------------------------
### Collection Filtering with `sample`
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Returns a random element from an array. This filter is useful for selecting a random item from a list or collection.
```liquid
{{ "1,2,3,4,5" | split: "," | sample }}
{{ "red,green,blue" | split: "," | sample }}
```
--------------------------------
### Convert Markdown to HTML with markdown_to_html
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `markdown_to_html` filter converts Markdown formatted text into HTML. It utilizes the Redcarpet library for parsing and rendering Markdown.
```liquid
{{ "This is **bold** and *italic*" | markdown_to_html }}
{{ description | markdown_to_html }}
```
--------------------------------
### Format Dates with Ordinal Suffixes using ordinalize
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `ordinalize` filter formats dates by appending the correct ordinal day suffix (e.g., 1st, 2nd, 3rd). It can be used with custom date formats to include the ordinal day.
```liquid
{{ "2025-10-02" | ordinalize: "%A, %B <>, %Y" }}
{{ "2025-12-31" | ordinalize: "%A, %b <>" }}
{{ "2025-01-01" | ordinalize: "<> of %B" }}
```
--------------------------------
### Collection Filtering with `group_by`
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Groups a collection of objects by a specified key. The output is a hash where keys are the grouping values and values are arrays of matching objects.
```liquid
{% assign grouped = collection | group_by: 'age' %}
{{ grouped }}
```
```ruby
environment = TRMNL::Liquid.build_environment
data = {
"collection" => [
{"name" => "Ryan", "age" => 35},
{"name" => "Sara", "age" => 29},
{"name" => "Jimbob", "age" => 29}
]
}
template = "{{ collection | group_by: 'age' }}"
result = Liquid::Template.parse(template, environment: environment).render(data)
# => {35=>[{"name"=>"Ryan", "age"=>35}], 29=>[{"name"=>"Sara", "age"=>29}, {"name"=>"Jimbob", "age"=>29}]}
```
--------------------------------
### Generate SVG QR Code with qr_code
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `qr_code` filter generates an SVG QR code from input data. It allows customization of the module size and error correction level.
```liquid
{{ "https://usetrmnl.com" | qr_code }}
{{ "Hello World" | qr_code: 15 }}
{{ url | qr_code: 11, "h" }}
```
```ruby
# QR code error correction levels:
# "l" - Low (~7% recovery)
# "m" - Medium (~15% recovery)
# "q" - Quartile (~25% recovery)
# "h" - High (~30% recovery, default)
template = '{{ "Test Data" | qr_code: 11, "m" }}'
result = Liquid::Template.parse(template, environment: environment).render({})
# => ""
```
--------------------------------
### Collection Filtering with `where_exp`
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
Filters a collection based on a Liquid expression, similar to Jekyll's `where_exp` filter. It evaluates the expression for each item in the collection.
```liquid
{% assign nums = "1,2,3,4,5" | split: "," | map_to_i %}
{{ nums | where_exp: "n", "n >= 3" }}
{% assign adults = users | where_exp: "user", "user.age >= 18" %}
{% for user in adults %}
{{ user.name }}
{% endfor %}
```
--------------------------------
### Localize Dates with l_date
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `l_date` filter localizes a given date string according to I18n translations and specified format. It allows for displaying dates in different languages and formats.
```liquid
{{ "2025-01-11" | l_date: "%y %b" }}
{{ "2025-01-11" | l_date: "%y %b", "ko" }}
{{ "2025-03-15" | l_date: "%A, %B %d", "es" }}
```
--------------------------------
### Convert Collection to Integers with map_to_i
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `map_to_i` filter converts all elements in a collection to integers. It is useful for performing numerical operations on string representations of numbers within a collection.
```liquid
{% assign nums = "5, 4, 3, 2, 1" | split: ", " | map_to_i %}
{{ nums }}
```
--------------------------------
### Calculate Past Dates with days_ago
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `days_ago` filter calculates a date a specified number of days in the past. It supports optional timezone arguments for accurate date calculations across different regions.
```liquid
{{ 3 | days_ago }}
{{ 5 | days_ago | date: "%b %d, %Y" }}
{{ 7 | days_ago: "America/New_York" }}
```
--------------------------------
### Pluralize Words with pluralize
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `pluralize` filter adjusts a word to its plural form based on a given count. It can also accept a custom plural form and leverages Rails inflections for common cases.
```liquid
{{ "book" | pluralize: 0 }}
{{ "book" | pluralize: 1 }}
{{ "book" | pluralize: 2 }}
{{ "person" | pluralize: 4 }}
{{ "person" | pluralize: 4, plural: "humans" }}
```
--------------------------------
### Convert Object to JSON String with json
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `json` filter converts a Liquid object into a JSON string representation. This is useful for outputting data structures in a standard JSON format.
```liquid
{{ data | json }}
```
```ruby
data = {"data" => [{"a" => 1, "b" => "c"}, "d"]}
template = "{{ data | json }}"
result = Liquid::Template.parse(template, environment: environment).render(data)
# => "[{"a":1,"b":"c"},"d"]"
```
--------------------------------
### Parse JSON String to Liquid Object with parse_json
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `parse_json` filter parses a JSON string and converts it into a Liquid object, allowing access to its properties and elements within the template.
```liquid
{% assign value = data | parse_json %}
{{ value.name }}
{{ value.items | size }}
```
```ruby
data = {"data" => '{"name":"John","age":30}'}
template = "{% assign value = data | parse_json %}{{ value.name }} is {{ value.age }}"
result = Liquid::Template.parse(template, environment: environment).render(data)
# => John is 30
```
--------------------------------
### Translate Words with l_word
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `l_word` filter translates a given word into a specified language using I18n custom plugin translations. It's useful for internationalizing text content within templates.
```liquid
{{ "today" | l_word: "es-ES" }}
{{ "tomorrow" | l_word: "ko" }}
{{ "yesterday" | l_word: "fr" }}
```
--------------------------------
### Append Random Hex String with append_random
Source: https://context7.com/usetrmnl/trmnl-liquid/llms.txt
The `append_random` filter appends a random 4-character hexadecimal string to a given value. It is commonly used for generating unique identifiers for elements like charts or divs.
```liquid
{% assign chart_id = "chart-" | append_random %}
{{ chart_id }}
...
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.