Skip to content

Support sigils for inline heex with liveview #169

Description

@tomfarm

After #166 has been merged, maybe the next step to support phoenix heex templates could be to
introduce a sigil for slime-heex.

Currently there are only ~L and ~l (as far as I know) and both render as plain text when used inside the liveview render(assigns) function, but it may be that I'm missing something there.

defmodule InlineSlimeLive do
  use TestWeb, :live_view

  def mount(_params, _session, socket) do
    {:ok, socket}
  end

 def render(assigns) do
  ~M"""
  h1 hello
  """
end

It would be nice though if Elixir supported sigils with multiple characters, ~S is already in use and we are running out of characters 😄 And getting syntax highlighting to work would be nice too … 🤔

At them moment, I'm using the following code. It works, but I not sure if this supports liveview 100% doing it's diffing magic

defmodule SlimeSigils do
  defmacro sigil_M({:<<>>, meta, [expr]}, []) do
    options = [
      engine: Phoenix.LiveView.HTMLEngine,
      file: __CALLER__.file,
      line: __CALLER__.line + 1,
      module: __CALLER__.module,
      indentation: meta[:indentation] || 0
    ]

    Slime.Renderer.precompile_heex(expr)
    |> EEx.compile_string(options)
  end
end

Importing it in the matching functions/macros in the TestWeb module

  def live_view do
    quote do
      use Phoenix.LiveView,
        layout: {BunWeb.LayoutView, "live.html"}

      import SlimeSigils

      unquote(view_helpers())
    end
  end
  def live_component do
    quote do
      use Phoenix.LiveComponent
      import SlimeSigils

      unquote(view_helpers())
    end
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions