node_template

package module
v0.0.0-...-4ec0e27 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2016 License: GPL-2.0 Imports: 6 Imported by: 0

README

node_template

HTML templating for go, with no mess!

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 );
}

GoDoc Build Status

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NodeTemplate

type NodeTemplate struct {
	*html.Node
}

func NodeTemplateFromFile

func NodeTemplateFromFile(filename string) (*NodeTemplate, error)

NodeTemplateFromFile gives you a shortcut to parse a template from a filename.

func Parse

func Parse(r io.Reader) (*NodeTemplate, error)

Parse HTML into a NodeTemplate

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

Jump to

Keyboard shortcuts

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