Giter Club home page Giter Club logo

go-render's Introduction

Render

A convenience template loader for stdlib's html/template. Takes away the pain of manually having to parse all files for a specific template.

Installation

go get github.com/gust1n/go-render/render

Usage

import (
    "github.com/gust1n/go-render/render"
)

func main() {
    templates, err := render.Load("templates")
    if err != nil {
        panic(err)
    }
    // Now I have a map[string]*template.Template to use in my handlers
}

Examples

Check out the examples folder for some examples

Extends

Just use the standard template keyword with a *.html file path. REMEMBER to pass the context to the parent template with the trailing dot (. }}).

index.html

{{ template "templates/layouts/fullwidth.html" . }}

{{ define "content" }}
    content of index to be inserted into the fullwidth template
{{ end }}

This will also work with multi-level support, e.g. index.html ---extends---> layouts/fullwidth.html ---extends---> base.html

Include

Automatically parse the right file just by writing the path to it

{{ define "content" }}
    content of the fullwidth template
    {{ template "includes/widgets/signup.html" . }}
{{ end }}

Overwriting define / default value

Any "define" of the same "template" down the extend chain will overwrite the former content This can be used to define default values for a {{ template }} like so

base.html

<!DOCTYPE html>
<html>
  <head>
    <title>{{ template "title" }}</title>
  </head>
</html>

{{ define "title" }}Default Title{{ end }}

profile.html

{{ template "templates/base.html" . }}
{{ define "title" }}Hello World{{ end }}

This would produce panic in std lib parsing but now it works by simply renaming the define's further down the chain not to interrupt the most specific one.

Custom FuncMap

The Renderer can load a custom FuncMap that is injected into every template. Use it as such:

var tmplErr error
templates, tmplErr = render.LoadWithFuncMap("templates", template.FuncMap{
    "greet": func(name string) string {
        return fmt.Sprintf("Hello %s", name)
    },
})
if tmplErr != nil {
    panic(tmplErr)
}

Credits

Inspired by https://github.com/daemonl/go_sweetpl

Disclaimer

This is me experimenting and trying to make more use of go templates. I do NOT currently use this in production

go-render's People

Contributors

gust1n avatar

Stargazers

 avatar Ishan Das Sharma avatar Daniel Cruz avatar  avatar Malcolm Fell avatar clude avatar Michael Chan avatar Mike Watkins avatar  avatar cnphpbb avatar  avatar Jonathan Ingram avatar Peter Vrba avatar

Watchers

James Cloos avatar  avatar

go-render's Issues

Yield/Parent functionality

I would like something like {{ yield }} to have both default content and the extended. Like

base.html
{{ template "sidebar" . }}
{{ define "sidebar" }}
Default sidebar
{{ end }}

child.html
{{ define "sidebar" }}
{{ parent }}
Child sidebar
{{ end }}

producing:
Default sidebar
Child sidebar

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.