web

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package web provides HTML generation functions and HTTP session management.

Index

Constants

This section is empty.

Variables

View Source
var (
	// PL is fmt.Println() but is used only for debugging.
	PL = fmt.Println

	// VL is zr.VerboseLog() but is used only for debugging.
	VL = zr.VerboseLog
)
View Source
var ContextDebugFunc func(a ...interface{}) (n int, err error)

ContextDebugFunc specifies a function for testing or debugging all HTTP traffic received and sent by Context. It is called when a request is received, and when a reply is sent.

During normal program operation it should not be assigned to avoid serious security and performance issues.

To print to the console, assign fmt.Print to it, or use your own custom function if you need to log the output elsewhere

The return values are not used, just specified to match fmt.Print

During normal opration, remove the followig assignment ---> = fmt.Print

View Source
var MediaTypes = []struct {
	ext      string
	mimeType string
}{} //                                                                  MediaTypes

/* 507 elements not displayed */

MediaTypes holds an array of file extensions and corresponding Internet media types (MIME type i.e. Multipurpose Internet Mail Extensions)

Functions

func BaseReferer

func BaseReferer(req *http.Request) string

BaseReferer _ _

func CompactCSS

func CompactCSS(css []byte) []byte

CompactCSS removes all comments, empty lines and indentation from CSS bytes.

func FormatURL

func FormatURL(url string) string

FormatURL trims spaces, trailing '#', '/', and '?' from url. It also replaces backslashes '\' with forward slashes '/'. Note: this function does not perform escaping.

func HTML

func HTML(content ...interface{}) []byte

HTML tag is the root of an HTML document and the top-level container for all other HTML tags. Attributes: manifest, xmlns

func MediaType

func MediaType(name string) string

MediaType returns the appropriate Internet MIME type given a file extension, file name or full MIME type. (When given a file name, it checks the ending of the given file name)

func SetClass

func SetClass(add bool, input string, classes ...string) string

SetClass appends or removes the specified class(es) to the given string. 'add' specifies if the class should be added (or removed if false).

'input' is the existing class string.
'classes' is a list of class names to add or remove.
Returns the input string with the required classes added or removed.

func SetUseNthChild

func SetUseNthChild(val bool)

SetUseNthChild _ _

func UseNthChild

func UseNthChild() bool

UseNthChild _ _

Types

type Attribute

type Attribute struct {
	Name  string
	Value string

} //                                                                   Attribute

Attribute holds the name and value of a single HTML attribute.

func Attr

func Attr(name, val string) Attribute

Attr specifies any element's attribute.

func Charset

func Charset(locale string) Attribute

Charset is DEPRECATED. Use MetaCharset() instead.

func Class

func Class(classList ...string) Attribute

Class represents the 'class' attribute. You can specify multiple class strings in which case they will be delimited by a space. E.g. Class("currency", "sum") will become class="currency sum"

func Content

func Content(locale string) Attribute

Content attribute applies to <meta> tags.

func HREF

func HREF(href string) Attribute

HREF attribute applies to <a> tags.

func ID

func ID(locale string) Attribute

ID attribute applies to various tags.

func Lang

func Lang(locale string) Attribute

Lang attribute applies to <html> tags.

func Name

func Name(locale string) Attribute

Name attribute applies to various tags.

func OnClick

func OnClick(jsCall string) Attribute

OnClick attribute applies to various tags.

func OnLoad

func OnLoad(jsCall string) Attribute

OnLoad attribute applies to various tags.

func Type

func Type(locale string) Attribute

Type attribute.

type Buffer

type Buffer struct {
	// contains filtered or unexported fields

} //                                                                      Buffer

Buffer represents a buffer holding HTML output.

func A

func A(href string, content ...interface{}) *Buffer

A tag specifies a hyperlink, which links other web pages and locations in the current document. Attributes: charset coords download href hreflang media name rel rev shape target type

func Article

func Article(content ...interface{}) *Buffer

Article defines self-contained content. Attributes: Global, Event

func Body

func Body(content ...interface{}) *Buffer

Body tag: contains the visible content of a page, including text, images, hyperlinks, etc. Attributes: alink, background, bgcolor, link, text, vlink

func Br

func Br(attributes ...Attribute) *Buffer

Br tag inserts a line break. This tag has no closing tag and is not a container. Attributes: Global, Event

func COLUMNS

func COLUMNS(cols []string, class string, useNthChild bool) *Buffer

