Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bitbucket.org/zappwork/smash

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitbucket.org/zappwork/smash

  • v0.0.0-20200322162931-7c122df14e5d
  • Source
  • Go
  • Socket score

Version published
Created
Source

Smash Golang Templating

codecov.io Go Report Card Build Status

Golang templating is fast and great for rendering html templates, but differs from other templating engines. Smash takes away some of the more complex configurations and restrictions. It is a simple wrapper around the Golang html/template package and is based on the Django templating syntax find documentation here (https://docs.djangoproject.com/en/1.9/ref/templates/builtins/). Most Django functionality has been ported to Golang and a list of tags and filters can be found at the end of this file. Some obscure tags and filters have been omitted from this package.

Smash adds a few functionalities and basic methods, such as extending, including, blocks and setting/changing variables in the main context.

GoDoc

Find documentation at GoDoc

Install

go get bitbucket.org/zappwork/smash

Usage

package main

import (
	"fmt"
	"time"
	"bitbucket.org/zappwork/smash"
)

func main() {
    // Smash use cache default is true. This will only work when using templates stored on disk and only for development purposes.
    smash.LoadWithIdentifier("example", "./tpl/extend.tmpl")
	ctx := smash.Context{"title": "Smash", "spacing": 3.0, "test": 10, "elements": []string{"one", "two", "three", "four"}, "Now": time.Now()}
	tpl := smash.NewTemplate("example", ctx)
	fmt.Println(tpl.Parsed())
	fmt.Println(tpl.String())
}

Smash Methods

Set template paths

Paths can take different formats and setting the main path is optional and the same is for namespaced paths.

SetPath

Set a the main template location path. This is the main location where template will be searched in.

smash.SetPath("./tests/")
tpl := smash.NewTemplate("simple-a.tmpl", smash.Context{"test": "Hello World!"})
fmt.Println(tpl.String())

Path

Returns the current main template path.

smash.SetPath("./tests")
fmt.Println(smash.Path())

AddNamespacedPath

Set a namespaced template location path. This is a namespaced template path and can be used in load, include and extending templates.

smash.AddNamespacedPath("smash", "./tests/namespace1")
tpl := smash.NewTemplate("smash:template.tmpl", nil)
fmt.Println(tpl.String())

NamespacedPath

Returns the requested namespaced template path.

smash.AddNamespacedPath("smash", "./tests/namespace1")
fmt.Println(smash.NamespacedPath("smash"))

LoadAndParse

Load and parse the template found in the path and namespaces paths. This will pre-process all templates found and will speed up initial load time. This will only have any effect if the caching is used.

smash.AddNamespacedPath("smash", "./tests/namespace1")
smash.LoadAndParse()

Set

Set a template as a string and not from file and register it to the pool.

smash.Set("id", "{{ test_var }}")
tpl := smash.NewTemplate("id", smash.Context{"test_var": "Hello World!"})
fmt.Println(tpl.String())

Load

Load a template as from file and register it to the pool.

smash.Load("./tests/simple-a.tmpl")
tpl := smash.NewTemplate("./tests/simple-a.tmpl", smash.Context{"test_var": "Hello World!"})
fmt.Println(tpl.String())

LoadWithIdentifier

Load a template as from file with and id and register it to the pool.

smash.LoadWithIdentifier("id", "./tests/simple-a.tmpl")
tpl := smash.NewTemplate("id", smash.Context{"test_var": "Hello World!"})
fmt.Println(tpl.String())

UseCache

Use caching of templates and parsed templates. During development you may want to disable caching to see the changes that were made to the templates without restarting your whole application.

// Only use for development purposes
smash.UseCache(false)

ClearCache

Clear the cache manually of all loaded and parsed templates

smash.ClearCache()

Supported Tags & Filter

TagsFilters
Supported tagsSupported Filters
extendsloop
blockupper
includelower
comment (single & multiline)default
fordefault_if_none
for ... emptysafe
if, elif and elseconcat
firstofadd
spacelesslength
setlength_is
nowaddslashes
capture (multiline Go >= 1.5)capfirst
cycle (silent not implemented)center
                             | escape

To add tags | escapejs with | divisibleby | filesizeformat Unsupported tags | first ifchanged | last load | join lorem | linebreaksbr regroup | linebreaks ssi | linenumbers templatetag | ljust url | rjust verbatim | make_list widthratio | pluralize | pprint | random | slice | slugify | striptags | title | truncatechars | truncatechars_html | truncatewords | truncatewords_html | urlencode | wordcount | wordwrap | yesno | date | time | now | timeuntil | timesince | stringformat | | Unsupported filters | surlize | surlizetrunc | sunordered_list | ssafeseq | sremovetags (Use striptags) | sphone2numeric | sget_digit | sforce_escape | sdictsortreversed | sdictsort

FAQs

Package last updated on 22 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc