mithril

package module
v0.0.0-...-2721f3f Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2016 License: MIT Imports: 5 Imported by: 0

README

go-mithril

Build Status Coverage Status GoDoc

Mithril (Isomorphic) server-side render for Go

What is Mithril(.JS)?

Mithril.js is a small and fast MVC JavaScript framework. It encourages an architecture similar to Angular.js, and uses a virtual DOM like React.js, all while avoiding the need for libraries like jQuery. Mithril's small size and API makes it ideal for embedded JavaScript widgets and user interfaces that have high performance requirements.

Github: https://github.com/lhorie/mithril.js

What is Go-Mithril?

Go-Mithril is a way to create reusable Mithril UI components for both server-side and client-side.

Isomorphic means?

i·so·mor·phic: “corresponding or similar in form and relations”

Usage

To create an username textbox:

M("input.username[type='text']") // Returns <input class="username" type="text" />

Create a textbox with id, class and type

M("input", []Attribute{
	ID("username"),
	Class("input", "input-username"),
	NewStringAttr("type", "text"),
}) // Returns <input id="username" class="input input-username" type="text" />

Create a div with content

M("div", "Hello World") // Returns <div>Hello World</div>

Create a list with component

type ListComponent struct {
	*BaseComponent
}

func (component *ListComponent) Controller() {
	component.Set("list_items", []string{"object 1", "object 2", "object 3"})
}

func (component *ListComponent) View() interface{} {
	return M("ul", func() []interface{} {
        var elements []interface{}
        for _, i := range component.Get("list_items").([]string) {
            elements = append(elements, M("li", i))
        }
        return elements
    }()),
}

func main() {
  mithril.Render(&ListComponent{ExtendComponent()}) // Returns <ul><li>object 1</li><li>object 2</li><li>object 3</li></ul>
}

Shortcut

To use m instead of M

import (
	"github.com/samuelngs/go-mithril"
)
var (
	m      = mithril.M
	render = mithril.Render
	id     = mithril.ID
	class  = mithril.Class
)
func main() {
 	render(
 		m("div", "Hello World")
 	)
}

Documentation

go doc format documentation for this project can be viewed online without installing the package by using the GoDoc page at: https://godoc.org/github.com/samuelngs/go-mithril

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

License

This project is distributed under the MIT license found in the LICENSE file.

The MIT License (MIT)

Copyright (c) 2015 Samuel

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

View Source
const (
	// Parser is the tags regex parser
	Parser = `(?:(^|#|\.)([^#\.\[\]]+))|(\[.+?\])`
	// AttrParser is the attribute regex parser
	AttrParser = `\[(.+?)(?:=("|'|)(.*?)("|'|))?\]`
)

Variables

View Source
var (
	// SelfClosingTagToken is a list of self closing elements tag
	SelfClosingTagToken = []string{"area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr", "!doctype"}
)

Functions

func Render

func Render(elements ...interface{}) string

Render returns HTML string

Types

type Attribute

type Attribute interface {
	String() string
	Key(...string) string
	Val(...string) string
	Namespace(...string) string
	Add(...string) string
	Remove(...string) string
}

Attribute is an attribute namespace-key-value triple.

func Attr

func Attr(name string, vals ...string) Attribute

Attr returns new attribute

func Class

func Class(vals ...string) Attribute

Class returns new class attribute

func ID

func ID(vals ...string) Attribute

ID returns new class attribute

func Style

func Style(vals ...string) Attribute

Style returns new class attribute

type BaseComponent

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

BaseComponent contains a controller and a view properties.

func ExtendComponent

func ExtendComponent() *BaseComponent

ExtendComponent creates a basic component view

func (*BaseComponent) Controller

func (component *BaseComponent) Controller()

Controller function creates map for View

func (*BaseComponent) Get

func (component *BaseComponent) Get(key string) interface{}

Get returns controller value

func (*BaseComponent) Set

func (component *BaseComponent) Set(key string, val interface{})

