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. Add custom or third party Template Funcs. Keywords: template funcs, funcmap, template func map, aah template funcs
By default Go lang provides set of template functions and aah framework provides template functions to access configuration, request parameters, session values, flash values, etc.
Accessing application configuration from aah.AppConfig()
.
{{ config "format.datetime" }}
Renders the common file from views/common
with ViewArgs and imports into caller template file.
{{ import "sidebar.html" . }}
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
- 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 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" }}
Access internationalization and localization message from view templates.
{{ i18n . "label.pages.title.get_involved" }}
Access Path parameter values.
{{ pparam . "userId" }}
Access Form parameter values.
{{ fparam . "email" }}
Access URL Query parameter values.
{{ qparam . "lang" }}
Access Session object values.
{{ session . "Username" }}
Returns the value of Session.IsAuthenticated
from current session.
// show logout option if user is authenticated
{{ if isauthenticated . }}
<a href="/logout">Logout</a>
{{ end }}
Access Flash values. Note: Flash value is deleted after accessing once.
{{ flash . "Username" }}
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.
{{ 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.AddTemplateFunc
.
func init() {
aah.AddTemplateFunc(template.FuncMap{
"myfuncname": func() string {
return "mycustom function value"
},
})
}
func init() {
aah.AddTemplateFunc(gtf.GtfFuncMap)
}
func init() {
aah.AddTemplateFunc(sprig.FuncMap())
}