Heya fellows!
I got two questions regarding rendering dynamic blocks for a CMS.
- Would you use the H sigil or another one?
- How can you dynamically create tags?
defmodule Cms.Blocks.TextBlock do
@moduledoc false
use CmsWeb, :live_component
alias Cms.Block
@type t() :: %__MODULE__{
id: String.t() | nil,
classes: list(String.t()),
tag: String.t(),
content: String.t(),
display_raw: boolean()
}
defstruct [:id, :classes, tag: "span", content: "", display_raw: false]
defimpl Block do
alias Cms.Blocks.TextBlock
def to_html(%TextBlock{} = text_block) do
assigns = Map.from_struct(text_block)
~H"""
<<%= @tag %>></<%= @tag %>>
"""
end
end
end
The error here is, that the H sigil cannot dynamically create the tag based on the given struct.
Would you pattern match on the tag and create the inner contents with a separate function?