View Source SilverOrb.StringBuilder (SilverOrb v0.0.10)

Build strings with dynamic content.

Here’s an example with components rendering dynamic HTML:

defmodule HelloWorldComponent do
  use Orb
  use SilverOrb.StringBuilder

  defwp daytime?(hour: I32), I32 do
    hour >= 6 &&& hour <= 19
  end

  defw render(hour: I32), StringBuilder do
    StringBuilder.build! do
      "<h1>"

      if daytime?(hour) do
        "Hello 🌞 sunny world"
      else
        "Hello 🌛 moonlit world"
      end

      "</h1>\n"
    end
  end
end

defmodule DynamicHTMLPage do
  use Orb
  use SilverOrb.StringBuilder

  Orb.include(HelloWorldComponent)

  global do
    @hour_of_day 8
  end

  defw set_hour_of_day(hour: I32) do
    @hour_of_day = hour
  end

  defw text_html(), StringBuilder do
    StringBuilder.build! do
      """
      <!doctype html>
      <meta charset="utf-8">
      """

      HelloWorldComponent.render(@hour_of_day)
    end
  end
end

Summary

Functions