Templating

There are two official middlewares built for templating for your Macaron application currently, which are macaron.Renderer and pongo2.Pongoer.

You're free to choose one of them to use, and one Macaron Instance only uses one templating engine.

Common behaviors:

  • Both of them are supporting render XML, JSON and raw content as response, the only difference between them is the way to render HTML.

  • Both of them use templates as default template file directory.

  • Both of them use .tmpl and .html as default template file extensions.

  • Both of them use Macaron Env to determine whether to cache template files(when macaron.Env == macaron.PROD) or not.

Render HTML

Go Templating Engine

This service can be injected by function macaron.Renderer and is represented by type macaron.Render. It is optional to use, normally, you should use *macaron.Context.Render.This service uses Go built-in templating engine to render your HTML. If you want to know about details of how it works, please see html/template documentation.

Example

Suppose you have following directory structure:

main/
    |__ main.go
    |__ templates/
            |__ hello.tmpl

hello.tmpl:

main.go:

Options

This service also accepts one argument for custom options(macaron.RenderOptions):

Pongo2 Templating Engine

This service can be injected by function pongo2.Pongoer and is represented by type macaron.Render. It is optional to use, normally, you should use *macaron.Context.Render.This service uses Pongo2 v3 templating engine to render your HTML. If you want to know about details of how it works, please see pongo2 documentation.

Example

Suppose you have following directory structure:

hello.tmpl:

main.go:

Options

This service also accepts one argument for custom options(pongo2.Options):

Template Sets

When you have more than one type of template files, you should use template sets, which allows you decide which one to render dynamically at runtime.

To use it in Go templating engine:

To use it in Pongo2 templating engine:

As you can see, the only difference here is two functions macaron.Renderers and pongo2.Pongoers.

The option argument is aiming for defualt template set and settings, and a list of name-directory pairs separate by :.

If the last part of template directory is same as your template set name, you can omit it as follows:

Helper methods for template sets

To check if given template set exists:

To change template set directory:

Quick summary on rendering HTML

As you can see, the only difference between two templating engines to render HTML is the syntax of template files, in the code level, they are exactly the same.

If you just want to get results of rendered HTML, call method *macaron.Context.Render.HTMLString:

Render XML, JSON and raw content

It is fairly easy to render XML, JSON and raw content compare to HTML.

Response status, error and redirect

To response status, error and redirect:

Change template path at runtime

In case you want to change your template path at runtime, call method *macaron.Context.SetTemplatePath. Note that this operation applies to global, not just current request.

Example

Suppose you have following directory structure:

templates/hello.tmpl:

templates2/hello.tmpl:

main.go:

When you first request /old, the response will be <h1>Hello Unknwon</h1>, right after response, the template path has been changed to template2. So when you request /new, the response will be <h1>What's up, Unknwon</h1>.

Last updated

Was this helpful?