### Defining and Using Custom Filters in Liquex Source: https://github.com/handlecommerce/liquex/blob/main/README.md Provides an example of creating a custom filter module (`CustomFilter`) that extends Liquex's filter capabilities. It shows how to import standard filters and define a new one. ```elixir defmodule CustomFilter do # Import all the standard liquid filters use Liquex.Filter def scream(value, _), do: String.upcase(value) <> "!" end context = Liquex.Context.new(%{}, filter_module: CustomFilter) {:ok, template_ast} = Liquex.parse("{{'Hello World' | scream}}") {result, _} = Liquex.render!(template_ast, context) result |> to_string() iex> "HELLO WORLD!" ``` -------------------------------- ### If/Elsif/Else Conditionals in Liquid Source: https://context7.com/handlecommerce/liquex/llms.txt Demonstrates the use of Liquid's if, elsif, and else tags for conditional rendering. Supports comparison and logical operators, as well as 'contains' checks. The example shows rendering based on product availability. ```elixir {:ok, ast} = Liquex.parse("{% if product.available %} In Stock {% elsif product.preorder %} Pre-order Available {% else %} Out of Stock {% endif %}") {result, _} = Liquex.render!(ast, %{ "product" => %{"available" => false, "preorder" => true} }) IO.puts(to_string(result)) ``` ```elixir {:ok, ast} = Liquex.parse("{% if user.age >= 18 %} Adult content allowed {% endif %}") ``` -------------------------------- ### Add Liquex Dependency to mix.exs Source: https://github.com/handlecommerce/liquex/blob/main/README.md Add the liquex package to your project's dependencies in mix.exs to install it. ```elixir def deps do [ {:liquex, "~> 0.13.0"} ] end ``` -------------------------------- ### Configure Local File System for Render Tags Source: https://context7.com/handlecommerce/liquex/llms.txt Initialize a LocalFileSystem to enable the {% render %} tag for partial template inclusion. ```elixir # Create a local file system pointing to templates directory file_system = Liquex.LocalFileSystem.new("/path/to/templates") # Custom pattern (default is "_%s.liquid") file_system = Liquex.LocalFileSystem.new("/path/to/templates", "%s.html") # Create context with file system context = Liquex.Context.new( %{"product" => %{"title" => "Shirt", "price" => 25}}, file_system: file_system ) # Main template using render tag # Assumes /path/to/templates/_product_card.liquid exists {:ok, ast} = Liquex.parse("""
{% render 'product_card' with product %}
""") {result, _} = Liquex.render!(ast, context) ``` -------------------------------- ### Basic Template Parsing and Rendering in Elixir Source: https://github.com/handlecommerce/liquex/blob/main/README.md Demonstrates the fundamental usage of Liquex for parsing a Liquid template string and rendering it with a given context. ```elixir iex> {:ok, template_ast} = Liquex.parse("Hello {{ name }}!") iex> {content, _context} = Liquex.render!(template_ast, %{"name" => "World"}) iex> content |> to_string() "Hello World!" ``` -------------------------------- ### Initializing Liquex Simple Cache Source: https://github.com/handlecommerce/liquex/blob/main/README.md Demonstrates how to initialize the built-in ETS-based cache for the render tag. Caching is disabled by default. ```elixir :ok = Liquex.Cache.SimpleCache.init() context = Context.new(%{...}, cache: Liquex.Cache.SimpleCache) ``` -------------------------------- ### Create Rendering Context Source: https://context7.com/handlecommerce/liquex/llms.txt Use Liquex.Context.new/2 to create a rendering context. Options include custom filters, file systems for partials, static environments, and caching. ```elixir context = Liquex.Context.new(%{"title" => "My Page", "items" => [1, 2, 3]}) ``` ```elixir defmodule MyFilters do use Liquex.Filter def scream(value, _), do: String.upcase(value) <> "!" end context = Liquex.Context.new( %{"greeting" => "hello"}, filter_module: MyFilters ) {:ok, ast} = Liquex.parse("{{ greeting | scream }}") {result, _context} = Liquex.render!(ast, context) IO.puts(to_string(result)) ``` ```elixir context = Liquex.Context.new( %{"page_title" => "Home"}, static_environment: %{"site_name" => "My Site"} ) ``` ```elixir file_system = Liquex.LocalFileSystem.new("/templates") context = Liquex.Context.new(%{}, file_system: file_system) ``` ```elixir :ok = Liquex.Cache.SimpleCache.init() context = Liquex.Context.new(%{}, cache: Liquex.Cache.SimpleCache) ``` -------------------------------- ### Liquex.Context.new/2 - Create a Rendering Context Source: https://context7.com/handlecommerce/liquex/llms.txt Creates a new context with variables and configuration options for rendering. Supports custom filters, file systems for partials, static environments, and caching. ```APIDOC ## Liquex.Context.new/2 - Create a Rendering Context ### Description Creates a new context with variables and configuration options for rendering. Supports custom filters, file systems for partials, static environments, and caching. ### Method `Liquex.Context.new/2` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```elixir # Basic context with variables context = Liquex.Context.new(%{"title" => "My Page", "items" => [1, 2, 3]}) # Context with custom filter module defmodule MyFilters do use Liquex.Filter def scream(value, _), do: String.upcase(value) <> "!" end context = Liquex.Context.new( %{"greeting" => "hello"}, filter_module: MyFilters ) {:ok, ast} = Liquex.parse("{{ greeting | scream }}") {result, _context} = Liquex.render!(ast, context) IO.puts(to_string(result)) # Output: HELLO! # Context with static environment (accessible in nested render tags) context = Liquex.Context.new( %{"page_title" => "Home"}, static_environment: %{"site_name" => "My Site"} ) # Context with local file system for render tag file_system = Liquex.LocalFileSystem.new("/templates") context = Liquex.Context.new(%{}, file_system: file_system) # Context with caching enabled :ok = Liquex.Cache.SimpleCache.init() context = Liquex.Context.new(%{}, cache: Liquex.Cache.SimpleCache) ``` ### Response #### Success Response (200) - **context** (Context) - A new rendering context. #### Response Example ```elixir context ``` ``` -------------------------------- ### Implementing and Using Custom Tags in Liquex Source: https://github.com/handlecommerce/liquex/blob/main/README.md Details how to create a custom tag (`CustomTag`) by implementing the `Liquex.Tag` behaviour, including parsing logic and rendering. It also shows how to integrate this custom tag into a `Liquex.Parser`. ```elixir defmodule CustomTag do @moduledoc false @behaviour Liquex.Tag import NimbleParsec @impl true # Parse <> def parse() do text = lookahead_not(string(">>")) |> utf8_char([]) |> times(min: 1) |> reduce({Kernel, :to_string, []}) |> tag(:text) ignore(string("<<")) |> optional(text) |> ignore(string(">>")) end @impl true def render(contents, context) do {result, context} = Liquex.render!(contents, context) {["Custom Tag: ", result], context} end end defmodule CustomParser do use Liquex.Parser, tags: [CustomTag] end iex> document = Liquex.parse!("<>", CustomParser) iex> {result, _} = Liquex.render!(document, context) iex> result |> to_string() "Custom Tag: Hello World!" ``` -------------------------------- ### Enable Template Caching Source: https://context7.com/handlecommerce/liquex/llms.txt Use the built-in SimpleCache or implement the Liquex.Cache behaviour for custom caching strategies. ```elixir # Initialize the simple ETS-based cache :ok = Liquex.Cache.SimpleCache.init() # Create context with caching enabled context = Liquex.Context.new( %{"products" => products}, cache: Liquex.Cache.SimpleCache, cache_prefix: "store_1" # Optional: for multi-tenant caching ) # Implement custom cache using Cachex defmodule MyCache do @behaviour Liquex.Cache @impl true def fetch(key, func) do case Cachex.get(:my_cache, key) do {:ok, nil} -> value = func.() Cachex.put(:my_cache, key, value) value {:ok, value} -> value end end end context = Liquex.Context.new(%{}, cache: MyCache) ``` -------------------------------- ### Apply String Filters Source: https://context7.com/handlecommerce/liquex/llms.txt Demonstrates common string manipulation filters like upcase, truncate, and replace. ```elixir {:ok, ast} = Liquex.parse(""" {{ "hello world" | upcase }} {{ "HELLO WORLD" | downcase }} {{ "hello world" | capitalize }} {{ " hello " | strip }} {{ "Hello World" | truncate: 8 }} {{ "Hello World" | truncatewords: 1 }} {{ "hello" | append: " world" }} {{ "world" | prepend: "hello " }} {{ "hello world" | replace: "world", "universe" }} {{ "hello world world" | replace_first: "world", "universe" }} {{ "hello world" | remove: "world" }} {{ "hello world" | split: " " | first }} {{ "hello\nworld" | newline_to_br }} {{ "hello
world" | strip_html }} {{ "/my path/" | url_encode }} {{ "%2Fmy+path%2F" | url_decode }} """) {result, _} = Liquex.render!(ast) ``` -------------------------------- ### Liquex.render!/2 - Render a Parsed Template Source: https://context7.com/handlecommerce/liquex/llms.txt Renders a parsed Liquid AST document with the provided context (variables). Returns a tuple of IO data and the updated context. ```APIDOC ## Liquex.render!/2 - Render a Parsed Template ### Description Renders a parsed Liquid AST document with the provided context (variables). Returns a tuple of IO data and the updated context. ### Method `Liquex.render!/2` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```elixir # Basic rendering with a map {:ok, ast} = Liquex.parse("Hello {{ name }}!") {result, _context} = Liquex.render!(ast, %{"name" => "World"}) IO.puts(to_string(result)) # Output: Hello World! # Rendering with atom keys (indifferent access) {:ok, ast} = Liquex.parse("Price: {{ product.price }}") {result, _context} = Liquex.render!(ast, %{product: %{price: 19.99}}) IO.puts(to_string(result)) # Output: Price: 19.99 # Rendering with nested variables {:ok, ast} = Liquex.parse("{{ user.address.city }}") context = %{ "user" => %{ "address" => %{ "city" => "San Francisco" } } } {result, _context} = Liquex.render!(ast, context) IO.puts(to_string(result)) # Output: San Francisco ``` ### Response #### Success Response (200) - **result** (IO Data) - The rendered output. - **context** (Context) - The updated rendering context. #### Response Example ```elixir {result, _context} ``` ``` -------------------------------- ### Render Parsed Liquid Template Source: https://context7.com/handlecommerce/liquex/llms.txt Use Liquex.render!/2 to render a Liquid AST with a given context. It returns the rendered output and the updated context. Supports map and atom-based keys for context variables. ```elixir {:ok, ast} = Liquex.parse("Hello {{ name }}!") {result, _context} = Liquex.render!(ast, %{"name" => "World"}) IO.puts(to_string(result)) ``` ```elixir {:ok, ast} = Liquex.parse("Price: {{ product.price }}") {result, _context} = Liquex.render!(ast, %{product: %{price: 19.99}}) IO.puts(to_string(result)) ``` ```elixir {:ok, ast} = Liquex.parse("{{ user.address.city }}") context = %{ "user" => %{ "address" => %{ "city" => "San Francisco" } } } {result, _context} = Liquex.render!(ast, context) IO.puts(to_string(result)) ``` -------------------------------- ### Use Logical Operators in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Demonstrates using 'and' within an if statement. ```elixir {:ok, ast} = Liquex.parse(""" {% if user.admin and user.active %} Admin Panel Access {% endif %} """) ``` -------------------------------- ### Use Case/When Tag in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Matches a variable against multiple values using a switch-like structure. ```elixir {:ok, ast} = Liquex.parse(""" {% case shipping_method %} {% when "standard" %} Arrives in 5-7 business days {% when "express" %} Arrives in 2-3 business days {% when "overnight" %} Arrives tomorrow {% else %} Contact us for shipping info {% endcase %} """) {result, _} = Liquex.render!(ast, %{"shipping_method" => "express"}) IO.puts(to_string(result)) # Output: Arrives in 2-3 business days ``` -------------------------------- ### Create Custom Filter Modules Source: https://context7.com/handlecommerce/liquex/llms.txt Defines custom filter logic by implementing the Liquex.Filter behaviour. ```elixir defmodule MyCustomFilters do use Liquex.Filter # Simple filter: value is first argument, context is last def scream(value, _context), do: String.upcase(value) <> "!" # Filter with additional arguments def repeat(value, times, _context) do String.duplicate(to_string(value), times) end # Filter with keyword arguments def img_url(path, size, [{"crop", direction}, {"filter", filter}], _context) do "https://cdn.example.com/#{path}?size=#{size}&crop=#{direction}&filter=#{filter}" end # Filter with optional keyword argument def money(amount, [{"currency", currency}], _context) do "#{currency}#{:erlang.float_to_binary(amount / 1, decimals: 2)}" end end # Using custom filters context = Liquex.Context.new( %{"greeting" => "hello", "product_image" => "shirt.jpg"}, filter_module: MyCustomFilters ) {:ok, ast} = Liquex.parse(""" {{ greeting | scream }} {{ "ha" | repeat: 3 }} {{ product_image | img_url: "400x400", crop: "center", filter: "sharpen" }} {{ 29.99 | money: currency: "$" }} """) {result, _} = Liquex.render!(ast, context) IO.puts(to_string(result)) # Output: # HELLO! # hahaha # https://cdn.example.com/shirt.jpg?size=400x400&crop=center&filter=sharpen # $29.99 ``` -------------------------------- ### Parse Liquid Template to AST Source: https://context7.com/handlecommerce/liquex/llms.txt Use Liquex.parse/2 to convert a Liquid template string into an Abstract Syntax Tree (AST). An optional custom parser module can be provided for extended tag syntax. The parse! variant raises an error on failure. ```elixir {:ok, ast} = Liquex.parse("Hello {{ name }}!") ``` ```elixir {:ok, ast} = Liquex.parse("{{ product.title | upcase }}") ``` ```elixir {:ok, ast} = Liquex.parse("{% if user.logged_in %} Welcome back, {{ user.name }}! {% else %} Please log in. {% endif %}") ``` ```elixir {:ok, ast} = Liquex.parse("<>", MyCustomParser) ``` ```elixir ast = Liquex.parse!("Hello {{ name }}!") ``` -------------------------------- ### Apply Default Filter Source: https://context7.com/handlecommerce/liquex/llms.txt Provides fallback values for nil, false, or empty inputs. ```elixir {:ok, ast} = Liquex.parse(""" {{ product.title | default: "Untitled" }} {{ missing_var | default: "Not found" }} {{ "" | default: "Empty string replaced" }} """) {result, _} = Liquex.render!(ast, %{"product" => %{}}) IO.puts(to_string(result)) # Output: Untitled, Not found, Empty string replaced ``` -------------------------------- ### Use For Loop in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Iterates over arrays and ranges, providing access to the forloop object. ```elixir {:ok, ast} = Liquex.parse("""
    {% for product in products %}
  • {{ forloop.index }}. {{ product.title }} - ${{ product.price }}
  • {% endfor %}
""") products = [ %{"title" => "Shirt", "price" => 25}, %{"title" => "Pants", "price" => 45}, %{"title" => "Hat", "price" => 15} ] {result, _} = Liquex.render!(ast, %{"products" => products}) IO.puts(to_string(result)) # Output: #
    #
  • 1. Shirt - $25
  • #
  • 2. Pants - $45
  • #
  • 3. Hat - $15
  • #
``` ```elixir {:ok, ast} = Liquex.parse(""" {% for item in array limit:2 offset:1 %}{{ item }} {% endfor %} """) {result, _} = Liquex.render!(ast, %{"array" => [1, 2, 3, 4, 5]}) IO.puts(to_string(result)) # Output: 2 3 ``` ```elixir {:ok, ast} = Liquex.parse(""" {% for i in (1..5) %}{{ i }} {% endfor %} """) {result, _} = Liquex.render!(ast) IO.puts(to_string(result)) # Output: 1 2 3 4 5 ``` ```elixir {:ok, ast} = Liquex.parse(""" {% for item in array reversed %}{{ item }} {% endfor %} """) {result, _} = Liquex.render!(ast, %{"array" => [1, 2, 3]}) IO.puts(to_string(result)) # Output: 3 2 1 ``` ```elixir {:ok, ast} = Liquex.parse(""" {% for item in products %} {{ item.title }} {% else %} No products found. {% endfor %} """) {result, _} = Liquex.render!(ast, %{"products" => []}) IO.puts(to_string(result)) # Output: No products found. ``` -------------------------------- ### Create Custom Tag Modules in Elixir Source: https://context7.com/handlecommerce/liquex/llms.txt Implement the Liquex.Tag behaviour to define custom Liquid tags. Use a custom parser module to register the tag for use in templates. ```elixir defmodule HighlightTag do @moduledoc "Custom tag that wraps content in a highlight span" @behaviour Liquex.Tag import NimbleParsec @impl true def parse do text = lookahead_not(string("%}")) |> utf8_char([]) |> times(min: 1) |> reduce({Kernel, :to_string, []}) |> tag(:text) ignore(string("{%")) |> ignore(string(" highlight ")) |> concat(text) |> ignore(string(" %}")) end @impl true def render([text: [content]], context) do {["", String.trim(content), ""], context} end end defmodule CustomParser do use Liquex.Parser, tags: [HighlightTag] end # Using the custom tag {:ok, ast} = Liquex.parse("{% highlight Important! %}", CustomParser) {result, _} = Liquex.render!(ast) IO.puts(to_string(result)) # Output: Important! ``` -------------------------------- ### Define Lazy Variables with Resolver Functions Source: https://context7.com/handlecommerce/liquex/llms.txt Pass functions in the context map to defer data loading until the variable is accessed during rendering. ```elixir defmodule ProductLoader do def load_products(_parent) do # Simulate database query - only runs when accessed [ %{"id" => 1, "title" => "Shirt", "price" => 25.00}, %{"id" => 2, "title" => "Pants", "price" => 45.00} ] end def load_user(_parent) do %{"name" => "Alice", "email" => "alice@example.com"} end end # Variables are functions that will be called lazily context = %{ "products" => &ProductLoader.load_products/1, "current_user" => &ProductLoader.load_user/1, "site_name" => "My Store" # Regular value, not lazy } {:ok, ast} = Liquex.parse(""" Welcome to {{ site_name }}! {% for product in products %} - {{ product.title }}: ${{ product.price }} {% endfor %} Logged in as: {{ current_user.name }} """) {result, _} = Liquex.render!(ast, context) IO.puts(to_string(result)) ``` -------------------------------- ### Apply Array Filters Source: https://context7.com/handlecommerce/liquex/llms.txt Covers array operations including mapping, filtering, sorting, and joining. ```elixir {:ok, ast} = Liquex.parse(""" {% assign fruits = "apple,banana,cherry" | split: "," %} First: {{ fruits | first }} Last: {{ fruits | last }} Joined: {{ fruits | join: " - " }} Size: {{ fruits | size }} Reversed: {{ fruits | reverse | join: ", " }} """) {result, _} = Liquex.render!(ast) IO.puts(to_string(result)) # Output: # First: apple # Last: cherry # Joined: apple - banana - cherry # Size: 3 # Reversed: cherry, banana, apple # Map and where filters {:ok, ast} = Liquex.parse(""" {% assign titles = products | map: "title" %} {{ titles | join: ", " }} {% assign available = products | where: "available", true %} Available count: {{ available | size }} """) products = [ %{"title" => "Shirt", "available" => true}, %{"title" => "Pants", "available" => false}, %{"title" => "Hat", "available" => true} ] {result, _} = Liquex.render!(ast, %{"products" => products}) IO.puts(to_string(result)) # Output: # Shirt, Pants, Hat # Available count: 2 # Sort filters {:ok, ast} = Liquex.parse(""" {{ names | sort | join: ", " }} {{ names | sort_natural | join: ", " }} """) {result, _} = Liquex.render!(ast, %{"names" => ["Bob", "alice", "Charlie"]}) ``` -------------------------------- ### Liquex.parse/2 - Parse a Liquid Template Source: https://context7.com/handlecommerce/liquex/llms.txt Parses a Liquid template string into an AST (Abstract Syntax Tree) that can be rendered. Accepts an optional custom parser module for extending the tag syntax. ```APIDOC ## Liquex.parse/2 - Parse a Liquid Template ### Description Parses a Liquid template string into an AST (Abstract Syntax Tree) that can be rendered. Accepts an optional custom parser module for extending the tag syntax. ### Method `Liquex.parse/2` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```elixir # Basic template parsing {:ok, ast} = Liquex.parse("Hello {{ name }}!") # Parse with variables and filters {:ok, ast} = Liquex.parse("{{ product.title | upcase }}") # Parse control flow templates {:ok, ast} = Liquex.parse(""" {% if user.logged_in %} Welcome back, {{ user.name }}! {% else %} Please log in. {% endif %} """) # Parse with a custom parser {:ok, ast} = Liquex.parse("<>", MyCustomParser) # Using parse! which raises on error ast = Liquex.parse!("Hello {{ name }}!") ``` ### Response #### Success Response (200) - **ast** (AST) - The Abstract Syntax Tree representation of the parsed template. #### Response Example ```elixir {:ok, ast} ``` ``` -------------------------------- ### Use Capture Tag in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Captures the rendered output of a block into a variable. ```elixir {:ok, ast} = Liquex.parse(""" {% capture email_body %} Dear {{ customer.name }}, Thank you for your order #{{ order.number }}. Your items will ship within 2-3 business days. Best regards, The Team {% endcapture %} {{ email_body }} """) {result, _} = Liquex.render!(ast, %{ "customer" => %{"name" => "Alice"}, "order" => %{"number" => "12345"} }) IO.puts(to_string(result)) ``` -------------------------------- ### Apply Date Filter Source: https://context7.com/handlecommerce/liquex/llms.txt Formats date and time values using strftime strings. ```elixir {:ok, ast} = Liquex.parse(""" {{ order_date | date: "%B %d, %Y" }} {{ "2024-01-15" | date: "%m/%d/%Y" }} {{ "now" | date: "%Y-%m-%d %H:%M" }} {{ "today" | date: "%A, %B %d" }} """) {result, _} = Liquex.render!(ast, %{"order_date" => ~D[2024-03-15]}) IO.puts(to_string(result)) # Output: March 15, 2024 ``` -------------------------------- ### Apply Math Filters Source: https://context7.com/handlecommerce/liquex/llms.txt Performs numeric calculations and rounding operations. ```elixir {:ok, ast} = Liquex.parse(""" {{ 4 | plus: 2 }} {{ 16 | minus: 4 }} {{ 3 | times: 4 }} {{ 16 | divided_by: 4 }} {{ 12 | modulo: 5 }} {{ 1.5 | round }} {{ 1.2 | ceil }} {{ 2.9 | floor }} {{ -5 | abs }} {{ 4 | at_least: 5 }} {{ 10 | at_most: 5 }} """) {result, _} = Liquex.render!(ast) # Output: 6, 12, 12, 4, 2, 2, 2, 2, 5, 5, 5 ``` -------------------------------- ### Lazy Variable Resolution in Liquex Source: https://github.com/handlecommerce/liquex/blob/main/README.md Shows how to use resolver functions for variables that require computation, preventing expensive operations on every render. The function is executed only when the variable is accessed. ```elixir products_resolver = fn _parent -> Product.all() end with {:ok, document} <- Liquex.parse("There are {{ products.size }} products"), {result, _} <- Liquex.render!(document, %{products: products_resolver}) do result end "There are 5 products" ``` -------------------------------- ### Indifferent Access for Map and Struct Keys Source: https://github.com/handlecommerce/liquex/blob/main/README.md Illustrates Liquex's ability to access map and struct keys using either strings or atoms, prioritizing string keys. This simplifies integration with Elixir structs. ```elixir iex> {:ok, template_ast} = Liquex.parse("Hello {{ name }}!") iex> {content, _context} = Liquex.render!(template_ast, %{name: "World"}) iex> content |> to_string() "Hello World!" ``` -------------------------------- ### If/Elsif/Else Conditionals Source: https://context7.com/handlecommerce/liquex/llms.txt Executes blocks of code based on conditional expressions. Supports comparison operators (==, !=, <, >, <=, >=), logical operators (and, or), and contains checks. ```APIDOC ## If/Elsif/Else Conditionals ### Description Executes blocks of code based on conditional expressions. Supports comparison operators (==, !=, <, >, <=, >=), logical operators (and, or), and contains checks. ### Method Liquid conditional tags ### Endpoint N/A (This is a control flow tag within templates) ### Parameters None (Parameters are defined within the Liquid template context) ### Request Example ```elixir {:ok, ast} = Liquex.parse(""" {% if product.available %} In Stock {% elsif product.preorder %} Pre-order Available {% else %} Out of Stock {% endif %} """) {result, _} = Liquex.render!(ast, %{ "product" => %{"available" => false, "preorder" => true} }) IO.puts(to_string(result)) # Output: Pre-order Available # Using comparison operators {:ok, ast} = Liquex.parse(""" {% if user.age >= 18 %} Adult content allowed {% endif %} """) ``` ### Response #### Success Response (200) - **result** (IO Data) - The rendered output based on the conditional logic. #### Response Example ```elixir result ``` ``` -------------------------------- ### Use Increment/Decrement Tags in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Creates and modifies counter variables. ```elixir {:ok, ast} = Liquex.parse(""" {% increment counter %} {% increment counter %} {% increment counter %} Current: {{ counter }} """) {result, _} = Liquex.render!(ast) IO.puts(to_string(result)) # Output: 0 1 2 Current: 3 ``` -------------------------------- ### Use Tablerow Tag in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Generates HTML table rows from a collection. ```elixir {:ok, ast} = Liquex.parse(""" {% tablerow product in products cols:2 %} {{ product.title }} {% endtablerow %}
""") products = [ %{"title" => "Shirt"}, %{"title" => "Pants"}, %{"title" => "Hat"}, %{"title" => "Socks"} ] {result, _} = Liquex.render!(ast, %{"products" => products}) ``` -------------------------------- ### Use Assign Tag in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Creates new variables in the current scope, optionally using filters. ```elixir {:ok, ast} = Liquex.parse(""" {% assign greeting = "Hello" %} {% assign full_greeting = greeting | append: ", World!" %} {{ full_greeting }} """) {result, _} = Liquex.render!(ast) IO.puts(to_string(result)) # Output: Hello, World! ``` ```elixir {:ok, ast} = Liquex.parse(""" {% assign handle = product.title | downcase | replace: " ", "-" %} {{ product.title }} """) {result, _} = Liquex.render!(ast, %{"product" => %{"title" => "Awesome Shoes"}}) IO.puts(to_string(result)) # Output: Awesome Shoes ``` -------------------------------- ### Assign Variables to Context Source: https://context7.com/handlecommerce/liquex/llms.txt Use Liquex.Context.assign/3 to add or update variables within a rendering context. This is useful for modifying context programmatically during rendering. Variables can be accessed using the Access behaviour. ```elixir context = Liquex.Context.new(%{"count" => 0}) context = Liquex.Context.assign(context, "name", "Alice") name = Access.get(context, "name") # Returns: "Alice" context = Liquex.Context.assign(context, "products", [ %{"title" => "Shirt", "price" => 25}, %{"title" => "Pants", "price" => 45} ]) ``` -------------------------------- ### Use Unless Tag in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Executes a block only if the condition is false. ```elixir {:ok, ast} = Liquex.parse(""" {% unless product.sold_out %} {% endunless %} """) {result, _} = Liquex.render!(ast, %{"product" => %{"sold_out" => false}}) IO.puts(to_string(result)) # Output: ``` -------------------------------- ### Liquex.Context.assign/3 - Assign Variables to Context Source: https://context7.com/handlecommerce/liquex/llms.txt Assigns a variable to the current scope in the context. Useful for programmatically modifying context during rendering. ```APIDOC ## Liquex.Context.assign/3 - Assign Variables to Context ### Description Assigns a variable to the current scope in the context. Useful for programmatically modifying context during rendering. ### Method `Liquex.Context.assign/3` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```elixir context = Liquex.Context.new(%{"count" => 0}) # Assign a new variable context = Liquex.Context.assign(context, "name", "Alice") # Retrieve with Access behaviour name = Access.get(context, "name") # Returns: "Alice" # Assign complex data context = Liquex.Context.assign(context, "products", [ %{"title" => "Shirt", "price" => 25}, %{"title" => "Pants", "price" => 45} ]) ``` ### Response #### Success Response (200) - **context** (Context) - The context with the assigned variable. #### Response Example ```elixir context ``` ``` -------------------------------- ### Use Contains Operator in Liquex Source: https://context7.com/handlecommerce/liquex/llms.txt Checks for the existence of a value within a collection. ```elixir {:ok, ast} = Liquex.parse(""" {% if product.tags contains "sale" %} On Sale! {% endif %} """) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.