ElementorexCms.Dynamic (ElementorexCms v0.2.0)

Copy Markdown View Source

Dynamic content: props of a block bound to fields of a record.

A node keeps its bindings next to its props, one per bound prop:

%{
  "type" => "heading",
  "props" => %{"text" => "Product name"},
  "dynamic" => %{
    "text" => %{"field" => "name"}
  }
}

When the node is rendered with a current item (inside a Loop grid, or a document rendered with record=), the value of the field replaces the prop; without one the prop keeps its own value, which works as a placeholder. Bound values are cast and sanitized like any other value of the prop, so a field can never inject markup or unsafe URLs.

A binding

  • "field" - the key of a field of the source ("name", "category.name") or "@url" (the page of the item)
  • "from" - "item" (default: the current item) or "site" (a top-level value of the data the host passes, such as the site name)
  • "before" / "after" - text around the value ("$", " left")
  • "fallback" - used when the item has no value
  • "format" - "number" (thousands separators of the language, with "decimals"), "date", "datetime", "time", "excerpt" (plain text cut to "length" characters), "upper" or "lower"

Only fields declared by the source can be read: a binding to anything else renders nothing.

Display conditions

A node can also be shown only for the items that match a condition on one of their fields (its Advanced attribute "condition"): a Sold out badge for products without stock, a Featured ribbon…

"attrs" => %{"condition" => %{"field" => "stock", "op" => "lte", "value" => "0"}}

The operators are those of the filters of a loop (see ElementorexCms.Source.Query). Without a current item the node is shown; on the canvas, nodes whose condition fails are dimmed instead of removed.

Summary

Functions

Whether a prop can be bound to a field (lists cannot).

The binding of a prop of a node, or nil.

The text of a value for a binding: formatted per its "format" and the current locale, "" for no value.

The raw value of a binding for the current item of ctx: {:ok, value, field} or :none when there is nothing to read from.

The source whose fields the :field props of a node choose from: its own source prop (a loop) or, for a node in a filter form (a block with a :loop prop, or inside one), the source of the loop grid it filters.

A date, date-time or time in the format of the current locale (kind is :date, :datetime or :time). Times of a DateTime are shown in the configured :time_zone when there is a time zone database.

A number with the thousands and decimal separators of the current locale. decimals fixes the decimal places (rounding); nil keeps them.

Formats of a binding (see the module docs).

The source of the items a node is rendered with: the source of its nearest enclosing loop, or document_source (the source of a template for single records) when it is in no loop. nil when there is none.

Whether a node repeats its children for the items of a source (a loop block).

Keeps the valid bindings of a node: of props of block that can be bound (any well-formed key when the block is unknown), with valid settings.

A valid binding or nil. Takes a map (string or atom keys), a keyword list or just the field key.

