gojade

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

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

Go to latest
Published: Feb 7, 2016 License: MIT Imports: 5 Imported by: 0

README

GoJade

Jade template rendering library for the Go programing language (golang). GoJade is inspired by the javascript template library http://jade-lang.com/

GoJade renders a jade file directly to HTML.

all examples on http://jade-lang.com/ and http://jade-lang.com/reference/ is working except for inline tags and filters. See status for more details.

pull request welcomed.

MIT License (MIT)

Using GoJade

Installing gojade

go get github.com/zdebeer99/gojade

Importing into your project

import (
  "github.com/zdebeer99/gojade"
)

Basic Usage

import (
  "github.com/zdebeer99/gojade"
  "fmt"
)

func main(){
  //GoJade can be declared globally. it will cache templates and keep config information for parsing.
  jade:=gojade.New()
  jade.ViewPath = "./view"

  //RenderFile renders a jade file into html.
  fmt.Println(jade.RenderFile("index.jade", nil).String())
}

GoJade Examples

See the Example Folder folder for examples. Currently the example folder includes;

gojade supports math operators, operator precedence, boolean operators and string concatenation.

-var x = 5
p Do some maths with x = 5
p x * 2 = #{x*2}
p More Maths (x+3)*2
p=(x+3)*2
//- boolean operations
if x>=0 && x<10
  p x is smaller than ten
else
  p x is equal or larger than 10

Status

What is out standing

  • Filters
  • Tag Interpolation

gojade requires a clean up, some function names and parameters may change.

debug information can be improved.

benchmarks and performance improvements has not been done yet.

Differences between jade and gojade

javascript

Keep in mind that gojade does not run in a javascript environment, because of this server side javascript in gojade is not supported as in jade, a work around will be to register custom functions defined in go and calling these functions from your jade template.

Registering Custom Functions.

jade:=gojade.New()
// Register a function called hello, that can be used in your jade template
jade.RegisterFunction("hello", func(name string){return "Hello"+name})

Using the function in your jade.

p= hello("Ben")

methods defined on the model struct passed to the render function is also accessible from jade.

Variables

  • Variable names is case sensitive.

  • Only public fields and methods can be accessed from a struct passed to the render method.

  • nil will eval to an empty string or false in a condition.

  • calling a field or a method on a nil variable will throw an error. Example: lets say the object person is null then "person.Name" will throw an error. but just "person" will return a empty string.

doctype

The doctype shortcut does not support custom doctypes.

Boolean Attribute

the Boolean Attribute

input(type='checkbox', checked=true && 'checked')

is not supported and no plans for support is currently in the pipeline, please log an issue if required. use the ? conditional instead. Example:

input(type='checkbox', checked=true ? 'checked')

Unbuffered Code

Full javascript support for unbuffered code will not be supported as the template runs in go runtime.

Special cases are:

Defining a variable using var

- var x = 5
- var person = {name:"ben", age:5}

[Planned] Calling Unbuffered Functions defined in go.

//Future Implementation, not done yet.
- SomeGoFunction(5,64)
  p This Content will be passed to the last argument of SomeGoFunction()
  p This content is outside of SomeGoFunctions scope.

Useful functions

because gojade does not support javascript, the following will not work.

somestring.toUpperCase()
someArray.length

in go strings does not have methods linked to the string. The Time type for example in go has methods map to it, show this will work.

type Model struct{
  Time time.Time
}
p The Current date is
p= Time.Format("02 Jan 2006")

To assist with these scenarios gojade includes some built in functions and you can register your own go functions, any struct that has methods passed to the render function it's methods will also automatically be available in the Jade parser.

Built in Functions included is:

  • len(value)

    Get the length of an array or string.

  • upper(string)

    Change a string to uppercase

  • lower(string)

    Change a string to lower case

  • format(string,args...)

    The same as fmt.SPrintf() in go

  • isnull(value) bool

    Check if a value is nil

  • ifnull(value, replace)

    Returns replace a value if the value is nil else it returns value

  • json(value)

    convert object to json encoding

  • more is planned, suggestions is welcomed.

Inspired by: jade, go templates, gojade, amber, and indirectly by slim, haml

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	ViewPath string
	Beautify bool
	// contains filtered or unexported fields
}

Engine keeps configuration information and redirect calls to the jadeparser.

func New

func New() *Engine

Creates a new instance of the jade instance struct.

func (*Engine) RegisterFunction

func (this *Engine) RegisterFunction(name string, fn interface{})

RegisterFunction registers a function tobe called from your jade template.

func (*Engine) RenderFile

func (this *Engine) RenderFile(filename string, data interface{}) *bytes.Buffer

RenderFile Renders a jade file to a bytes.Buffer. Example: jade.RenderFile("index.jade",nil).String()

func (*Engine) RenderFileW

func (this *Engine) RenderFileW(wr io.Writer, template string, data interface{}) error

RenderFileW Render a jade file to a io.writer stream.

func (*Engine) RenderString

func (this *Engine) RenderString(template string, data interface{}) *bytes.Buffer

REnderString Renders a jade string to html.

Directories

Path Synopsis
example
gin
GoJade Gin Example demonstrates how to integrate GoJade into Gin.
GoJade Gin Example demonstrates how to integrate GoJade into Gin.
standard
GoJade Standard Example demonstrates how to use gojade with go standard http.
GoJade Standard Example demonstrates how to use gojade with go standard http.

Jump to

Keyboard shortcuts

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