Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JuneKelly/sneeze
Render Elixir data-structures to HTML, inspired by Hiccup.
https://github.com/JuneKelly/sneeze
elixir elixir-lang
Last synced: 18 days ago
JSON representation
Render Elixir data-structures to HTML, inspired by Hiccup.
- Host: GitHub
- URL: https://github.com/JuneKelly/sneeze
- Owner: JuneKelly
- License: mit
- Created: 2016-09-28T16:23:07.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2023-10-31T21:17:27.000Z (about 1 year ago)
- Last Synced: 2024-10-12T09:49:59.710Z (about 1 month ago)
- Topics: elixir, elixir-lang
- Language: Elixir
- Homepage:
- Size: 55.7 KB
- Stars: 59
- Watchers: 1
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-elixir - sneeze - Render elixir data structures to HTML. Inspired by [hiccup](https://github.com/weavejester/hiccup). (Templating)
README
# Sneeze
Render Elixir data-structures to HTML. Inspired by [Hiccup](https://github.com/weavejester/hiccup).
## Installation
Sneeze is available from [Hex.pm](https://hex.pm/packages/sneeze):
```elixir
def deps do
[{:sneeze, "~> 2.0"}]
end
```## Usage
The `Sneeze.render/1` function will render a list of 'nodes' to html. A node can be any of:
- A tag: `[:br]`
- A tag with a body: `[:p, "hello"]`
- A tag with attributes: `[:input, %{type: "text", class: "form-field", name: "widget-input"}]`
- A tag with attributes and a body: `[:p, %{class: "article-content"}, "hello"]`
- A bare, stringable node: `22`
- A "raw html" node, which will not be escaped: `[:__@raw_html, "derp"]`Of course, the `body` of any node can be an arbitrary number of other nodes, like so:
`[:p, %{id: "status"}, [:span, "woo"]]`The `Sneeze.render_iodata/1` function returns an iodata list, which can be more performant in cases where you are passing the result to a function that accepts iodata.
## Examples
```elixir
Sneeze.render([:p %{class: "greeting"} "hello world"])
# =>hello world
Sneeze.render([:br])
# =>Sneeze.render([:span, "will be escaped"])
# => <a>will be escaped</a>Sneeze.render([:div, [:__@raw_html, "totally not escaped"]])
# =>totally not escapedSneeze.render(
[:ul, %{class: "super-cool-list"},
[:li,
[:a, %{href: "/"}, "Home"]],
[:li,
[:a, %{href: "/about"}, "About"]],
[:li,
[:a, %{href: "/contact"}, "Contact"]]]
)
# =>
- Home ...
[:head,
[:title, "wat"]],
[:body,
[:div, %{id: "main-content"}, "hello"]]
]
# => wat
Sneeze.render_iodata([:a, %{href: "example.com"}, "hello"])
# => [
# ["<", "a", [[" ", "href", "=\"", "example.com", "\""]], ">"],
# [["hello"]],
# ["", "a", ">"]
# ]
```
If you're using sneeze and getting surprising/screwy results, please [open an issue](https://github.com/JuneKelly/sneeze/issues).
## Documentation
Documentation can be found on [hexdocs](https://hexdocs.pm/sneeze/).
## Demo App
See [cold](https://github.com/JuneKelly/cold-sneeze) for a demo of how to use Sneeze with Plug.
## Bugs, Improvements and Contributing
Open an issue or pull-request on [GitHub](https://github.com/JuneKelly/sneeze), all contributions welcome! :)
## Changes
### 2.0.0
- Improved performance! (https://github.com/JuneKelly/sneeze/pull/19)
- Attributes are now sorted before being rendered. This fixes an issue found when upgrading to OTP 26 (https://github.com/JuneKelly/sneeze/issues/17)
### 1.3.0
- A new `Sneeze.render_iodata/1` function
- Various modernizations, dependency upgrades
### 1.2.1
- Upgrade `html_entities`
### 1.2.0
- Add `html_entities` to application list
### 1.1.0
- Better performance, using iolists instead of string-concatination