/template

Primary LanguageGo

gopkg.in/jo3-l/template.v3

Fork of botlabs-gg/template, which is itself a fork of the Go standard text/template library.

Originally this was just meant to hold some feature branches to contribute upstream, but it doesn't seem likely that there is interest in accepting them anytime soon. Thus this branch was created so those self-hosting YAGPDB and interested in the new template features can switch easily if they wish. (This package should be a drop-in replacement for botlabs-gg/template.)

See this commit for an example of what needs to change in your code.

Additional features

  • New Go number syntax support: Upstream change; see the Go spec for what's allowed.

    Example:

    {{ $b := 0b111001 }}
    {{ $sep := 1_000_000 }}
    
  • With-else-if support: Does what you think it does.

    Example:

    {{ with $c1 }}
    	c1 truthy; set to dot
    {{ else if $c2 }}
    	c2 truthy
    {{ else }}
    	neither c1 nor c2 truthy
    {{ end }}
    
  • break/continue actions in loops: Does what you think it does.

    Example:

    {{ range seq 0 100 }}
    	{{ if gt . 50 }} {{ break }}
    	{{ else }} {{ continue }}
    	{{ end }}
    {{ end }}
    
  • while loop action: Does what you think it does.

    Example:

    {{ $i := 0 }}
    {{ while lt $i 10 }}
    	{{ $i }}
    	{{ $i = add $i 1 }}
    {{ end }}
    
  • return action and execTemplate built-in: Better support for using associated templates as procedures in templates.

    Example 1:

    {{ define "fac" }}
    	{{ if eq . 0 }}
    		{{ return 1 }}
    	{{ end }}
    	{{ return mult . (execTemplate "fac" (sub . 1)) }}
    {{ end }}
    {{ execTemplate "fac" 5 }}
    

    Example 2:

    {{/* `return` can be used at the top-level, in which case it stops the execution of the template */}}
    {{ if not .CmdArgs }} {{ return sendMessage nil "no args" }} {{ end }}
    {{ index .CmdArgs 0 }}
    {{/* not executed if .CmdArgs is empty, so no possibility for index out of bounds */}}
    
  • try/catch action: Support for gracefully recovering from errors returned from template functions.

    Example 1:

    {{ try }}
    	{{ addReactions "emoji" }}
    {{ catch }}
    	{{/* user probably blocked bot */}}
    	Got error: {{ . }} {{/* dot is set to error */}}
    {{ end }}