COLUMNS creates a group of <p> tags with class c1, c2, c3, etc. for every string passed in 'columns'. Used to create tabular listings using CSS and <p> tags, without the need to use HTML tables.

func CSS

func CSS(styles ...string) *Buffer

CSS links or embeds one or more CSS (Cascading Style Sheet) files or scripts.

To link to a CSS file, just specify the filename in 'styles', for example: CSS("global_style.css")

To embed a style locally, specify the style in styles, for example, CSS("body { font: normal 11pt Helvetica }")

Should be placed within the <head> element under <html>.

func Comment

func Comment(s string) *Buffer

Comment composes an HTML comment.

func Container

func Container(elementName string, content ...interface{}) *Buffer

Container composes an arbitrary HTML container tag.

func Div

func Div(content ...interface{}) *Buffer

Div tag defines an arbitrary division in the document. Attributes: align (left, right, center, justify)

func Element

func Element(elementName string, attributes ...Attribute) *Buffer

Element composes a HTML tag with optional attributes but no child tags.

func Embed

func Embed(html string) Buffer

Embed creates a Buffer from a HTML string.

func Form

func Form(content ...interface{}) *Buffer

Form tag.

func H1

func H1(content ...interface{}) *Buffer

H1 tag specifies headings (level 1) Attributes: align (left, center, right, justify)

func H2

func H2(content ...interface{}) *Buffer

H2 tag specifies headings (level 2) Attributes: align (left, center, right, justify)

func H3

func H3(content ...interface{}) *Buffer

H3 tag specifies headings (level 3) Attributes: align (left, center, right, justify)

func H4

func H4(content ...interface{}) *Buffer

H4 tag specifies headings (level 4) Attributes: align (left, center, right, justify)

func H5

func H5(content ...interface{}) *Buffer

H5 tag specifies headings (level 5) Attributes: align (left, center, right, justify)

func H6

func H6(content ...interface{}) *Buffer

H6 tag specifies headings (level 6) Attributes: align (left, center, right, justify)

func Head(content ...interface{}) *Buffer

Head element is the container for a page's head elements, including the title, scripts and metadata. Allowed child elements: <base> <link> <meta> <noscript> <script> <style> <title> (required)

func Header(content ...interface{}) *Buffer

Header tag contains the headings of a section or navigational links.

func Hr

func Hr(attributes ...Attribute) *Buffer

Hr tag inserts a thematic break (horizontal rule pre-HTML5). This tag has no closing tag and is not a container. Attributes: Global, Event

func IFrame

func IFrame(content ...interface{}) *Buffer

IFrame represents an <iframe> tag, i.e. inline frame.

func Img

func Img(content ...interface{}) *Buffer

Img inserts an image element. For example Img("folder/filename.png") will become <img src="folder/filename.png"> in the output HTML

func Input

func Input(attributes ...Attribute) *Buffer

Input tag represents an element for user input. This tag has no closing tag and is not a container.

func JOIN

func JOIN(content ...*Buffer) *Buffer

JOIN concatenates the content of multiple buffers into a single buffer. It can be useful to merge together several HTML elements, with one element listed after the other.

func JS

func JS(scripts ...string) *Buffer

JS links JavaScript (.js) script files or embeds JS code snippets.

func Label

func Label(content ...interface{}) *Buffer

Label tag represents a label element.

func Li

func Li(content ...interface{}) *Buffer

Li tag defines a list item in unordered (ul), ordered (ol) or menu lists (menu). Attributes: global attributes and value.

func Meta

func Meta(attributes ...Attribute) *Buffer

Meta tag is DEPRECATED. Use MetaCharset() and/or MetaViewport() instead.

func MetaCharset

func MetaCharset(locale string) *Buffer

MetaCharset attribute applies to <meta> tags.

func MetaViewport

func MetaViewport() *Buffer

MetaViewport meta tag: enables a web page to have a responsive layout.

func NAV(href string, content ...interface{}) *Buffer

NAV helper tag specifies a hyperlink, which links other web pages and locations in the current document. It is similar to the 'A' tag, but uses zr.go() in JS to save the current page reference. Attributes: charset coords download href hreflang

media name rel rev shape target type
func Nav(content ...interface{}) *Buffer

Nav tag defines a section with navigation links.

func NewBuffer

func NewBuffer(size int) Buffer

