Title: View Template Functions Desc: aah provides template functions to access configuration, request parameters, session data, flash, etc. In addition to default go lang template funcs. You can add custom or third party Template Funcs too. Keywords: template funcs, funcmap, template func map, aah template funcs
By default Go lang provides set of template functions and aah provides template functions to access configuration, request parameters, security check, session values, flash values, etc.
Function | Description | Example |
---|---|---|
config | Accessing application configuration from aah.App().Config() |
{{ config "format.datetime" }} |
i18n | Access internationalization and localization message from view templates | {{ i18n . "label.pages.title.aboutus" }} |
pparam | Access Path parameter values | {{ pparam . "userId" }} |
fparam | Access Form parameter values | {{ fparam . "email" }} |
qparam | Access URL Query parameter values | {{ qparam . "lang" }} |
flash | Access Flash values. Note: Flash value is deleted after accessing once | {{ flash . "error.user.email.incorrect" }} |
session | Access Session object values | {{ session . "Username" }} |
include | Refer here | |
rurl | Create Route URL, Refer here | |
rurlm | Create Route URL, Refer here | |
isauthenticated | Refer here | |
hasrole | Refer here | |
hasanyrole | Refer here | |
hasallroles | Refer here | |
ispermitted | Refer here | |
ispermittedall | Refer here | |
safeHTML | Refer here |
Include and render template file from anywhere under <app-base-dir>/views/**
sub-tree with ViewArgs.
Since v0.12.0 new behaviour introduced.
{{ include "/common/sidebar.html" . }}
{{ include "/users/form.html" . }}
Previous behaviour is deprecated, to be removed in subsequent future release.
<!-- from common directory -->
{{ include "sidebar.html" . }}
Creates the Route 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
- Ans: Use sub-domain (the one use have used in
- How to access root-domain URL for reverse route on sub-domain template file?
- Ans: Use
root.
as prefix before the route name
- Ans: Use
- 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
- For example: this is applicable to
- Ans:
// 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"}}
Creates the Route 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" }}
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 }}
Returns true
, if Subject (aka User) has given role otherwise false
.
{{ if hasrole . "manager" }}
<!-- You have a role "manager" -->
{{ end }}
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 }}
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 }}
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 }}
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 }}
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]-->` }}
You add custom template func using aah.App().AddTemplateFunc
.
func init() {
aah.App().AddTemplateFunc(template.FuncMap{
"myfuncname": func() string {
return "mycustom function value"
},
})
}
func init() {
aah.App().AddTemplateFunc(gtf.GtfFuncMap)
}
func init() {
aah.App().AddTemplateFunc(sprig.FuncMap())
}