github.com/gofiber/pug@v1.0.1/README.md (about)

     1  # Jade.go - template engine for Go (golang)  
     2  Package jade (github.com/Joker/jade) is a simple and fast template engine implementing Jade/Pug template.  
     3  Jade precompiles templates to Go code or generates html/template.  
     4  Now Jade-lang renamed to [Pug template engine](https://pugjs.org/api/getting-started.html).  
     5  
     6  [![GoDoc](https://godoc.org/github.com/Joker/jade?status.svg)](https://godoc.org/github.com/Joker/jade) [![Go Report Card](https://goreportcard.com/badge/github.com/Joker/jade)](https://goreportcard.com/report/github.com/Joker/jade)
     7  
     8  ## Jade/Pug syntax
     9  example:
    10  
    11  ```jade
    12  :go:func Index(pageTitle string, youAreUsingJade bool)
    13  
    14  mixin for(golang)
    15      #cmd Precompile jade templates to #{golang} code.
    16  
    17  doctype html
    18  html(lang="en")
    19      head
    20          title= pageTitle
    21          script(type='text/javascript').
    22              if(question){
    23                  answer(40 + 2)
    24              }
    25      body
    26          h1 Jade - template engine
    27              +for('Go')
    28  
    29          #container.col
    30              if youAreUsingJade
    31                  p You are amazing
    32              else
    33                  p Get on it!
    34              p.
    35                  Jade/Pug is a terse and simple
    36                  templating language with
    37                  a #[strong focus] on performance 
    38                  and powerful features.
    39  ```
    40  
    41  becomes
    42  
    43  ```html
    44  <!DOCTYPE html>
    45  <html lang="en">
    46      <head>
    47          <title>Jade.go</title>
    48          <script type="text/javascript">
    49              if(question){
    50                  answer(40 + 2)
    51              }
    52          </script>
    53      </head>
    54      <body>
    55          <h1>Jade - template engine
    56              <div id="cmd">Precompile jade templates to Go code.</div>
    57          </h1>
    58          <div id="container" class="col">
    59              <p>You are amazing</p>
    60              <p>
    61                  Jade/Pug is a terse and simple
    62                  templating language with
    63                  a <strong>focus</strong> on performance 
    64                  and powerful features.
    65              </p>
    66          </div>
    67      </body>
    68  </html>
    69  ```
    70  
    71  
    72  See [more](https://github.com/Joker/jade/tree/master/testdata/v2) [examples](https://github.com/Joker/jade/tree/master/example).  
    73  
    74  
    75  ## Example usage
    76  
    77  ```sh
    78  jade -pkg=main -writer hello.jade
    79  ```
    80  jade command precompiles hello.jade to hello.jade.go  
    81  
    82  `hello.jade`
    83  ```
    84  :go:func(arg) word string
    85  doctype 5
    86  html
    87      body
    88          p Hello #{word}!
    89  ```
    90  
    91  `hello.jade.go`
    92  ```go
    93  // Code generated by "jade.go"; DO NOT EDIT.
    94  package main
    95  
    96  import "io"
    97  
    98  const (
    99      hello__0 = `<!DOCTYPE html><html><body><p>Hello `
   100      hello__1 = `!</p></body></html>`
   101  )
   102  func tpl_hello(word string, wr io.Writer) {
   103      buffer := &WriterAsBuffer{wr}
   104      buffer.WriteString(hello__0)
   105      WriteEscString(word, buffer)
   106      buffer.WriteString(hello__1)
   107  }
   108  ```
   109  
   110  `main.go`
   111  ```go
   112  package main
   113  //go:generate jade -pkg=main -writer hello.jade
   114  
   115  import "net/http"
   116  
   117  func main() {
   118  	http.HandleFunc("/", func(wr http.ResponseWriter, req *http.Request) {
   119  		tpl_hello("jade", wr)
   120  	})
   121  	http.ListenAndServe(":8080", nil)
   122  }
   123  ```
   124  
   125  Output on localhost:8080 :
   126  ```html
   127  <!DOCTYPE html><html><body><p>Hello jade!</p></body></html>
   128  ```
   129    
   130    
   131  or generate html/template at runtime
   132  
   133  
   134  ```go
   135  package main
   136  
   137  import (
   138      "fmt"
   139      "github.com/Joker/hpp" // Prettify HTML
   140      "github.com/Joker/jade"
   141  )
   142  
   143  func main() {
   144      tpl, err := jade.Parse("name_of_tpl", "doctype 5\n html: body: p Hello #{.Word} !")
   145      if err != nil {
   146          fmt.Printf("Parse error: %v", err)
   147          return
   148      }
   149  
   150      fmt.Printf( "Output:\n\n%s", hpp.PrPrint(tpl) )
   151  }
   152  ```
   153  
   154  Output:
   155  
   156  ```html
   157  <!DOCTYPE html>
   158  <html>
   159      <body>
   160          <p>Hello {{.Word}} !</p>
   161      </body>
   162  </html>
   163  ```
   164  
   165  
   166  ## Installation
   167  
   168  ```sh
   169  $ go get github.com/Joker/jade/cmd/jade
   170  ```
   171  
   172  
   173  ## Custom filter  :go
   174  This filter is used as helper for command line tool  
   175  (to set imports, function name and parameters).  
   176  Filter may be located at any nesting level.  
   177  When jade used as library :go filter is not needed.  
   178  
   179  ### Nested filter  :func
   180  ```
   181  :go:func
   182      CustomNameForTemplateFunc(any []int, input string, args map[string]int)
   183  
   184  :go:func(name)
   185      OnlyCustomNameForTemplateFunc
   186  
   187  :go:func(args)
   188      (only string, input float32, args uint)
   189  ```
   190  
   191  ### Nested filter  :import
   192  ```
   193  :go:import
   194      "github.com/Joker/jade"
   195      github.com/Joker/hpp
   196  ```