jade

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: BSD-3-Clause Imports: 15 Imported by: 0

README

Jade.go - template engine for Go (golang)

Package jade (github.com/Joker/jade) is a simple and fast template engine implementing Jade/Pug template.
Jade precompiles templates to Go code or generates html/template.
Now Jade-lang renamed to Pug template engine.

GoDoc Go Report Card

Jade/Pug syntax

example:

:go:func Index(pageTitle string, youAreUsingJade bool)

mixin for(golang)
    #cmd Precompile jade templates to #{golang} code.

doctype html
html(lang="en")
    head
        title= pageTitle
        script(type='text/javascript').
            if(question){
                answer(40 + 2)
            }
    body
        h1 Jade - template engine
            +for('Go')

        #container.col
            if youAreUsingJade
                p You are amazing
            else
                p Get on it!
            p.
                Jade/Pug is a terse and simple
                templating language with
                a #[strong focus] on performance 
                and powerful features.

becomes

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Jade.go</title>
        <script type="text/javascript">
            if(question){
                answer(40 + 2)
            }
        </script>
    </head>
    <body>
        <h1>Jade - template engine
            <div id="cmd">Precompile jade templates to Go code.</div>
        </h1>
        <div id="container" class="col">
            <p>You are amazing</p>
            <p>
                Jade/Pug is a terse and simple
                templating language with
                a <strong>focus</strong> on performance 
                and powerful features.
            </p>
        </div>
    </body>
</html>

See more examples.

Example usage

jade -pkg=main -writer hello.jade

jade command precompiles hello.jade to hello.jade.go

hello.jade

:go:func(arg) word string
doctype 5
html
    body
        p Hello #{word}!

hello.jade.go

// Code generated by "jade.go"; DO NOT EDIT.
package main

import "io"

const (
    hello__0 = `<!DOCTYPE html><html><body><p>Hello `
    hello__1 = `!</p></body></html>`
)
func tpl_hello(word string, wr io.Writer) {
    buffer := &WriterAsBuffer{wr}
    buffer.WriteString(hello__0)
    WriteEscString(word, buffer)
    buffer.WriteString(hello__1)
}

main.go

package main
//go:generate jade -pkg=main -writer hello.jade

import "net/http"

func main() {
	http.HandleFunc("/", func(wr http.ResponseWriter, req *http.Request) {
		tpl_hello("jade", wr)
	})
	http.ListenAndServe(":8080", nil)
}

Output on localhost:8080 :

<!DOCTYPE html><html><body><p>Hello jade!</p></body></html>

or generate html/template at runtime

package main

import (
    "fmt"
    "github.com/Joker/hpp" // Prettify HTML
    "github.com/Joker/jade"
)

func main() {
    tpl, err := jade.Parse("name_of_tpl", "doctype 5\n html: body: p Hello #{.Word} !")
    if err != nil {
        fmt.Printf("Parse error: %v", err)
        return
    }

    fmt.Printf( "Output:\n\n%s", hpp.PrPrint(tpl) )
}

Output:

<!DOCTYPE html>
<html>
    <body>
        <p>Hello {{.Word}} !</p>
    </body>
</html>

Installation

$ go get github.com/Joker/jade/cmd/jade

Custom filter :go

This filter is used as helper for command line tool
(to set imports, function name and parameters).
Filter may be located at any nesting level.
When jade used as library :go filter is not needed.

Nested filter :func
:go:func
    CustomNameForTemplateFunc(any []int, input string, args map[string]int)

:go:func(name)
    OnlyCustomNameForTemplateFunc

:go:func(args)
    (only string, input float32, args uint)
Nested filter :import
:go:import
    "github.com/Joker/jade"
    github.com/Joker/hpp

Documentation

Overview

Jade.go - template engine. Package implements Jade-lang templates for generating Go html/template output.

Index

Constants

This section is empty.

Variables

View Source
var TabSize = 4

Functions

func Config

func Config(c Cfg)

func ConfigOtputPHP

func ConfigOtputPHP()

func Parse

func Parse(name string, text []byte) (string, error)

Parse parses the template definition string to construct a representation of the template for execution.

Trivial usage:

package main

import (
	"fmt"
	"github.com/Joker/jade"
)

func main() {
	tpl, err := jade.Parse("tpl_name", "doctype 5: html: body: p Hello world!")
	if err != nil {
		fmt.Printf("Parse error: %v", err)
		return
	}

	fmt.Printf( "Output:\n\n%s", tpl  )
}

Output:

<!DOCTYPE html><html><body><p>Hello world!</p></body></html>

func ParseFile

func ParseFile(filename string) (string, error)

ParseFile parse the jade template file in given filename

Types

type BlockNode

type BlockNode struct {
	NodeType
	Pos

	Name string
	// contains filtered or unexported fields
}

func (*BlockNode) Copy

func (t *BlockNode) Copy() Node

func (*BlockNode) String

func (t *BlockNode) String() string

func (*BlockNode) WriteIn

func (t *BlockNode) WriteIn(b io.Writer)

type Cfg

type Cfg struct {
	GolangMode bool
	TagBgn     string
	TagEnd     string
	TagVoid    string
	TagArgEsc  string
	TagArgUne  string
	TagArgStr  string
	TagArgAdd  string
	TagArgBgn  string
	TagArgEnd  string

	CondIf     string
	CondUnless string
	CondCase   string
	CondWhile  string
	CondFor    string
	CondEnd    string
	CondForIf  string

	CodeForElse   string
	CodeLongcode  string
	CodeBuffered  string
	CodeUnescaped string
	CodeElse      string
	CodeElseIf    string
	CodeCaseWhen  string
	CodeCaseDef   string
	CodeMixBlock  string

	TextStr     string
	TextComment string

	MixinBgn         string
	MixinEnd         string
	MixinVarBgn      string
	MixinVar         string
	MixinVarRest     string
	MixinVarEnd      string
	MixinVarBlockBgn string
	MixinVarBlock    string
	MixinVarBlockEnd string
}

type CodeNode

type CodeNode struct {
	NodeType
	Pos

	Code []byte // The text; may span newlines.
	// contains filtered or unexported fields
}

func (*CodeNode) Copy

func (t *CodeNode) Copy() Node

func (*CodeNode) String

func (t *CodeNode) String() string

func (*CodeNode) WriteIn

func (t *CodeNode) WriteIn(b io.Writer)

type CondNode

type CondNode struct {
	NodeType
	Pos

	Nodes []Node
	// contains filtered or unexported fields
}

func (*CondNode) Copy

func (l *CondNode) Copy() Node

func (*CondNode) CopyCond

func (l *CondNode) CopyCond() *CondNode

func (*CondNode) String

func (l *CondNode) String() string

func (*CondNode) WriteIn

func (l *CondNode) WriteIn(b io.Writer)

type DoctypeNode

type DoctypeNode struct {
	NodeType
	Pos
	// contains filtered or unexported fields
}

func (*DoctypeNode) Copy

func (d *DoctypeNode) Copy() Node

func (*DoctypeNode) String

func (d *DoctypeNode) String() string

func (*DoctypeNode) WriteIn

func (d *DoctypeNode) WriteIn(b io.Writer)

type ListNode

type ListNode struct {
	NodeType
	Pos

	Nodes []Node // The element nodes in lexical order.
	// contains filtered or unexported fields
}

ListNode holds a sequence of nodes.

func (*ListNode) Copy

func (l *ListNode) Copy() Node

func (*ListNode) CopyList

func (l *ListNode) CopyList() *ListNode

func (*ListNode) String

func (l *ListNode) String() string

func (*ListNode) WriteIn

func (l *ListNode) WriteIn(b io.Writer)

type MixinNode

type MixinNode struct {
	NodeType
	Pos

	Nodes     []Node
	AttrName  []string
	AttrCode  []string
	AttrRest  []string
	MixinName string
	// contains filtered or unexported fields
}

func (*MixinNode) Copy

func (l *MixinNode) Copy() Node

func (*MixinNode) CopyMixin

func (l *MixinNode) CopyMixin() *MixinNode

func (*MixinNode) String

func (l *MixinNode) String() string

func (*MixinNode) WriteIn

func (l *MixinNode) WriteIn(b io.Writer)

type Node

type Node interface {
	Type() NodeType
	String() string
	WriteIn(io.Writer)
	// Copy does a deep copy of the Node and all its components.
	// To avoid type assertions, some XxxNodes also have specialized
	// CopyXxx methods that return *XxxNode.
	Copy() Node
	Position() Pos // byte position of start of node in full original input string
	// contains filtered or unexported methods
}

A Node is an element in the parse tree. The interface is trivial. The interface contains an unexported method so that only types local to this package can satisfy it.

type NodeType

type NodeType int

NodeType identifies the type of a parse tree node.

const (
	NodeText NodeType = iota
	NodeList
	NodeTag
	NodeCode
	NodeCond
	NodeString
	NodeDoctype
	NodeMixin
	NodeBlock
)

func (NodeType) String

func (i NodeType) String() string

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes.

type Out

type Out struct {
	Name, Args, Import string
}
var Go Out

type Pos

type Pos int

Pos represents a byte position in the original input text from which this template was parsed.

func (Pos) Position

func (p Pos) Position() Pos

type TagNode

type TagNode struct {
	NodeType
	Pos

	Nodes    []Node
	AttrName []string
	AttrCode []string
	AttrUesc []bool
	TagName  string
	// contains filtered or unexported fields
}

func (*TagNode) Copy

func (l *TagNode) Copy() Node

func (*TagNode) CopyTag

func (l *TagNode) CopyTag() *TagNode

func (*TagNode) String

func (l *TagNode) String() string

func (*TagNode) WriteIn

func (l *TagNode) WriteIn(b io.Writer)

type TextNode

type TextNode struct {
	NodeType
	Pos

	Text []byte // The text; may span newlines.
	// contains filtered or unexported fields
}

func (*TextNode) Copy

func (t *TextNode) Copy() Node

func (*TextNode) String

func (t *TextNode) String() string

func (*TextNode) WriteIn

func (t *TextNode) WriteIn(b io.Writer)

type Tree

type Tree struct {
	Name string    // name of the template represented by the tree.
	Root *ListNode // top-level root of the tree.
	// contains filtered or unexported fields
}

Tree is the representation of a single parsed template.

func New

func New(name string) *Tree

New allocates a new parse tree with the given name.

func (*Tree) Copy

func (t *Tree) Copy() *Tree

Copy returns a copy of the Tree. Any parsing state is discarded.

func (*Tree) Parse

func (t *Tree) Parse(text []byte) (tree *Tree, err error)

func (*Tree) WriteIn

func (t *Tree) WriteIn(b io.Writer)

Directories

Path Synopsis
cmd
Code generated by "jade.go"; DO NOT EDIT.
Code generated by "jade.go"; DO NOT EDIT.

Jump to

Keyboard shortcuts

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