Skip to content

sangupta/snowmark

Repository files navigation

snowmark - HTML templates for Go.

Build Status Code Coverage go.mod version GitHub Go Report Card PkgGoDev

snowmark is a library for HTML templates that uses HTML custom tags and custom attributes instead of confusing markup intertwined within HTML. It is very similar to Java Server Pages but uses string manipulation to merge templates. In some ways snowmark is also similar to Velocity templates except using custom tags.

Table of contents

Features

  • Merge HTML templates with custom model
  • Bring your own custom tags
  • Attribute expressions
  • Standard tag library includes:
    • Get variable
    • Set variable (global or in block)
    • If-then

API

Usage Example

Let's use the following example HTML template that we want to merge with our own data model:

<html>
    <head>
        <title>
            <test:get var="pageTitle" />
        </title>
    </head>
</html>

The data model to be used represented as JSON is:

{
    "pageTitle" : "Hello World"
}

The following code allows us to do the same:

// parse HTML doc
template := "<html><head><title><get var='pageTitle' /></title></head></html>"

// create the model
model := snowmark.NewModel()
model.Put("pageTitle", "Hello World")

// create a page processor
processor := snowmark.NewHtmlPageProcessor()

// add all your custom tags
// you have the choice to name each tag differently
processor.AddCustomTag("test:get", snowmark.GetVariableTag)

// call merge
html, _ := processor.MergeHtml(template, model)
fmt.Println(html)

Hacking

Changelog

  • Version 0.1.0
    • Initial release

License

MIT License. Copyright (C) 2022, Sandeep Gupta.