Categorygithub.com/open2b/scriggo
modulepackage
0.56.1
Repository: https://github.com/open2b/scriggo.git
Documentation: pkg.go.dev

# README

Scriggo

The world’s most powerful template engine and Go embeddable interpreter.

Go Reference Go Report Card

Website | Get Started | Documentation | Community | Contributing

Features

  • Fast, a very fast embeddable pure Go language interpreter.
  • Modern and powerful template engine with Go as scripting language.
  • Native support for Markdown in templates.
  • Secure by default. No access to packages unless explicitly enabled.
  • Easy to embed and to interop with any Go application.

Get Started with Programs

Execute a Go program embedded in your application:

package main

import "github.com/open2b/scriggo"

func main() {

    // src is the source code of the program to run.
    src := []byte(`
        package main

        func main() {
            println("Hello, World!")
        }
    `)

    // Create a file system with the file of the program to run.
    fsys := scriggo.Files{"main.go": src}

    // Build the program.
    program, err := scriggo.Build(fsys, nil)
    if err != nil {
        panic(err)
    }
 
    // Run the program.
    err = program.Run(nil)
    if err != nil {
        panic(err)
    }

}

Get Started with Templates

Scriggo, in templates, supports inheritance, macros, partials, imports and contextual autoescaping but most of all it uses the Go language as the template scripting language.

{% extends "layout.html" %}
{% import "banners.html" %}
{% macro Body %}
    <ul>
      {% for product in products %}
      <li><a href="{{ product.URL }}">{{ product.Name }}</a></li>
      {% end %}
    </ul>
    {{ render "pagination.html" }}
    {{ Banner() }}
{% end %}

Scriggo template files can be written in plain text, HTML, Markdown, CSS, JavaScript and JSON.

Execute a Scriggo template in your application

// Build and run a Scriggo template.
package main

import (
	"os"

	"github.com/open2b/scriggo"
	"github.com/open2b/scriggo/builtin"
	"github.com/open2b/scriggo/native"
)

func main() {

    // Content of the template file to run.
    content := []byte(`
    <!DOCTYPE html>
    <html>
    <head>Hello</head> 
    <body>
        Hello, {{ capitalize(who) }}!
    </body>
    </html>
    `)

    // Create a file system with the file of the template to run.
    fsys := scriggo.Files{"index.html": content}

    // Declare some globals.
    var who = "world"
    opts := &scriggo.BuildOptions{
        Globals: native.Declarations{
            "who":        &who,               // global variable
            "capitalize": builtin.Capitalize, // global function
        },
    }

    // Build the template.
    template, err := scriggo.BuildTemplate(fsys, "index.html", opts)
    if err != nil {
        panic(err)
    }
 
    // Run the template and print it to the standard output.
    err = template.Run(os.Stdout, nil, nil)
    if err != nil {
        panic(err)
    }

}

For a complete get started guide see the Scriggo site.

Contributing

Want to help contribute to Scriggo? See CONTRIBUTING.md.

# Packages

Package ast declares the types used to define program and template trees.
Package builtin provides simple functions, types, constants and a package that can be used as globals in a Scriggo template.
No description provided by the author
Package native provides types to implement native variables, constants, functions, types and packages that can be imported or used as builtins in programs and templates.

# Functions

Build builds a program from the package in the root of fsys with the given options.
BuildTemplate builds the named template file rooted at the given file system.
HTMLEscape escapes s, replacing the characters <, >, &, " and ' and returns the escaped string as HTML type.
NewExitError returns an exit error with the given status code and error.

# Constants

Formats.
Formats.
Formats.
Formats.
Formats.
Formats.

# Structs

BuildError represents an error occurred building a program or template.
BuildOptions contains options for building programs and templates.
ExitError represents an exit from an execution with a non-zero status code.
PanicError represents the error that occurs when an executed program or template calls the panic built-in and the panic is not recovered.
Position is a position in a file.
Program is a program compiled with the Build function.
RunOptions are the run options.
Template is a template compiled with the BuildTemplate function.

# Interfaces

FormatFS is the interface implemented by a file system that can determine the file format from a path name.

# Type aliases

Converter is implemented by format converters.
Files implements a file system that read the files from a map.
A Format represents a content format.
PrintFunc represents a function that prints the arguments of the print and println builtins.