|
| 1 | +# Strings |
| 2 | + |
| 3 | +A feature central to C is string constants: you type `"abc"` and you can pass it around, read the characters in it, and even do operations on it. Underneath it's really a pointer to a sequence of bytes initialized in memory for you, yet C makes it feel natural. |
| 4 | + |
| 5 | +WebAssembly also gives you the ability to initialize a region of its memory with a string. But it doesn’t let you use the strings directly like C. Instead you must manually allocate memory addresses for each string constant, and then in functions hard-code the addresses, remembering what at what memory offset each string had. |
| 6 | + |
| 7 | +Orb adds a simple string constant system like C’s by letting you use strings directly. At compile-time these strings are substituted by a tuple of their `i32` memory address and `i32` byte length, represented as the custom type `Orb.Str`. |
| 8 | + |
| 9 | +```elixir |
| 10 | +defmodule StaticHTMLPage do |
| 11 | + use Orb |
| 12 | + |
| 13 | + defw get_mime_type, Str do |
| 14 | + "text/html" |
| 15 | + end |
| 16 | + |
| 17 | + defw get_body, Str do |
| 18 | + """ |
| 19 | + <!doctype html> |
| 20 | + <meta charset="utf-8"> |
| 21 | + <h1>Hello world</h1> |
| 22 | + """ |
| 23 | + end |
| 24 | +end |
| 25 | +``` |
| 26 | + |
| 27 | +## Building dynamic strings |
| 28 | + |
| 29 | +Dynamic string concatentation is not supported in vanilla Orb as that requires a memory allocator which core WebAssembly doesn’t have. This is one of the batteries that comes with Orb‘s sister standard library **SilverOrb**. |
| 30 | + |
| 31 | +Here’s an example rendering a dynamic HTML page: |
| 32 | + |
| 33 | +```elixir |
| 34 | +defmodule DynamicHTMLPage do |
| 35 | + use Orb |
| 36 | + use SilverOrb.StringBuilder |
| 37 | + |
| 38 | + global do |
| 39 | + @hour_of_day 8 |
| 40 | + end |
| 41 | + |
| 42 | + defw set_hour_of_day(hour) do |
| 43 | + @hour_of_day = hour |
| 44 | + end |
| 45 | + |
| 46 | + defwp daytime?() do |
| 47 | + @hour_of_day >= 6 and @hour_of_day <= 19 |
| 48 | + end |
| 49 | + |
| 50 | + defw text_html, StringBuilder do |
| 51 | + StringBuilder.build! do |
| 52 | + """ |
| 53 | + <!doctype html> |
| 54 | + <meta charset="utf-8"> |
| 55 | + """ |
| 56 | + |
| 57 | + if daytime?() do |
| 58 | + "<h1>Hello 🌞 sunny world</h1>" |
| 59 | + else |
| 60 | + "<h1>Hello 🌛 moonlit world</h1>" |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | +``` |
| 66 | + |
| 67 | +In modern web development we have components, and we can create simple components with `SilverOrb.StringBuilder` too. Let’s extract the `<h1>` rendering into its own component module: |
| 68 | + |
| 69 | +```elixir |
| 70 | +defmodule HelloWorldComponent do |
| 71 | + use Orb |
| 72 | + use SilverOrb.StringBuilder |
| 73 | + |
| 74 | + defwp daytime?(hour_of_day: I32), I32 do |
| 75 | + hour_of_day >= 6 &&& hour_of_day <= 19 |
| 76 | + end |
| 77 | + |
| 78 | + defw render(hour_of_day: I32), StringBuilder do |
| 79 | + StringBuilder.build! do |
| 80 | + "<h1>" |
| 81 | + |
| 82 | + if daytime?(hour_of_day) do |
| 83 | + "Hello 🌞 sunny world" |
| 84 | + else |
| 85 | + "Hello 🌛 moonlit world" |
| 86 | + end |
| 87 | + |
| 88 | + "</h1>\n" |
| 89 | + end |
| 90 | + end |
| 91 | +end |
| 92 | + |
| 93 | +defmodule DynamicHTMLPage do |
| 94 | + use Orb |
| 95 | + use SilverOrb.StringBuilder |
| 96 | + |
| 97 | + Orb.include(HelloWorldComponent) |
| 98 | + |
| 99 | + global do |
| 100 | + @hour_of_day 8 |
| 101 | + end |
| 102 | + |
| 103 | + defw set_hour_of_day(hour: I32) do |
| 104 | + @hour_of_day = hour |
| 105 | + end |
| 106 | + |
| 107 | + defw text_html(), StringBuilder do |
| 108 | + StringBuilder.build! do |
| 109 | + """ |
| 110 | + <!doctype html> |
| 111 | + <meta charset="utf-8"> |
| 112 | + """ |
| 113 | + |
| 114 | + HelloWorldComponent.render(@hour_of_day) |
| 115 | + end |
| 116 | + end |
| 117 | +end |
| 118 | +``` |
0 commit comments