Documentation ¶
Overview ¶
node_template extends go.net/html & cascadia to make it as easy as possible to process pure HTML templates. Using jquery like search syntax you can find the elements you wish to populate by looking for the attributes you require. This approach allows HTML templates which are pure HTML.
Example:
func main_page( w http.ResponseWriter, r * http.Request ){ template, err := node_template.NodeTemplateFromFile( "temp/main.html"); if( err != nil ){ log.Println( err ); return } title_el, err := template.FindFirst(`#title`); if( title_el != nil){ title_el.ReplaceContentText("Billy & Jane"); } names, err := template.Find(`.name`); if( names != nil ){ names.ReplaceContentText("tom"); } var people list.List people.PushBack("Tom"); people.PushBack("Richard"); people.PushBack("Harry"); person, _ := template.FindFirst(".person"); if( person != nil ){ person.RepeatNode( &people, func( node * node_template.NodeTemplate, e * list.Element ){ node.ReplaceContentText( e.Value.(string) ); }); } template.Render( w ); }
Index ¶
- type NodeTemplate
- func (t *NodeTemplate) Copy() *NodeTemplate
- func (t *NodeTemplate) Find(pat string) (*NodeTemplateSet, error)
- func (t *NodeTemplate) FindFirst(pat string) (*NodeTemplate, error)
- func (t *NodeTemplate) Render(w io.Writer)
- func (t *NodeTemplate) RepeatNode(l *list.List, f func(*NodeTemplate, *list.Element))
- func (t *NodeTemplate) ReplaceContent(new_node *NodeTemplate)
- func (t *NodeTemplate) ReplaceContentText(content string)
- type NodeTemplateSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NodeTemplate ¶
func NodeTemplateFromFile ¶
func NodeTemplateFromFile(filename string) (*NodeTemplate, error)
NodeTemplateFromFile gives you a shortcut to parse a template from a filename.
func (*NodeTemplate) Copy ¶
func (t *NodeTemplate) Copy() *NodeTemplate
func (*NodeTemplate) Find ¶
func (t *NodeTemplate) Find(pat string) (*NodeTemplateSet, error)
Final all nodes which match the jquery-style pattern
func (*NodeTemplate) FindFirst ¶
func (t *NodeTemplate) FindFirst(pat string) (*NodeTemplate, error)
Find the first node that matces the jquery-style pattern (see code.google.com/p/cascadia docs for details) and returns a NodeTemplate to represent that.
func (*NodeTemplate) Render ¶
func (t *NodeTemplate) Render(w io.Writer)
Render the HTML to an io.Writer
func (*NodeTemplate) RepeatNode ¶
func (t *NodeTemplate) RepeatNode(l *list.List, f func(*NodeTemplate, *list.Element))
func (*NodeTemplate) ReplaceContent ¶
func (t *NodeTemplate) ReplaceContent(new_node *NodeTemplate)
Replace the content of a node with new_node
func (*NodeTemplate) ReplaceContentText ¶
func (t *NodeTemplate) ReplaceContentText(content string)
Replace the content of the node a string. This is html safe, so &< etc are fine in the string.
type NodeTemplateSet ¶
type NodeTemplateSet []*NodeTemplate
func (NodeTemplateSet) For ¶
func (nts NodeTemplateSet) For(f func(*NodeTemplate))
For each node execute this function
func (NodeTemplateSet) Get ¶
func (nts NodeTemplateSet) Get(index int) *NodeTemplate
func (NodeTemplateSet) Len ¶
func (nts NodeTemplateSet) Len() int
func (NodeTemplateSet) ReplaceContent ¶
func (nts NodeTemplateSet) ReplaceContent(content *NodeTemplate)
func (NodeTemplateSet) ReplaceContentText ¶
func (nts NodeTemplateSet) ReplaceContentText(content string)
Replace content of each node with a string