ElementorexCms.Templates behaviour (ElementorexCms v0.2.0)

Copy Markdown View Source

Ready-made sections the editor can insert (Templates panel).

Add your own with a module that implements this behaviour and list it in the config (or pass templates={...} to the editor):

defmodule MyApp.EditorTemplates do
  @behaviour ElementorexCms.Templates
  import ElementorexCms.Builder

  @impl true
  def templates do
    [
      %{
        key: "promo",
        label: "Promotion",
        description: "Headline, countdown and button",
        icon: "megaphone",
        nodes: fn -> [block("heading", %{text: "Black Friday"}), block("countdown")] end
      }
    ]
  end
end

config :elementorex_cms, templates: [MyApp.EditorTemplates]

nodes is a function so every insertion gets fresh ids.

Dynamic templates

Templates can repeat the items of a source and bind blocks to their fields (ElementorexCms.Builder.loop/4 and dynamic:). Give them a :source key and the editor only offers them when that source is available:

%{
  key: "featured_products",
  label: "Featured products",
  source: "products",
  nodes: fn ->
    [loop("products", [block("card", %{}, dynamic: %{title: "name"})], %{limit: 3})]
  end
}

The editor also makes templates from the fields of every source on its own (for_sources/2): a grid and a list of its items and, in a template for the pages of a source, the page of one item.

Summary

Callbacks

Templates offered by the module.

Functions

Built-in templates followed by the configured ones.

The templates for an editor: all/1 without those of sources that are not available, plus for_sources/2.

The built-in templates.

Templates made from the fields of each source (see ElementorexCms.Source.role/2): a grid of cards and a list of its items, linked to their page when the source has one. With document_source (the source of a template for single items) also the page of one item, with a list of related items.

The template with key, or nil.

Types

template()

@type template() :: %{
  :key => String.t(),
  :label => String.t(),
  :nodes => (-> [map()]),
  optional(:description) => String.t(),
  optional(:icon) => String.t(),
  optional(:source) => String.t(),
  optional(:group) => atom()
}

Callbacks

templates()

@callback templates() :: [template()]

Templates offered by the module.

Functions

all(modules \\ nil)

@spec all([module()] | nil) :: [template()]

Built-in templates followed by the configured ones.

available(modules, sources, document_source \\ nil)

@spec available([module()] | nil, ElementorexCms.Sources.t() | nil, String.t() | nil) ::
  [template()]

The templates for an editor: all/1 without those of sources that are not available, plus for_sources/2.

builtin()

@spec builtin() :: [template()]

The built-in templates.

for_sources(sources, document_source \\ nil)

@spec for_sources(ElementorexCms.Sources.t() | nil, String.t() | nil) :: [template()]

Templates made from the fields of each source (see ElementorexCms.Source.role/2): a grid of cards and a list of its items, linked to their page when the source has one. With document_source (the source of a template for single items) also the page of one item, with a list of related items.

get(key, modules \\ nil)

@spec get(String.t(), [module()] | nil) :: template() | nil

The template with key, or nil.