View Source Orbit.Gemtext (Orbit v0.3.1)
Render Gemtext content.
PrEEx.Engine is used to render Gemtext templates, instead of the default
EEx.SmartEngine, which provides a more developer-friendly approach to writing preformatted, plain-text templates.
Concepts
A template is any 1-arity function that accepts an assigns map and returns a string of rendered Gemtext.
A view is a module containing template functions.
Templates can be defined directly as functions, or embedded from .gmi.eex files into view module via embed_templates/1.
Templates can be precompiled with sigil_G/2, as long as the assigns variable is in-scope.
Usage
Add the imports to the view module:
import Orbit.GemtextExamples
Importing templates from a directory
defmodule MyAppCapsule.PostGmi do
import Orbit.Gemtext
embed_templates "post_gmi/*"
endWriting templates directly
defmodule MyAppCapsule.PostGmi do
import Orbit.Gemtext
def list(assigns) do
~G"""
<%= for item <- @items do %>
* <%= item %>
<% end %>
"""t
end
end
Summary
Functions
Precompile template functions from external files.
Renders a template.
Sigil for defining Gemtext content.
Functions
Precompile template functions from external files.
Every file found in the wildcard path is compiled as EEx and injected as a template function into the view
module, using the file basename as the function name. For example, index.gmi.eex will be defined as
def index(assigns).
Renders a template.
The assigns and block arguments are optional.
If a block is passed, its contents set to @inner_content assign of the template.
Examples
<%= render &my_template/1 %>
<%= render &my_template/1, title: @title %>
<%= render &my_template/1 do %>
inner content here
<% end %>
Sigil for defining Gemtext content.
The assigns map must be in-scope.
Modifiers
t- Trim trailing newline
Multi-line strings always end with a newline, which may
be undesired. Append the t modifier to the ~G sigil to trim the trailing newline.
Example
def hello(assigns) do
~G"""
Why hello there, <%= @name %>!
"""t
end
hello(%{name: "capsuleer"})
# => "Why hello there, capsuleer!"