All Projects → foolin → echo-template

foolin / echo-template

Licence: MIT License
golang template for echo framework!

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to echo-template

Gin Template
golang template for gin framework!
Stars: ✭ 106 (+171.79%)
Mutual labels:  rice, view, render
Goview
Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.
Stars: ✭ 213 (+446.15%)
Mutual labels:  view, echo
blade
🏃 A library for using Laravel Blade templates in WordPlate.
Stars: ✭ 28 (-28.21%)
Mutual labels:  view, render
render react
Pre-render and mount React components from Ruby
Stars: ✭ 14 (-64.1%)
Mutual labels:  view, render
GonioView
Android view to represent an angle measurement (Goniometry)
Stars: ✭ 27 (-30.77%)
Mutual labels:  view
react-native-masonry-brick-list
Staggered Or Masonary List View For React Native Written in pure js
Stars: ✭ 24 (-38.46%)
Mutual labels:  view
SPConfetti
Show the confetti only when the user is having fun, and if not having fun, don't show it.
Stars: ✭ 187 (+379.49%)
Mutual labels:  view
TicTacToe
No description or website provided.
Stars: ✭ 40 (+2.56%)
Mutual labels:  view
react-native-PixelsCatcher
👀 Library for UI snapshot testing of React Native
Stars: ✭ 99 (+153.85%)
Mutual labels:  view
TitleBar
🔥空祖家的标题栏组件
Stars: ✭ 36 (-7.69%)
Mutual labels:  view
SwiftUIViewRecorder
Efficiently record any SwiftUI View as image or video
Stars: ✭ 20 (-48.72%)
Mutual labels:  view
TextBanner
搜索栏文字轮播切换控件,京东淘宝头条资讯轮播
Stars: ✭ 35 (-10.26%)
Mutual labels:  view
bubble-layout
An Android ViewGroup that displays avatar bubbles... similar to the chat bubbles on Facebook Messenger.
Stars: ✭ 46 (+17.95%)
Mutual labels:  view
table-layout
Styleable plain-text table generator. Useful for formatting console output.
Stars: ✭ 18 (-53.85%)
Mutual labels:  view
PinFloyd
MapKit annotations clustering for iOS
Stars: ✭ 29 (-25.64%)
Mutual labels:  render
axyl-iso
Axyl is a Linux distro centered on tiling window managers. Choose from i3, bspwm, dwm and more.
Stars: ✭ 348 (+792.31%)
Mutual labels:  rice
l2cu
L²CU: LDraw Linux Command line Utility
Stars: ✭ 14 (-64.1%)
Mutual labels:  render
VerifyBlocksView
Android view for providing blocks (Edit Texts) to achieve verification process.
Stars: ✭ 28 (-28.21%)
Mutual labels:  view
android-prefix-suffix-edit-text
EditText with support for non editable prefix and suffix.
Stars: ✭ 36 (-7.69%)
Mutual labels:  view
android-constraintlayout-demo
Demo usage of various ConstraintLayout features
Stars: ✭ 49 (+25.64%)
Mutual labels:  view

echo-template

GoDoc

Golang template for echo framework!

Deprecated!!!

Please consider trying to migrate to Goview

Goview

Goview is a lightweight, simple and easy template library based on golang html/template for building Go web application. Please consider trying to migrate to Goview.

Feature

  • Easy and simple to use for echo framework.
  • Use golang html/template syntax.
  • Support configure master layout file.
  • Support configure template file extension.
  • Support configure templates directory.
  • Support configure cache template.
  • Support include file.
  • Support dynamic reload template(disable cache mode).
  • Support multiple templates for fontend and backend.
  • Support go.rice add all resource files to a executable.

Docs

See https://www.godoc.org/github.com/foolin/echo-template

Install

go get github.com/foolin/echo-template

Usage

package main

import (
	"github.com/labstack/echo"
	"github.com/foolin/echo-template"
	"net/http"
)

