Giter Site home page Giter Site logo

nt's Introduction

nt

GitHub go.mod Go version

GitHub

GitHub release (latest by date)

nt is a Go package that provides a simple and flexible way to create and use templates with context variables and functions.

Installation

To install nt, use the go get command:

go get github.com/levinion/nt

Usage

To use nt, you need to import it in your Go files:

import "github.com/levinion/nt"

Then you can create a template with the Create function, which takes an optional name argument. If you specify a name, you can later find the template with the Find function.

t := nt.Create("mytemplate")

You can register or append template functions with the Join method, which takes a function that accepts a *nt.Ctx argument. The Ctx type is a struct that holds a map of variables, an error field, and a read-only flag. You can use the Set, SafeSet, and Get methods to manipulate the variables in the context. You can also use the Error method to set an error and stop the remaining functions from executing.

t.Join(func(c *nt.Ctx) {
    c.Set("name", "Alice")
    c.Set("age", 25)
})

You can call the template with the Call method, which also takes a function that accepts a *nt.Ctx argument. This function can access the variables and error set by the previous functions, but cannot modify them.

err := t.Call(func(c *nt.Ctx) {
    name := c.Get("name")
    age := c.Get("age")
    fmt.Printf("Hello, %s. You are %d years old.\n", name, age)
})
if err != nil {
    // handle error
}

You can also add variables to the context with the Watch and WatchMany methods, which take a key-value pair and a map of key-value pairs respectively.

t.Watch("city", "New York")
t.WatchMany(map[string]any{
    "country": "USA",
    "state": "NY",
})

You can merge two templates with the Concat method, which combines their contexts and functions.

t1 := nt.Create()
t1.Join(func(c *nt.Ctx) {
    c.Set("foo", "bar")
})

t2 := nt.Create()
t2.Join(func(c *nt.Ctx) {
    c.Set("baz", "qux")
})

t1.Concat(t2) // t1 now has both foo and baz variables

License

nt is licensed under the MIT license. See LICENSE for more details.

nt's People

Contributors

levinion 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.