htmlc

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 14 Imported by: 1

README

HTMLC (HTML Compiler)

icon

Compiles HTML to Elixir. Who says HTML is not a programming language?

This module is just another templating engine. With elixir, we can leverage its ability to call individual functions in iex mode. Elixir can quickly build templates on the fly.

Notice: This Project Is Still In Beta.

Installation

go get github.com/tkdeng/htmlc

Usage

import (
  "github.com/tkdeng/htmlc"
)

func main(){
  htmlc.Compile("./src", "./output.exs")
}

Using The Binary

You can opptionally just use the binary instead of importing the module.

./htmlc --src="./src" --out="./output.exs"

You can also specify a port number, to automatically start a static-like http server.

./htmlc --port="3000"

Note: by default, "--src" is set to the current working directory, and "--out" is set to the same directory, with the file name set to the base folder name.

You can also call this method without the "--src" or "--port"

./htmlc --src="/var/www/html"
# is equivalent to
./htmlc /var/www/html

./htmlc --port="3000"
# is equivalent to
./htmlc 3000

# so you can use the method like this
./htmlc /var/www/html 3000

# and the order doesnt matter, as long as the port number is a valid uint16
./htmlc 3000 /var/www/html

# note: the output file must still be specified with "--out"
./htmlc --out="html.exs" /var/www/html 3000

Elixir Template Usage

# compile
./htmlc

# start template engine
elixir "./html.exs"

# render page
> render, mypage/home, mylayout/layout, eyJqc29uIjogImFyZ3MifQ== # base64({"json": "args"})

# render widget (optional)
> widget, mywidget/app, eyJqc29uIjogImFyZ3MifQ== # base64({"json": "args"})

# render layout (optional)
> layout, mylayout/layout, eyJqc29uIjogImFyZ3MifQ==, eyJqc29uIjogImh0bWwgY29udGVudCJ9
#                          base64({"json": "args"}), base64({"json": "html content"})

# stop template engine
stop
IEX Template Usage
# compile
./htmlc --iex

# start template engine
iex "./html.exs"

# render page
iex> App.render "mypage/home", "mylayout/layout", %{args: "myarg"}

# render widget (optional)
iex> App.widget "mywidget/app", %{args: "myarg"}

# render layout (optional)
iex> App.layout "mylayout/layout", %{args: "myarg"}, %{body: "page embed"}

# stop template engine
iex> System.halt

HTML Templates

Layout
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0"/>
  <meta name="description" content="{desc}"/>
  <title>{title}<!-- {variable} --></title>
  {@head} <!-- embed page head -->
</head>
<body>
  {@body} <!-- embed page body -->
</body>
</html>
Page
<_@head> <!-- embed into layout {@head} reference -->
  <link rel="stylesheet" href="/style.css">
</_@head>

<_@body> <!-- embed into {@body} -->
  <h1>Hello, World</h1>

  <!-- use `<_#name>` to embed widgets -->
  <_#app n="2">
    widget body
  </_#app>

  <main>
    {&main} <!-- {&variable} use `&` to allow raw HTML -->
  </main>
</_@body>
Widget
<div class="widget">
  <!-- use <% scripts %> to run elixir (feature not yet implemented) -->
  {n} * 2 = <%
    args.n * 2
  %>
</div>

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorStopped error = errors.New("elixir cmd stopped running")
View Source
var ErrorTimeout error = errors.New("request timed out")
View Source
var IexMode bool = false

Functions

func Compile

func Compile(src string, out string) error

func EscapeExsArgs added in v0.0.3

func EscapeExsArgs(html []byte, quote ...byte) []byte

Types

type ExsEngine added in v0.0.2

type ExsEngine struct {
	// contains filtered or unexported fields
}

func Engine added in v0.0.2

func Engine(file string) (*ExsEngine, error)

func (*ExsEngine) Layout added in v0.0.4

func (exs *ExsEngine) Layout(name string, args Map, cont ...map[string]string) ([]byte, error)

func (*ExsEngine) Render added in v0.0.2

func (exs *ExsEngine) Render(name string, args Map, layout ...string) ([]byte, error)

func (*ExsEngine) Widget added in v0.0.4

func (exs *ExsEngine) Widget(name string, args Map) ([]byte, error)

type Map

type Map map[string]any

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL