View Template Functions

By default Go lang provides set of template functions and aah framework provides template functions to access configuration, request parameters, security check, session values, flash values, etc.

Table of Contents

aah Template Funcs

Func: config

Accessing application configuration from aah.AppConfig().

{{ config "format.datetime" }}

Func: import

Renders the common file from views/common with ViewArgs and imports into caller template file.

{{ import "sidebar.html" . }}

Func: rurl

Creates the Reverse URL for the given route name with arguments. Additional arguments are discarded.

  • How to access sub-domain URL for reverse route on root domain template file?
    • Ans: Use sub-domain (the one use have used in routes.conf) prefix before the route name
  • How to access root-domain URL for reverse route on sub-domain template file?
    • Ans: Use root. as prefix before the route name
  • How to I get host url for root domain or sub-domain on template file?
    • Ans: host is the keyword or virtual route name
      • For example: this is applicable to rurlm func too.
        • {{ rurl "host" }} - on root domain template file
        • {{ rurl "root.host" }} - on sub-domain template file
        • {{ rurl "docs.host" }} - on root domain template file access sub-domain host URL
// route name and arguments
// Path: /users/:userId/addresses/:addressId
{{ rurl . "user_address_info"  .UserId .Addresses.AddressId }}

// route name
// Path: /login.html
{{ rurl . "login" }}

==================================
// How to access sub-domain url by route name?
// Ans: use
{{ rurl . "docs.index"}}

Func: rurlm

Creates the Reverse URL for given route name with map arguments. Additional arguments added as URL query parameters.

// route name and arguments
// Path: /v1/users/:userId/addresses/:addressId
{{ rurlm . "user_address_info"  .MapArguments }}

// route name
// Path: /login.html
{{ rurlm . "login" }}

Func: i18n

Access internationalization and localization message from view templates.

{{ i18n . "label.pages.title.get_involved" }}

Func: pparam

Access Path parameter values.

{{ pparam . "userId" }}

Func: fparam

Access Form parameter values.

{{ fparam . "email" }}

Func: qparam

Access URL Query parameter values.

{{ qparam . "lang" }}

Func: flash

Access Flash values. Note: Flash value is deleted after accessing once.

{{ flash . "Username" }}

Func: session

Access Session object values.

{{ session . "Username" }}

Func: isauthenticated

Returns the value of ctx.Subject().IsAuthenticated from current request subject.

<!-- show logout option if user is authenticated -->
{{ if isauthenticated . }}
  <a href="/logout">Logout</a>
{{ end }}

Func: hasrole

Returns true, if Subject (aka User) has given role otherwise false.

{{ if hasrole . "manager" }}
  <!-- You have a role "manager" -->
{{ end }}

Func: hasanyrole

Returns true, if Subject (aka User) has any of the given roles otherwise false.

{{ if hasanyrole . "manager" "dayshift" }}
  <!-- You have any one of the above role -->
{{ end }}

Func: hasallroles

Returns true, if Subject (aka User) has all the given roles otherwise false.

{{ if hasallroles . "role1" "role2" "role3" }}
  <!-- You have all the roles, congrats -->
{{ end }}

Func: ispermitted

Returns true if Subject (aka User) has given permission otherwise false.

{{ if ispermitted . "newsletter:read,write" }}
   <!-- You have permission, you may read or write newsletter -->
{{ end }}

Func: ispermittedall

Returns true if Subject (aka User) has all the given permissions otherwise false.

{{ if ispermittedall . "newsletter:read,write" "newsletter:12345" }}
   <!-- You have permission, you may read or write newsletter of 12345 -->
{{ end }}

Func: safeHTML

Go template strips the HTML comment while rendering the template file. To preserve HTML comment or any special char use safeHTML template func. Of-course use it with care.

Note:

Use only if you trust the HTML source, since it preserves the HTML content without escaping.

<!-- For example: -->
{{ safeHTML `<!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->` }}

Adding your custom Funcs or Third-Party Funcs

You add custom template func using aah.AddTemplateFunc.

func init() {
	aah.AddTemplateFunc(template.FuncMap{
		"myfuncname": func() string {
			return "mycustom function value"
		},
	})
}

Third-Party Funcs

Adding https://github.com/leekchan/gtf

func init() {
  aah.AddTemplateFunc(gtf.GtfFuncMap)
}

Adding https://github.com/Masterminds/sprig

func init()  {
  aah.AddTemplateFunc(sprig.FuncMap())
}