Views - HTML UI

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

Out-of-the-box of the aah provides Partial Inheritance capability using Go built-in template engine.

Reference to View Config.

Table of Contents

View Directory Structure and Usage

aah provides flexible and effective directory structure to organize your view files. Minimizes your copy and paste of view content. Use your creativity, organize and make best use of it.

  • common - common template segments/parts goes here, Don’t Repeat Yourself (DRY). Use import 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
  • layouts - You can define one or more view layout for your application. Default layout name is master.html.
  • pages - templates of each controller and it’s action. Also you can have your custom page templates.

Note: each controller and its action can have same template filename like Rails. You can have Index 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
                |--- index.html
                |--- login.html
                |--- help.html
                |--- about.html
           |--- doc
                |--- index.html
                |--- showversion.html
                |--- overview.html

Template Auto Resolve OR User-Defined Inputs

By default aah framework resolve 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 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 use it my way?.

Besides the framework auto view resolve when use method HTML(data). 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.html layout.
  • Reply().HTMLlf(layout, filename, data) - layout and view filename is user input.

Since v0.6 if 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 value via middleware or in the controller.
  • Via Reply().HTML* methods as a aah.Data{ ... } param.

Framework provides access to aah.AppConfig(), Session, Flash PathParam, FormParam, and QueryParam from view template via template function.

Adding User-Defined View Engine into aah

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

In the upcoming release, I will try to provide pluggable view engine for amber, pongo2, and jade. So you use it selectively. Or you’re very welcome to contribute to aah framework.

Create your own view engine using view.Enginer interface.

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

Adding view engine into aah

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

Configuring your custom view engine into aah

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

view {
  engine = "enginename"
}

Values made available in ViewArgs by framework

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

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

  Need Help?

  •   Chat on Gitter
  •   Github Issues
  •   Ask on StackOverflow - tag aahframework