Set value to controller scope

func (*BaseComponent) View

func (component *BaseComponent) View() interface{}

View function creates VirtualElement or string, int, bool

type Component

type Component interface {
	Controller()
	View() interface{}
	Get(string) interface{}
	Set(string, interface{})
}

Component for Mithril

type ListAttribute

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

ListAttribute is an attribute namespace-key-value triple.

func NewListAttr

func NewListAttr(args ...string) *ListAttribute

NewListAttr Creates a ListAttribute object

func NewNSListAttr

func NewNSListAttr(args ...string) *ListAttribute

NewNSListAttr creates a ListAttribute object

func (*ListAttribute) Add

func (attr *ListAttribute) Add(vals ...string) string

Add for adding value(s) to attr store

func (*ListAttribute) Contains

func (attr *ListAttribute) Contains(v string) bool

Contains return the result of if attr contains a specific value

func (*ListAttribute) Key

func (attr *ListAttribute) Key(keys ...string) string

Key for sets or returns key string

func (*ListAttribute) Namespace

func (attr *ListAttribute) Namespace(namespaces ...string) string

Namespace for sets and returns namespace value

func (*ListAttribute) Remove

func (attr *ListAttribute) Remove(vals ...string) string

Remove for removing value(s) from attr value variable

func (*ListAttribute) Separator

func (attr *ListAttribute) Separator(s string) Attribute

Separator for change specific text separator when join string array

func (*ListAttribute) String

func (attr *ListAttribute) String() string

String returns ListAttribute in string format

func (*ListAttribute) Val

func (attr *ListAttribute) Val(vals ...string) string

Val for sets or returns attr value

type StringAttribute

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

StringAttribute is an attribute namespace-key-value triple.

func NewStringAttr

func NewStringAttr(args ...string) *StringAttribute

NewStringAttr Creates a StringAttribute object

func (*StringAttribute) Add

func (attr *StringAttribute) Add(vals ...string) string

Add for adding value(s) to attr store (Do nothing for StringAttribute)

func (*StringAttribute) Key

func (attr *StringAttribute) Key(keys ...string) string

Key for sets or returns key string

func (*StringAttribute) Namespace

func (attr *StringAttribute) Namespace(namespaces ...string) string

Namespace for sets and returns namespace value

func (*StringAttribute) Remove

func (attr *StringAttribute) Remove(vals ...string) string

Remove for removing value(s) from attr value variable (Do nothing for StringAttribute)

func (*StringAttribute) Separator

func (attr *StringAttribute) Separator(s string) *StringAttribute

Separator for change specific text separator when join string array (Do nothing for StringAttribute)

func (*StringAttribute) String

func (attr *StringAttribute) String() string

String returns StringAttribute in string format

func (*StringAttribute) Val

func (attr *StringAttribute) Val(vals ...string) string

Val for sets or returns attr value

type TrustElement

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

TrustElement flags a string as trusted HTML.

func Trust

func Trust(str string) *TrustElement

Trust allows insert HTML for render arbitrary, potentially invalid markup, as well as run arbitrary Javascript, and therefore the developer is responsible for either

type VirtualElement

type VirtualElement struct {
	Tag      string
	Attrs    []Attribute
	Children interface{}
}

VirtualElement is an object which can convert to real DOM element

func M

func M(selector string, opts ...interface{}) *VirtualElement

M returns VirtualElement

func (*VirtualElement) Attr

func (el *VirtualElement) Attr(key string, val ...string) string

Attr returns or sets the attribute

func (*VirtualElement) HasAttr

func (el *VirtualElement) HasAttr(key string) bool

HasAttr returns true if VirtualElement has this attribute in attributes array

func (*VirtualElement) IsSelfClosingTag

func (el *VirtualElement) IsSelfClosingTag() bool

IsSelfClosingTag returns true if Tag is a self closing tag element

func (*VirtualElement) String

func (el *VirtualElement) String() string

String returns the html string of the element

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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