stickgen

package module
v0.0.0-...-955f044 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2019 License: MIT Imports: 8 Imported by: 0

README

go-stickgen

GoDoc

Generates Go code for stick templates.

This project is currently in a proof-of-concept state, and is under early development.

Installation

Install the stickgen library and command with:

go get -u github.com/veonik/go-stickgen/...

Usage

Usage: stickgen [-path <templates>] [-out <generated>] <glob>
  -out string
    	Output path (default "./generated")
  -path string
    	Path to templates (default ".")
Usage as a library

Below is a simple example that uses the stickgen Generator.

package main

import (
	"fmt"

	"github.com/tyler-sommer/stick"
	"github.com/veonik/go-stickgen"
)

func main() {
	loader := &stick.MemoryLoader{
		Templates: map[string]string{
			"layout.twig": `Hello, {% block name %}{% endblock %}!`,
			"test.twig":   `{% extends 'layout.twig' %}{% block name %}World{% endblock %}`,
		},
	}

	g := stickgen.NewGenerator("views", loader)
	output, err := g.Generate("test.twig")
	if err != nil {
		fmt.Printf("An error occured: %s", err.Error())
		return
	}

	fmt.Println(output)
	// Output:
	// // Code generated by stickgen.
	// // DO NOT EDIT!
	//
	// package views
	//
	// import (
	// 	"github.com/tyler-sommer/stick"
	// 	"io"
	// 	"fmt"
	// )
	//
	// func blockTestTwigName(env *stick.Env, output io.Writer, ctx map[string]stick.Value) {
	// 	// line 1, offset 43 in test.twig
	// 	fmt.Fprint(output, `World`)
	// }
	//
	// func TemplateTestTwig(env *stick.Env, output io.Writer, ctx map[string]stick.Value) {
	// 	// line 1, offset 0 in layout.twig
	// 	fmt.Fprint(output, `Hello, `)
	// 	// line 1, offset 10 in layout.twig
	// 	blockTestTwigName(env, output, ctx)
	// 	// line 1, offset 37 in layout.twig
	// 	fmt.Fprint(output, `!`)
	// }
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

A Generator handles generating Go code from Twig templates.

func NewGenerator

func NewGenerator(pkgName string, loader stick.Loader) *Generator

NewGenerator creates a new code generator using the given Loader.

Example
loader := &stick.MemoryLoader{
	Templates: map[string]string{
		"layout.twig": `Hello, {% block name %}{% endblock %}!`,
		"test.twig":   `{% extends 'layout.twig' %}{% block name %}World{% endblock %}`,
	},
}

g := stickgen.NewGenerator("views", loader)
output, err := g.Generate("test.twig")
if err != nil {
	fmt.Printf("An error occured: %s", err.Error())
	return
}

fmt.Println(output)
Output:

// Code generated by stickgen.
// DO NOT EDIT!

package views

import (
	"github.com/tyler-sommer/stick"
	"io"
	"fmt"
)

func blockTestTwigName(env *stick.Env, output io.Writer, ctx map[string]stick.Value) {
	// line 1, offset 43 in test.twig
	fmt.Fprint(output, `World`)
}

func TemplateTestTwig(env *stick.Env, output io.Writer, ctx map[string]stick.Value) {
	// line 1, offset 0 in layout.twig
	fmt.Fprint(output, `Hello, `)
	// line 1, offset 10 in layout.twig
	blockTestTwigName(env, output, ctx)
	// line 1, offset 37 in layout.twig
	fmt.Fprint(output, `!`)
}

func (*Generator) Generate

func (g *Generator) Generate(name string) (string, error)

Generate parses the given template and outputs the generated code.

Directories

Path Synopsis
cmd
stickgen
Command stickgen generates Go code from a Stick/Twig template.
Command stickgen generates Go code from a Stick/Twig template.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL