github.com/gofiber/pug@v1.0.1/template.go (about)

     1  // Jade.go - template engine. Package implements Jade-lang templates for generating Go html/template output.
     2  package jade
     3  
     4  import (
     5  	"bytes"
     6  	"io"
     7  	"io/ioutil"
     8  	"path/filepath"
     9  )
    10  
    11  /*
    12  Parse parses the template definition string to construct a representation of the template for execution.
    13  
    14  Trivial usage:
    15  
    16  	package main
    17  
    18  	import (
    19  		"fmt"
    20  		"github.com/Joker/jade"
    21  	)
    22  
    23  	func main() {
    24  		tpl, err := jade.Parse("tpl_name", "doctype 5: html: body: p Hello world!")
    25  		if err != nil {
    26  			fmt.Printf("Parse error: %v", err)
    27  			return
    28  		}
    29  
    30  		fmt.Printf( "Output:\n\n%s", tpl  )
    31  	}
    32  
    33  Output:
    34  
    35  	<!DOCTYPE html><html><body><p>Hello world!</p></body></html>
    36  */
    37  func Parse(name string, text []byte) (string, error) {
    38  	outTpl, err := New(name).Parse(text)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	b := new(bytes.Buffer)
    43  	outTpl.WriteIn(b)
    44  	return b.String(), nil
    45  }
    46  
    47  // ParseFile parse the jade template file in given filename
    48  func ParseFile(filename string) (string, error) {
    49  	bs, err := ioutil.ReadFile(filename)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  	return Parse(filepath.Base(filename), bs)
    54  }
    55  
    56  func (t *Tree) WriteIn(b io.Writer) {
    57  	t.Root.WriteIn(b)
    58  }