A valid display condition or nil: "field" (like a binding's), "op" (an operator of ElementorexCms.Source.Query), "value" and optionally "from" => "site".

The settings of a binding (everything but the field): before, after, fallback, format, decimals and length, validated.

The props of node with its bindings applied for the current item of ctx (an ElementorexCms.Renderer.Context). Bindings without a value to read (no current item) keep the prop's own value.

Fields for the site values of data (from: "site" bindings).

A top-level value of data (atom or string key) that can be shown: text, numbers, booleans and dates. Anything else (structs, maps, lists) is not readable from documents.

Turns a raw value into the value of prop: numbers for number props, true or false for booleans, escaped text (paragraphs for rich text) and the formatted text with before, after and fallback for the rest. The result is cast again by the prop when the block renders.

Whether a node is shown for the current item of ctx: true without a condition or without an item to check it against.

Functions

bindable?(prop)

@spec bindable?(ElementorexCms.Block.Prop.t()) :: boolean()

Whether a prop can be bound to a field (lists cannot).

binding(arg1, prop)

@spec binding(map() | nil, atom() | String.t()) :: map() | nil

The binding of a prop of a node, or nil.

display(raw, field, binding)

@spec display(term(), ElementorexCms.Source.Field.t() | nil, map()) :: String.t()

The text of a value for a binding: formatted per its "format" and the current locale, "" for no value.

fetch(arg1, ctx)

@spec fetch(map(), map()) ::
  {:ok, term(), ElementorexCms.Source.Field.t() | nil} | :none

The raw value of a binding for the current item of ctx: {:ok, value, field} or :none when there is nothing to read from.

field_source(doc, node_id, sources, registry \\ nil)

The source whose fields the :field props of a node choose from: its own source prop (a loop) or, for a node in a filter form (a block with a :loop prop, or inside one), the source of the loop grid it filters.

format_date(value, kind)

@spec format_date(term(), :date | :datetime | :time) :: String.t()

A date, date-time or time in the format of the current locale (kind is :date, :datetime or :time). Times of a DateTime are shown in the configured :time_zone when there is a time zone database.

format_number(value, decimals \\ nil)

@spec format_number(term(), non_neg_integer() | nil) :: String.t()

A number with the thousands and decimal separators of the current locale. decimals fixes the decimal places (rounding); nil keeps them.

iex> ElementorexCms.Dynamic.format_number(1234567.891, 2)
"1,234,567.89"
iex> ElementorexCms.I18n.with_locale("es", fn -> ElementorexCms.Dynamic.format_number(19990, 0) end)
"19.990"

formats()

@spec formats() :: [String.t()]

Formats of a binding (see the module docs).

item_source(doc, node_id, sources, document_source, registry \\ nil)

The source of the items a node is rendered with: the source of its nearest enclosing loop, or document_source (the source of a template for single records) when it is in no loop. nil when there is none.

loop?(arg1, registry)

@spec loop?(map(), ElementorexCms.Registry.t() | nil) :: boolean()

Whether a node repeats its children for the items of a source (a loop block).

normalize(dynamic, block)

@spec normalize(term(), module() | nil) :: map()

Keeps the valid bindings of a node: of props of block that can be bound (any well-formed key when the block is unknown), with valid settings.

normalize_binding(field)

@spec normalize_binding(term()) :: map() | nil

A valid binding or nil. Takes a map (string or atom keys), a keyword list or just the field key.

iex> ElementorexCms.Dynamic.normalize_binding(field: "price", before: "$", format: "number")
%{"field" => "price", "before" => "$", "format" => "number"}

normalize_condition(condition)

@spec normalize_condition(term()) :: map() | nil

A valid display condition or nil: "field" (like a binding's), "op" (an operator of ElementorexCms.Source.Query), "value" and optionally "from" => "site".

normalize_settings(settings)

@spec normalize_settings(term()) :: map()

The settings of a binding (everything but the field): before, after, fallback, format, decimals and length, validated.

resolve(node, block, ctx)

@spec resolve(map(), module(), map()) :: map()

The props of node with its bindings applied for the current item of ctx (an ElementorexCms.Renderer.Context). Bindings without a value to read (no current item) keep the prop's own value.

site_fields(data)

@spec site_fields(term()) :: [ElementorexCms.Source.Field.t()]

Fields for the site values of data (from: "site" bindings).

site_value(data, key)

@spec site_value(term(), String.t()) :: {:ok, term()} | :error

A top-level value of data (atom or string key) that can be shown: text, numbers, booleans and dates. Anything else (structs, maps, lists) is not readable from documents.

to_prop(raw, field, binding, prop)

@spec to_prop(
  term(),
  ElementorexCms.Source.Field.t() | nil,
  map(),
  ElementorexCms.Block.Prop.t()
) ::
  term()

Turns a raw value into the value of prop: numbers for number props, true or false for booleans, escaped text (paragraphs for rich text) and the formatted text with before, after and fallback for the rest. The result is cast again by the prop when the block renders.

visible?(arg1, ctx)

@spec visible?(map(), map()) :: boolean()

Whether a node is shown for the current item of ctx: true without a condition or without an item to check it against.