Giter VIP home page Giter VIP logo

kaptinlin / template Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 66 KB

A Go Template Engine library enabling dynamic templating with variable interpolation and data manipulation, inspired by Liquid and Django's syntax. It allows for easy parsing and executing of templates with context, supporting quick rendering methods, error handling, and custom filter extensions for enhanced data manipulation within templates.

License: MIT License

Go 98.75% Makefile 1.25%
template template-engine view

template's Introduction

Simple Go Template Engine

Overview

This Go Template Engine library introduces dynamic templating for Go applications, enabling variable interpolation and data manipulation with filters. Drawing inspiration from the syntax of Liquid and Django, it simplifies the generation of dynamic content.

Getting Started

Installation

Ensure you have Go set up on your system. To add the Go Template Engine to your project, run:

go get github.com/kaptinlin/template

This command downloads the library and prepares it for use in your project.

Basic Usage

Parsing and Executing a Template

Create and parse a template, then execute it with a context:

package main

import (
	"fmt"
	"github.com/kaptinlin/template"
)

func main() {
    // Define your template
    source := "Hello, {{ name }}!"
    // Parse the template
    tpl, err := template.Parse(source)
    if err != nil {
        panic(err)
    }
    
    // Create a context and add variables
    context := template.NewContext()
    context.Set("name", "World")
    
    // Execute the template
    output, err := template.Execute(tpl, context)
    if err != nil {
        panic(err)
    }
    
    fmt.Println(output) // Output: Hello, World!
}

Quick Parsing and Execution with Render

Directly parse and execute a template in one step:

package main

import (
	"fmt"
	"github.com/kaptinlin/template"
)

func main() {
    // Define your template and context
    source := "Goodbye, {{ name }}!"
    context := template.NewContext()
    context.Set("name", "Mars")
    
    // Render the template
    output, err := template.Render(source, context)
    if err != nil {
        panic(err)
    }
    
    fmt.Println(output) // Output: Goodbye, Mars!
}

Ignoring Errors with MustExecute

Execute a template and ignore any errors, useful for templates guaranteed not to fail:

package main

import (
	"fmt"
	"github.com/kaptinlin/template"
)

func main() {
    // Define your template
    source := "Welcome, {{ name }}!"
    // Parse the template
    tpl, err := template.Parse(source)
    if err != nil {
        panic(err)
    }
    
    // Create a context and add variables
    context := template.NewContext()
    context.Set("name", "Universe")
    
    // MustExecute the template, ignoring errors
    output := template.MustExecute(tpl, context)
    
    fmt.Println(output) // Output: Welcome, Universe!
}

Syntax and Features

Variables

Enclose variables in {{ }} to embed dynamic content:

{{ userName }}

For extended syntax, refer to the documentation.

Filters

Use the pipe | to apply filters to variables:

Hello, {{ name|capitalize }}!

Detailed usage can be found in the documentation.

Custom Filters

Easily extend functionality by adding custom filters. For example, a filter to capitalize a string:

package main

import (
	"github.com/kaptinlin/template"
	"strings"
)

func capitalize(input interface{}, args ...string) (interface{}, error) {
	s, ok := input.(string)
	if !ok {
		return input, nil
	}
	return strings.Title(s), nil
}

func init() {
	template.RegisterFilter("capitalize", capitalize)
}

Use the custom filter like so:

{{ "john doe"|capitalize }}

Context Management

Contexts pass variables to templates. Here’s how to create and use one:

context := template.NewContext()
context.Set("key", "value")

How to Contribute

Contributions to the template package are welcome. If you'd like to contribute, please follow the contribution guidelines.

License

This project is licensed under the MIT License - see the LICENSE file for details.

template's People

Contributors

kaptinlin avatar dependabot[bot] avatar

Stargazers

 avatar  avatar peleus avatar

Watchers

 avatar peleus avatar

Forkers

guochengh

template's Issues

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.