web

package
v0.0.0-...-f18cc39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2013 License: BSD-3-Clause, MIT Imports: 9 Imported by: 0

Documentation

Overview

The `babou` web application core

Provides library methods that are useful from many different layers in the `app` package.

If an appropriate solution exists in this package it should be preferred over a hand-rolled implementation. As they are designed to be fairly generic solutions that are fairly composable.

Index

Constants

View Source
const DEBUG_RELOAD = false

Variables

View Source
var Router *mux.Router = nil

A global router or middleware implementation that will service requests from the HTTP server and direct them to an appropriate controller

Functions

func BuildForm

func BuildForm(params []string, data string) string

Builds a form context and renders a sub-template inside of it.

Example:
  {{#FormFor (controller) (method) [enctype] (formId)} }
    {{ > app/views/users/register.template }}
  {{/FormFor}}

Where the register.template can now use any of the Form helpers.

func DisableDirectoryListing

func DisableDirectoryListing(h http.Handler) http.HandlerFunc

Returns a 404 error if a user asks `babou` for the contents of a directory. Useful for serving static files.

func EscapeString

func EscapeString(in string) string

func FileFor

func FileFor(params []string, data string) string

Generates an input for a file. The id defaults to the `name` property if it is omitted.

Example:
  {{#FileFor (name) [id]}}

  {{/FileFor}}

func LabelFor

func LabelFor(params []string, data string) string

Generates a label for a form field

Example:
  {{#LabelFor [fieldId]}}[display]{{/LabelFor}}
[fieldId] is the id="" attr of the form field.
[display] is the display text of the label.

func LinkTo

func LinkTo(params []string, data string) string

Returns an HTML link as a string suitable for insertion into an HTML template.

  Params: {{#UrlFor [controllerName] [routeParameters]...}}
	params are passed to the router to create a properly formed URL.
  Data: {{#UrlFor}} [data] {{/UrlFor}}
	data will be escaped and used as the link's display text.

func Render

func Render(controllerName, actionName string, viewData *ViewData) string

Renders the application template; yielding to app layout if requested.

func RenderIn

func RenderIn(templateName, controllerName, actionName string, viewData *ViewData) string

Renders the requested template inside a layout. This can override the default behavior to render inside the application layout.

func RenderTo

func RenderTo(templateName string, viewData *ViewData) string

Renders a mustache template from the views/ directory

func RenderWith

func RenderWith(templateName, controllerName, actionName string, filterHelpers ...interface{}) string

func TextFieldFor

func TextFieldFor(params []string, data string) string

Generates a text field suitable for rendering in an HTML form.

Example:
  {{#TextFieldFor [fieldId] [?fieldType]}}{{/TextFieldFor}}
[fieldId] is the field's HTML id="" attribute.
[?fieldType] is an optional parameter used as the HTML type="" attribute.

func UrlFor

func UrlFor(params []string, data string) string

Returns a URL as a string suitable for insertion into an HTML template.

Params: {{#UrlFor [controllerName] [routeParameters]...}}
  params are passed to the router to create a properly formed URL.

Types

type Action

type Action func() *Result

An action takes a map of request-parameters from the middleware or router and turns it into a servicable HTTP result.

type AfterPhaseContext

type AfterPhaseContext interface {
	ChainableContext
	AfterAttach(http.ResponseWriter, *http.Request) error
}

Contexts which provide a callback for the "AFTER-ATTACH" phase of route resolution.

type ChainableContext

type ChainableContext interface {
	NewInstance() ChainableContext // returns a clean instance that is safe for a single request/response

	// Resolve
	TestContext(Controller, []ChainableContext) error // Allows a context to test if a route is properly configured before any requests are serviced.

	// Attach
	ApplyContext(Controller, http.ResponseWriter, *http.Request, []ChainableContext) // Delegate down the chain until somebody answers the request.

	// After
	CloseContext()
}

Contexts which can be chained together ApplyContext will actually attach the context to a specific controller TestContext can be used to determine if a route supports a given context.

type Controller

type Controller interface {
	Dispatch(string, string) (Controller, Action)
	TestContext([]ChainableContext) error
}

A dispatcher returns a request-facing version of itself and action which is considered safe to infoke ONLY IF the context satisfies the dispatcher with no errors.

type MustacheRenderer

type MustacheRenderer struct {
	// contains filtered or unexported fields
}

func NewMustacheRenderer

func NewMustacheRenderer(viewsPath string) *MustacheRenderer

func (*MustacheRenderer) RenderWith

func (mu *MustacheRenderer) RenderWith(
	templateName,
	controllerName,
	actionName string,
	filterHelpers ...interface{}) string

type Param

type Param struct {
	All   map[string]string
	Form  map[string]string
	Files map[string][]*multipart.FileHeader
}

Includes parameters from URLEncoded POST and GET data. Also includes multipart form data if its available

func RetrieveAllParams

func RetrieveAllParams(request *http.Request) *Param

Retrieves GET and POST vars from an http Request

type RedirectPath

type RedirectPath struct {
	NamedRoute string //or:

	ControllerName string
	ActionName     string
}

Requests an HTTP redirect from the middleware or router.

type Renderer

type Renderer interface {
	RenderWith(string, string, string, ...interface{}) string
}

A renderer maps actions to executable views.

type Result

type Result struct {
	Body   []byte //HTTP Response Body
	Status int    //HTTP Status Code

	IsFile   bool //Indicates that the body should be served as a file.
	Filename string

	Redirect *RedirectPath
}

Represents an HTTP response from a `Controller` The middleware or router is responsible for using this result appopriately.

type ViewData

type ViewData struct {
	Yield func(params []string, data string) string
	//Flash   func() string
	Context interface{}
}

A context that will be passed to the underlying html template. Yield is a function that will be replaced by the renderer. It will call your requested template and automatically pass it the supplied `Context` argument.

type ViewableContext

type ViewableContext interface {
	ChainableContext
	GetViewHelpers() []interface{}
}

Contexts which have view helpers associated with them If they are passed to a RenderWith method their view helpers will be added.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL