snowmark

package module
v0.0.0-...-b78c72c Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

View Source
const PREFIX = "expr:"

Variables

This section is empty.

Functions

func ForEachTag

func ForEachTag(node *lhtml.HtmlNode, model *Model, evaluator *Evaluator) error

A for-each tag that takes in a `collection` and then adds it as the given variable name. If the collection is a `slice` the variable gets one collection object at a time.

<foreach collection="mySlice" item="sliceItem">
   <get var="sliceItem" />
</foreach>

If the collection is a `map` the variable gets an object that contains two properties: `key` and `value` representing the key/value pair for each entry in the map.

<foreach collection="myMap" item="mapItem">
   <get var="mapItem.key" />
   <get var="mapItem.value" />
</foreach>

func GetVariableTag

func GetVariableTag(node *lhtml.HtmlNode, model *Model, evaluator *Evaluator) error

A simple tag to get the value of any variable inside the model.

<get value="name" />

func IfElseTag

func IfElseTag(node *lhtml.HtmlNode, model *Model, evaluator *Evaluator) error

A simple if-else tag.

<custom:if condition="age > 10">
   <custom:then>
   </custom:then>
   </custom:else>
   </custom:else>
</custom:if>

func SetVariableTag

func SetVariableTag(node *lhtml.HtmlNode, model *Model, evaluator *Evaluator) error

You can use it in two ways. For a self-closing tag, the model will be modified immediately and any older value will not be preserved. For example, the variable `hello` is set to `world` for the rest of processing.

<setVar var="hello" value="world" />

Another way is to use children, in which case the older value (if any) will be restored at the closing of tag. In the following example, the value of variable `hello` is only available to the children of the `setVar` node.

<setVar var="hello" value="world">
   ...
</setVar>

Types

type CustomTagProcessor

type CustomTagProcessor func(node *lhtml.HtmlNode, model *Model, evaluator *Evaluator) error

type Evaluator

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

The Evaluator instance.

func (*Evaluator) EvaluateExpression

func (evaluator *Evaluator) EvaluateExpression(expr string, model *Model) (interface{}, error)

Evaluate an expression against the model.

func (*Evaluator) EvaluateExpressionAsString

func (evaluator *Evaluator) EvaluateExpressionAsString(expr string, model *Model) (string, error)

Evaluate an expression against the model and return resulting value as a `string` using default platform conversion.

func (*Evaluator) EvaluateNode

func (evaluator *Evaluator) EvaluateNode(node *lhtml.HtmlNode, model *Model) error

Evaluate the given node against the model.

func (*Evaluator) EvaluateNodes

func (evaluator *Evaluator) EvaluateNodes(nodes []*lhtml.HtmlNode, model *Model) error

Evaluate multiple nodes against the model.

func (*Evaluator) GetAttributeValue

func (evaluator *Evaluator) GetAttributeValue(node *lhtml.HtmlNode, attributeName string, model *Model) (interface{}, error)

Get an attribute's value from the node. It also checks if an expression attribute with same name is present, and if yes, if will evalue the expression and return the value.

func (*Evaluator) GetAttributeValueAsString

func (evaluator *Evaluator) GetAttributeValueAsString(node *lhtml.HtmlNode, attributeName string, model *Model) (string, error)

func (*Evaluator) GetEvaluation

func (evaluator *Evaluator) GetEvaluation() string

func (*Evaluator) Write

func (evaluator *Evaluator) Write(b []byte) (int, error)

Write byte array to internal string builder.

func (*Evaluator) WriteByte

func (evaluator *Evaluator) WriteByte(b byte) error

Write single byte to internal string builder.

func (*Evaluator) WriteRune

func (evaluator *Evaluator) WriteRune(r rune) (int, error)

Write rune to internal string builder.

func (*Evaluator) WriteString

func (evaluator *Evaluator) WriteString(s string) (int, error)

Write given string to internal string builder.

type HtmlPageProcessor

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

The main interface that allows you to add custom tag processors and then use this instance to merge HTML templates.

func NewHtmlPageProcessor

func NewHtmlPageProcessor() *HtmlPageProcessor

Create a new instance of HTML page processor.

func (*HtmlPageProcessor) AddCustomTag

func (pageProcessor *HtmlPageProcessor) AddCustomTag(name string, tagProcessor CustomTagProcessor) (bool, error)

Add a custom tag to the processor which will make use of the provided `func` to work upon. The tag name is case insensitive. If a tag with the same name already exists, an error is returned.

func (*HtmlPageProcessor) GetCustomTag

func (pageProcessor *HtmlPageProcessor) GetCustomTag(name string) (CustomTagProcessor, bool)

Return the custom tag processor attached for the given name. If the name is empty or blank, a `nil` is returned.

func (*HtmlPageProcessor) HasCustomTag

func (pageProcessor *HtmlPageProcessor) HasCustomTag(name string) bool

Check if a custom tag processor is attached for the given name.

func (*HtmlPageProcessor) Merge

func (pageProcessor *HtmlPageProcessor) Merge(elements *lhtml.HtmlElements, model *Model) (string, error)

Merge given parsed HTML document with the given model.

func (*HtmlPageProcessor) MergeHtml

func (pageProcessor *HtmlPageProcessor) MergeHtml(html string, model *Model) (string, error)

Merge given HTML string with the given model.

func (*HtmlPageProcessor) RemoveCustomTag

func (pageProcessor *HtmlPageProcessor) RemoveCustomTag(name string) (bool, error)

Remove a custom tag with given name. An error is returned if the name is empty, or blank. No error is returned if no custom tag processor can be found attached to this name.

type Model

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

Represents a model of values that are used to merge with a page or a fragment.

func NewModel

func NewModel() *Model

Create new model instance.

func (*Model) Clear

func (model *Model) Clear()

Clear model and remove all keys

func (*Model) Get

func (model *Model) Get(key string) (interface{}, bool)

Get the value from the model.

func (*Model) GetBool

func (model *Model) GetBool(key string, defaultValue bool) bool

Get a `bool` value for a key from the model, or the default value if the key does not exist, or is not a `bool`.

func (*Model) GetFloat64

func (model *Model) GetFloat64(key string, defaultValue float64) float64

Get a `float64` value for a key from the model, or the default value if the key does not exist, or is not a `float64`.

func (*Model) GetInt64

func (model *Model) GetInt64(key string, defaultValue int64) int64

Get a `int64` value for a key from the model, or the default value if the key does not exist, or is not a `int64`.

func (*Model) GetMap

func (model *Model) GetMap() map[string]interface{}

Return the map associated with this model.

func (*Model) GetString

func (model *Model) GetString(key string, defaultValue string) string

Get a `string` value for a key from the model, or the default value if the key does not exist, or is not a `string`.

func (*Model) GetUInt64

func (model *Model) GetUInt64(key string, defaultValue uint64) uint64

Get a `uint64` value for a key from the model, or the default value if the key does not exist, or is not a `uint64`.

func (*Model) IsEmpty

func (model *Model) IsEmpty() bool

Check if model is empty or not. Returns `true` if there are no keys in model, `false` otherwise.

func (*Model) Put

func (model *Model) Put(key string, value interface{})

Put the value in model against given key.

func (*Model) PutIfNotExists

func (model *Model) PutIfNotExists(key string, value interface{}) bool

Put the value in model if the key does not exist. Returns `true` if value was added to model, `false` otherwise.

func (*Model) Remove

func (model *Model) Remove(key string)

Remove the key from the model, if it exists.

func (*Model) Replace

func (model *Model) Replace(key string, value interface{}) bool

Replace the current key with new value. If no key exists the function returns `false`. Returns `true` if value was replaced.

func (*Model) Size

func (model *Model) Size() int

Return the size of the model.

Jump to

Keyboard shortcuts

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