Giter Site home page Giter Site logo

stemp's Introduction

GitHub Logo This package aims to help the loading, compiling and managing data of Golang's Templates.

Installation

go get "github.com/gale93/stemp"

Template's folder

In this folder we have 2 different type of files

  • .tmpl
  • .html

stemp will compiles the templates and make them viable in the following way: Pseudo code:

For Each [Html file]
	Join it all [.tmpl] files...

Pass Data to templates

Handling data is very easy because it's already done !

Stemp struct has these two functions :

	AddData(name string, data interface{})
	RemoveData(name string)

So you can manage the data you want to cast to template in 2 different ways:

  1. You can add static data you ALWAYS want to pass to all templates just calling AddData() function one time at the start of the program

  2. You can pass specific data to the template and Remove() it after it is used. Somewhat like this:

func fakehandler(w http.ResponseWriter, r *http.Request) {

	st.AddData("lottery_numbers", []int{3, 2, 9, 2, 3, 12})

	st.Render(&w, "home.html")

	st.RemoveData("lottery_numbers")
}

Code Sample

You will have a base.tmpl file that looks like this:

base.tmpl

{{define "base"}}
<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Stemp</title>
  <meta name="description" content="Stemp">
  <meta name="author" content="Matteo Galeotti">

  {{template "includes" .}}

</head>

<body>
	<nav></nav>

	<div id="content">
		{{template "content" .}}
	</div>
</body>
</html>
{{end}}

{{define "includes" }}{{end}}
{{define "content" }}{{end}}

And a index.html who is the specific content while the user land on main page:

index.html

{{define "content"}}
	<h1> Welcome </h1>
{{end}}

So your Golang file will looks like this !

package main

import (
	"fmt"
	"net/http"
	"os"
	"stemplate/stemp"
)

var st *stemp.Stemplate

func handler(w http.ResponseWriter, r *http.Request) {

	st.AddData("anything_you_want", &TheStructYouNeed)
	st.Render(&w, "index.html")
}

func main() {
	var err error
	// Just give to Stemplate Plugin the folder of your templates and it will do the rest
	st, err = stemp.NewStemplate("./views/")

	// Use this when you are in development and dont want to restart server after modify html files
	st.LiveReload = true


	if err != nil {
		fmt.Println("error initialitating Stemplate")
		os.Exit(1)
	}

	http.HandleFunc("/index", handler)

	// Let's get this party started
	http.ListenAndServe(":9393", nil)
}

stemp's People

Contributors

gale93 avatar

Watchers

 avatar

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.