NewBuffer creates and initializes a new Buffer of the specified size. It is intended to prepare a buffer for writing HTML markup. In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.

func P

func P(content ...interface{}) *Buffer

P tag defines a paragraph. Attribute: align, global attributes

func Span

func Span(content ...interface{}) *Buffer

Span tag groups inline elements.

func TEXT

func TEXT(texts ...string) *Buffer

TEXT helper tag is a non-standard tag that helps inject literal strings into HTML content.

func Title

func Title(content ...interface{}) *Buffer

Title tag specifies the title or name of the document. It is required to be placed within the <head> element.

func Ul

func Ul(content ...interface{}) *Buffer

Ul defines an unordered list. (A list in which the ordering of items is not important). The list can contain zero or more <li> elements.

func (*Buffer) Bytes

func (ob *Buffer) Bytes() []byte

Bytes returns the contents of the buffer as an array of bytes.

func (*Buffer) Len

func (ob *Buffer) Len() int

Len returns the length of the buffer.

func (*Buffer) Reset

func (ob *Buffer) Reset()

Reset makes the buffer empty, but retains allocated storage for future reuse.

func (*Buffer) String

func (ob *Buffer) String() string

String returns the contents of the buffer as a string and implements the fmt.Stringer interface.

func (*Buffer) Write

func (ob *Buffer) Write(html ...*Buffer)

Write writes 'html' to the buffer.

func (*Buffer) WriteBytes

func (ob *Buffer) WriteBytes(arrays ...[]byte)

WriteBytes writes a string to the buffer.

func (*Buffer) WriteString

func (ob *Buffer) WriteString(strings ...string)

WriteString writes a string to the buffer.

type Context

type Context struct {
	Session *Session
	// contains filtered or unexported fields

} //                                                                     Context

Context structure wraps a HTTP request and attaches a Session object to allow state to be maintained between requests.

func NewContext

func NewContext(w http.ResponseWriter, req *http.Request, sess *Sessions,
) Context

NewContext creates a new Context object by wrapping a response writer (w) and request (req) from ListenAndServe() and attaching a new or existing session using 'sess'. Sessions uses cookies to determine if the request is a new session, or a previously started session.

func (*Context) BaseReferer

func (ctx *Context) BaseReferer() string

BaseReferer property returns the base referer path of the current request. I.e. a path with '/', '\', '#' and numbers stripped from the end.

func (*Context) DebugString

func (ctx *Context) DebugString() string

DebugString _ _

func (*Context) HREF

func (ctx *Context) HREF() string

HREF property returns the URL path of the current request. This property does not return the query parameters.

func (*Context) Method

func (ctx *Context) Method() string

Method returns the request's HTTP method ('GET', 'POST', 'PUT', etc)

func (*Context) PostData

func (ctx *Context) PostData() []byte

PostData property returns the POSTDATA of the current request.

func (*Context) Redirect

func (ctx *Context) Redirect(url string)

Redirect redirects the client to another url using HTTP redirect code 302 (temporary redirect).

func (*Context) Referer

func (ctx *Context) Referer() string

Referer property returns the referer path of the current request.

func (*Context) Reply

func (ctx *Context) Reply(data []byte, mediaType string)

Reply method sends the reply to a request. Specify 'mediaType' to set 'Content-Type' in the HTTP header. The media type can be a file extension, such as 'pdf' or 'png' in which case it gets converted to a proper MIME type, e.g. 'application/pdf' or 'image/png' Use the file extension value, e.g. "pdf"

func (*Context) ResetPostData

func (ctx *Context) ResetPostData()

ResetPostData _ _

func (*Context) URI

func (ctx *Context) URI() string

URI property returns the full URI path of the current request. This includes the HREF() part and any query parameters.

type Session

type Session struct {
	// contains filtered or unexported fields

} //                                                                     Session

Session _ _

func (*Session) GetSetting

func (ob *Session) GetSetting(name string) string

GetSetting _ _

func (*Session) ID

func (ob *Session) ID() string

ID _ _

func (*Session) SetSetting

func (ob *Session) SetSetting(name string, value interface{})

SetSetting _ _

type Sessions

type Sessions struct {
	// contains filtered or unexported fields

} //                                                                    Sessions

Sessions _ _

func (*Sessions) GetByCookie

func (ob *Sessions) GetByCookie(
	w http.ResponseWriter,
	req *http.Request,
) *Session

GetByCookie _ _

Jump to

Keyboard shortcuts

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