View Source SilverOrb.StringBuilder (SilverOrb v0.0.6)

Build strings with dynamic content.

defmodule MultiStepForm do
  use Orb
  use I32.String
  use SilverOrb.BumpAllocator
  use SilverOrb.StringBuilder

  global do
    @step 1
  end

  global :export_mutable do
    @step_count 4
  end

  defw(get_current_step(), I32, do: @step)

  defwp change_step(step: I32) do
    @step =
      if step < 1 do
        1
      else
        if(step > @step_count, do: @step_count, else: step)
      end
  end

  defw(next_step(), do: change_step(@step + 1))
  defw(previous_step(), do: change_step(@step - 1))
  defw(jump_to_step(step: I32), do: change_step(step))

  defw(to_string(), I32.String, do: to_html())

  defw to_html(), I32.String do
    build! do
      build_step(1)
      build_step(2)
      build_step(3)
      build_step(4)
      build_step(5)
    end
  end

  defwp build_step(step: I32), I32.String, current_step?: I32 do
    current_step? = step === Orb.Instruction.global_get(:i32, :step)

    build! do
      ~S[<div class="w-4 h-4 text-center ]

      if current_step? do
        ~S[bg-blue-600 text-white]
      else
        ~S[text-black]
      end

      ~S[">]
      append!(decimal_u32: step)
      ~S[</div>\n]
    end
  end
end

Summary

Functions

Import all WebAssembly functions from this module’s Orb definition.

Import a specific WebAssembly function from this module’s Orb definition.

Convert this module’s Orb definition to WebAssembly text (Wat) format.

Functions

Link to this function

append!(function, a, b, c)

View Source

Import all WebAssembly functions from this module’s Orb definition.

Import a specific WebAssembly function from this module’s Orb definition.

Convert this module’s Orb definition to WebAssembly text (Wat) format.