Documentation ¶
Overview ¶
A jinja like template-engine
Blog posts about gonja (including introduction and migration): https://www.florian-schlachter.de/?tag=gonja
Complete documentation on the template language: https://docs.djangoproject.com/en/dev/topics/templates/
Try out gonja live in the gonja playground: https://www.florian-schlachter.de/gonja/
Make sure to read README.md in the repository as well.
A tiny example with template strings:
(Snippet on playground: https://www.florian-schlachter.de/gonja/?id=1206546277)
// Compile the template first (i. e. creating the AST) tpl, err := gonja.FromString("Hello {{ name|capfirst }}!") if err != nil { panic(err) } // Now you can render the template with the given // gonja.Context how often you want to. out, err := tpl.Execute(gonja.Context{"name": "fred"}) if err != nil { panic(err) } fmt.Println(out) // Output: Hello Fred!
Index ¶
- Constants
- Variables
- func Must(tpl *exec.Template, err error) *exec.Template
- type Config
- type Context
- type Environment
- func (env *Environment) CleanCache(filenames ...string)
- func (env *Environment) FromBytes(tpl []byte) (*exec.Template, error)
- func (env *Environment) FromCache(filename string) (*exec.Template, error)
- func (env *Environment) FromFile(filename string) (*exec.Template, error)
- func (env *Environment) FromString(tpl string) (*exec.Template, error)
- func (env *Environment) GetTemplate(filename string) (*exec.Template, error)
Constants ¶
const VERSION = "0.1.0"
Variables ¶
var ( // DefaultLoader is being used by the DefaultSet. DefaultLoader = loaders.MustNewFileSystemLoader("") // DefaultEnv is an environment created for quick/standalone template rendering. DefaultEnv = NewEnvironment(config.DefaultConfig, DefaultLoader) // Methods on the default set FromString = DefaultEnv.FromString FromBytes = DefaultEnv.FromBytes FromFile = DefaultEnv.FromFile FromCache = DefaultEnv.FromCache // Globals for the default set Globals = DefaultEnv.Globals )
var NewConfig = config.NewConfig
Functions ¶
Types ¶
type Environment ¶
type Environment struct { *exec.EvalConfig Loader loaders.Loader Cache map[string]*exec.Template CacheMutex sync.Mutex }
func NewEnvironment ¶
func NewEnvironment(cfg *config.Config, loader loaders.Loader) *Environment
func (*Environment) CleanCache ¶
func (env *Environment) CleanCache(filenames ...string)
CleanCache cleans the template cache. If filenames is not empty, it will remove the template caches of those filenames. Or it will empty the whole template cache. It is thread-safe.
func (*Environment) FromBytes ¶
func (env *Environment) FromBytes(tpl []byte) (*exec.Template, error)
FromBytes loads a template from bytes and returns a Template instance.
func (*Environment) FromCache ¶
func (env *Environment) FromCache(filename string) (*exec.Template, error)
FromCache is a convenient method to cache templates. It is thread-safe and will only compile the template associated with a filename once. If Environment.Debug is true (for example during development phase), FromCache() will not cache the template and instead recompile it on any call (to make changes to a template live instantaneously).
func (*Environment) FromFile ¶
func (env *Environment) FromFile(filename string) (*exec.Template, error)
FromFile loads a template from a filename and returns a Template instance.
func (*Environment) FromString ¶
func (env *Environment) FromString(tpl string) (*exec.Template, error)
FromString loads a template from string and returns a Template instance.
func (*Environment) GetTemplate ¶
func (env *Environment) GetTemplate(filename string) (*exec.Template, error)