tvecty

package module
v0.0.0-...-8504736 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: MIT Imports: 11 Imported by: 0

README

tvecty

An experiment for embedding html templates in Go files that can rendered into Vecty code (https://github.com/hexops/vecty).

example.vtpl

package comps

import (
	"github.com/hexops/vecty"
	"github.com/hexops/vecty/elem"
)

func ExampleComp() vecty.ComponentOrHTML {
    embedAsText := "embed this as text"
    child := <div>this is a child</div>
	
    return <div class="root">
    	<p>this is some test</p>
    	<p>{s:embedAsText}</p>
        {child}
        {AnotherComp()}
    </div>
}

func AnotherComp() vecty.ComponentOrHTML {
    //...
}

Installation

go install github.com/mdev5000/tvecty/cmd/tvecty@latest

Usage

Compiling the example above via:

tvecty compile file example.vtpl

Will output the following:

package comps

import (
	"github.com/hexops/vecty"
	"github.com/hexops/vecty/elem"
)

func ExampleComp() vecty.ComponentOrHTML {
	embedAsText := "embed this as text"
	child :=
		elem.Div(
			vecty.Text("this is a child"),
		)
	return elem.Div(
		vecty.Markup(
			vecty.Class("root"),
		),
		elem.Paragraph(
			vecty.Text("this is some test"),
		),
		elem.Paragraph(
			vecty.Text(embedAsText),
		),
		child,
		AnotherComp())
}

func AnotherComp() vecty.ComponentOrHTML {
	//...
}

You can also output directly to file or compile based on directory matching.

# compile a single file
tvecty compile file example.vtpl example.vtpl.go

# compile a directory file
tvecty compile dir ./tpl/**/*.vtpl

Or setup to use with Go generate.

tpl/gen.go

//go:generate tvecty compile directory **/*.vtpl
go gen ./tpl

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToVecty

func ConvertToVecty(filename string, w io.Writer, src []byte) error

func ExtractHtml

func ExtractHtml(filename string, w io.Writer, src []byte) ([]*html.TagOrText, error)

func FinishHtmlFuncDefinitions

func FinishHtmlFuncDefinitions(f *dst.File) error

FinishHtmlFuncDefinitions trims the comments indicating a function is an html function and adds the correct return type. An html function has no return since the return is always vecty.HTMLOrComponent Ex. html MyHtmlFn() { /* ... */ }

func Replace

func Replace(h ReplacementFinder, f *dst.File) error

Types

type ReplacementFinder

type ReplacementFinder interface {
	Get(id int) (dst.Expr, bool)
}

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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