Git Product home page Git Product logo

jade's Introduction

Macaron-jade

GoDoc

Macaron middleware/handler for easily rendering serialized JSON and HTML template responses from Jade templates.

If you donot know about jade, learn from here

Usage

This middleware uses Jade implementation in Go go-floki/jade to render Jade templates.

Some examples can be found in examples

// main.go
package main

import (
  "gopkg.in/macaron.v1"
	"github.com/go-macaron/jade"
)

func main() {
	m := macaron.Classic()
	// render html templates from templates directory
	m.Use(jade.Renderer())

	m.Get("/", func(r jade.Render) {
		r.HTML(200, "hello", map[string]string{
			"foo": "bar",
		})
	})

	m.Run()
}

File templates/hello.jade

h2 Hello #{foo}!

Options

jade.Renderer comes with a variety of configuration options:

Layout is not supported.

// ...
m.Use(jade.Renderer(jade.Options{
  Directory: "templates", // Specify what path to load the templates from.
  Extensions: []string{".jade"}, // Specify extensions to load for templates.
  Funcs: []template.FuncMap{AppHelpers}, // Specify helper function maps for templates to access.
  Charset: "UTF-8", // Sets encoding for json and html content-types. Default is "UTF-8".
  IndentJSON: true, // Output human readable JSON
}))
// ...

Loading Templates

By default the jade.Renderer middleware will attempt to load templates with a '.jade' extension from the "templates" directory. Templates are found by traversing the templates directory and are named by path and basename. For instance, the following directory structure:

templates/
  |
  |__ admin/
  |      |
  |      |__ index.jade
  |      |
  |      |__ edit.jade
  |
  |__ home.jade

Will provide the following templates:

admin/index
admin/edit
home

Character Encodings

The jade.Renderer middleware will automatically set the proper Content-Type header based on which function you call. See below for an example of what the default settings would output (note that UTF-8 is the default):

// main.go
package main

import (
  "gopkg.in/macaron.v1"
  "github.com/go-macaron/jade"
)

func main() {
  m := macaron.Classic()
  m.Use(jade.Renderer())

  // This will set the Content-Type header to "text/html; charset=UTF-8"
  m.Get("/", func(r jade.Render) {
    r.HTML(200, "hello", "world")
  })

  // This will set the Content-Type header to "application/json; charset=UTF-8"
  m.Get("/api", func(r jade.Render) {
    r.JSON(200, map[string]interface{}{"hello": "world"})
  })

  m.Run()
}

In order to change the charset, you can set the Charset within the jade.Options to your encoding value:

// main.go
package main

import (
  "gopkg.in/macaron.v1"
  "github.com/go-macaron/jade"
)

func main() {
  m := macaron.Classic()
  m.Use(jade.Renderer(render.Options{
    Charset: "ISO-8859-1",
  }))

  // This is set the Content-Type to "text/html; charset=ISO-8859-1"
  m.Get("/", func(r jade.Render) {
    r.HTML(200, "hello", "world")
  })

  // This is set the Content-Type to "application/json; charset=ISO-8859-1"
  m.Get("/api", func(r jade.Render) {
    r.JSON(200, map[string]interface{}{"hello": "world"})
  })

  m.Run()
}

Authors

jade's People

Contributors

codeskyblue avatar stansm avatar unknwon avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jade's Issues

Loop is not working

Hey guys,

I have a JSON Array with data I got from my Database.
Then at my template when I try:

each ac in access
                    tr
                        td ac

It does not print the content as HTML but it print as JSON object.
Does this Jade/Pug render support loop and conditionals?

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.