func main() {
	// Echo instance
	e := echo.New()

	e.Renderer = echotemplate.Default()

	e.GET("/page", func(c echo.Context) error {
		//render only file, must full name with extension
		return c.Render(http.StatusOK, "page.html", echo.Map{"title": "Page file title!!"})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Configure

    TemplateConfig{
		Root:      "views", //template root path
		Extension: ".tpl", //file extension
		Master:    "layouts/master", //master layout file
		Partials:  []string{"partials/head"}, //partial files
		Funcs: template.FuncMap{
			"sub": func(a, b int) int {
				return a - b
			},
			// more funcs
		},
		DisableCache: false, //if disable cache, auto reload template file for debug.
	}

Render

Render with master

The ctx is instance of echo.Context

//use name without extension `.html`
ctx.Render(http.StatusOK, "index", echo.Map{})

Render only file(not use master layout)

//use full name with extension `.html`
ctx.Render(http.StatusOK, "page.html", echo.Map{})

Include syntax

//template file
{{include "layouts/footer"}}

Examples

Basic example

package main

import (
	"net/http"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/foolin/echo-template"
)

func main() {

	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	//Set Renderer
	e.Renderer = echotemplate.Default()

	// Routes
	e.GET("/", func(c echo.Context) error {
		//render with master
		return c.Render(http.StatusOK, "index", echo.Map{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	e.GET("/page", func(c echo.Context) error {
		//render only file, must full name with extension
		return c.Render(http.StatusOK, "page.html", echo.Map{"title": "Page file title!!"})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
    

See in "examples/basic" folder

Basic example

Advance example

package main

import (
	"net/http"
	"html/template"
	"time"
	"github.com/labstack/echo"
	"github.com/foolin/echo-template"
	"github.com/labstack/echo/middleware"
)

func main() {

	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	//Set Renderer
	e.Renderer = echotemplate.New(echotemplate.TemplateConfig{
		Root:      "views",
		Extension: ".tpl",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"sub": func(a, b int) int {
				return a - b
			},
			"copy": func() string{
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	e.GET("/", func(c echo.Context) error {
		//render with master
		return c.Render(http.StatusOK, "index", echo.Map{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	e.GET("/page", func(c echo.Context) error {
		//render only file, must full name with extension
		return c.Render(http.StatusOK, "page.tpl", echo.Map{"title": "Page file title!!"})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Project structure:

|-- app/views/
    |--- index.tpl          
    |--- page.tpl
    |-- layouts/
        |--- footer.tpl
        |--- head.tpl
        |--- master.tpl
    |-- partials/
        |--- ad.tpl
    

See in "examples/advance" folder

Advance example

Multiple example

package main

import (
	"html/template"
	"net/http"
	"time"

	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/foolin/echo-template"
)

func main() {

	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	//new template engine
	e.Renderer = echotemplate.New(echotemplate.TemplateConfig{
		Root:      "views/fontend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	e.GET("/", func(ctx echo.Context) error {
		// `HTML()` is a helper func to deal with multiple TemplateEngine's.
		// It detects the suitable TemplateEngine for each path automatically.
		return echotemplate.Render(ctx, http.StatusOK, "index", echo.Map{
			"title": "Fontend title!",
		})
	})

	//=========== Backend ===========//

	//new middleware
	mw := echotemplate.NewMiddleware(echotemplate.TemplateConfig{
		Root:      "views/backend",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})

	// You should use helper func `Middleware()` to set the supplied
	// TemplateEngine and make `HTML()` work validly.
	backendGroup := e.Group("/admin", mw)

	backendGroup.GET("/", func(ctx echo.Context) error {
		// With the middleware, `HTML()` can detect the valid TemplateEngine.
		return echotemplate.Render(ctx, http.StatusOK, "index", echo.Map{
			"title": "Backend title!",
		})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Project structure:

|-- app/views/
    |-- fontend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        |-- partials/
            |--- ad.html
    |-- backend/
        |--- index.html
        |-- layouts/
            |--- footer.html
            |--- head.html
            |--- master.html
        
See in "examples/multiple" folder

Multiple example

Block example

/*
 * Copyright 2018 Foolin.  All rights reserved.
 *
 * Use of this source code is governed by a MIT style
 * license that can be found in the LICENSE file.
 *
 */

package main

import (
	"net/http"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/foolin/echo-template"
)

func main() {

	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	//Set Renderer
	e.Renderer = echotemplate.Default()

	// Routes
	e.GET("/", func(c echo.Context) error {
		return c.Render(http.StatusOK, "index", echo.Map{
			"title": "Index title!",
		})
	})

	e.GET("/block", func(c echo.Context) error {
		return c.Render(http.StatusOK, "block", echo.Map{"title": "Block file title!!"})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- block.html
    |-- layouts/
        |--- master.html
        
See in "examples/block" folder

Block example

go.rice example

/*
 * Copyright 2018 Foolin.  All rights reserved.
 *
 * Use of this source code is governed by a MIT style
 * license that can be found in the LICENSE file.
 *
 */

package main

import (
	"net/http"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/GeertJohan/go.rice"
	"github.com/foolin/echo-template/supports/gorice"
)

func main() {

	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	// servers other static files
	staticBox := rice.MustFindBox("static")
	staticFileServer := http.StripPrefix("/static/", http.FileServer(staticBox.HTTPBox()))
	e.GET("/static/*", echo.WrapHandler(staticFileServer))

	//Set Renderer
	e.Renderer = gorice.New(rice.MustFindBox("views"))

	// Routes
	e.GET("/", func(c echo.Context) error {
		//render with master
		return c.Render(http.StatusOK, "index", echo.Map{
			"title": "Index title!",
			"add": func(a int, b int) int {
				return a + b
			},
		})
	})

	e.GET("/page", func(c echo.Context) error {
		//render only file, must full name with extension
		return c.Render(http.StatusOK, "page.html", echo.Map{"title": "Page file title!!"})
	})

	// Start server
	e.Logger.Fatal(e.Start(":9090"))
}

Project structure:

|-- app/views/
    |--- index.html          
    |--- page.html
    |-- layouts/
        |--- footer.html
        |--- master.html
|-- app/static/  
    |-- css/
        |--- bootstrap.css   	
    |-- img/
        |--- gopher.png

See in "examples/gorice" folder

gorice example

Supports

Relative Template

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].