Views - HTML

View is to present user interface for particular application flow and action. For e.g.: login.html, home.html, etc.

OOTB supported view engines are -

  • Default view engine: Go - with flexible, inheritance
  • External view engines
    • Upcoming

Reference to View Config.

Table of Contents

View Directory Structure and Usage

aah provides flexible and meaningful directory structure to organize application view files. Use your creativity, organize and make a best use of it.

  • common - Common template segments/parts goes here, Don’t Repeat Yourself (DRY). Use import or include template function include wherever you need it.
  • errors - Application error pages for 404, 500, 403, etc. Status codes used as file name. Since v0.8.0
  • layouts - Define one or more view layout. Default layout name is master.<ext> and file extension based on view engine from config view.ext.
  • pages - Page template of each controller and it’s action. Also aah user can have your custom page templates.

Note: each controller and its action can have same template filename like Rails. You can have index page template for every controller.

# App base directory
  |-- views
      |--- common
           |--- header.html
           |--- footer.html
           |--- sidebar.html
           |--- ads.html
      |--- errors
           |--- 404.html
           |--- 500.html
      |--- layouts
           |--- master.html
           |--- docs.html
           |--- sitemap.html
      |--- pages
           |--- app     # AppController
                |--- index.html
                |--- login.html
                |--- help.html
                |--- about.html
           |--- doc     # DocController
                |--- index.html
                |--- showversion.html
                |--- overview.html

Template Auto Resolve OR User-Defined Inputs

By default aah resolves and render view templates based on-

  • Namespace Controller package path
  • Path Controller and Action
  • View extension view.ext
  • Case-sensitive view.case_sensitive
  • Default layout is master.html if not provided
  • Since v0.6.0 Config option to disable default layout.

Reference to View Config.

For Example:
    Namespace: admin
    Controller: App
    Action: Login
    view.ext: html
    view.case_sensitive: false

    template ===> /views/pages/admin/app/login.html == /views/pages/admin/App/Login.html

User-Defined Inputs

Ok, I understood the framework default behavior, now how I can have it my way?.

Besides the aah auto view resolve when using method HTML(data) and framework gives you full-control of view rendering via Reply Builder-

  • Reply().HTMLl(layout, data) - layout is user input and framework resolves view template file.
  • Reply().HTMLf(filename, data) - view filename is user input and default master.<ext> layout.
  • Reply().HTMLlf(layout, filename, data) - layout and view filename is user input.
    • Since v0.6.0 if the filename starts with /; framework uses as-is from pages directory.
    • For e.g: HTMLf("/mydir/file.html", data) => becomes views/pages/mydir/file.html
    • For e.g: HTMLf("mydir/file.html", data) => becomes views/pages/<packages>/<controller>/mydir/file.html

Supplying View Arguments

aah provides following ways to add value into ViewArgs, templates are render with ViewArgs.

  • ctx.AddViewArg(key, value) this method is available in entire request life cycle.
    • For e.g.: adding view arg via middleware or in the controller.
  • Via Reply().HTML* methods as a aah.Data{ ... } param.

aah provides access to aah.App().Config(), Session, Flash PathParam, FormParam, and QueryParam on view template via template function.

Adding User-Defined View Engine into aah

Currently aah supports Go template engine. Don’t feel bad, you can added your favorite view engine into aah.

Pug view engine support temporarly removed from aah due to upstream library issue.

Create your own view engine implementing interface view.Enginer

// Enginer interface defines a methods for pluggable view engine.
type Enginer interface {
	Init(appCfg *config.Config, baseDir string) error
	Get(layout, path, tmplName string) (*template.Template, error)

Adding view engine into aah

func init()  {
  app := aah.App()
  if err := app.AddViewEngine("enginename", &MyViewEngine{}); err != nil {

Configuring your custom view engine into aah

Goto view { ... } section in aah.conf.

view {
  engine = "enginename"

  ext = "your-file-extension"

Values made available in ViewArgs by framework

aah provides following values on ViewArgs, so you could use it on templates.

  • Scheme
  • Host
  • HTTPMethod
  • HTTPReferer
  • RequestPath
  • Locale
  • ClientIP
  • IsAJAX
  • AahVersion
  • EnvProfile
  • AppBuildInfo