elixirphoenix-frameworkliveview

Programmatically composing a ~H sigil in LiveView


how can I populate a part of or a whole ~H sigil with a variable? I’m trying to use it to insert some existing html table markup into a Liveview page. I tried the following methods, but they result in texts like “#{html_codes}” or “ … ” on the page instead of inserting a table on the Liveview page. If there is a better way to insert arbitrary html codes in a LiveView page, I’m all ears.

html_codes = """
  <table> .... </table>
"""

~H"""
   #{html_codes} or <%= html_codes %>
"""

Solution

  • Received the following tip from the Elixir Slack channel and it worked.

    ~H"""
    <% = Phoenix.HTML.raw(html_codes) %>
    """