bham

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: May 2, 2014 License: BSD-2-Clause, BSD-2-Clause Imports: 10 Imported by: 0

README

bham - Blocky Hypertext Abstraction Markup

bham is a similar language to something like haml, but it implements a subset of what haml does in order to keep the language neat and tidy. I think of it as the haml syntax mixed with the command language of Go's builtin template library, not that suprising since this library compiles into ParseTrees for the builtin template libraries. It can interoperate with existing go templates, and benefits from the html/template's escaping functionality.


Documentation

Full syntax documentation is on Godoc.org

Implemented Features

  • Plaintext passthrough
  • %tag expansion
  • If/Else Statements
  • Tag Nesting
  • Range statements for collection data structures
  • = ... for Lines with pipelines on them
  • Parentheses for HTML-like attributes
  • Class and ID shorthand
  • With statement for limited visibility variables
  • Template Variables
  • {{ }} For embedded pipeline output

Features being worked on

  • More Documentation

Unlikely To Be Implemented Features

  • Curly branch hashrocket syntax for attributes
  • Multiple line prefixes for different visibility/escaping

Documentation

Overview

bham (block-based html abstraction markup)

Is a restricted subset of haml that brings the usefulness of haml nesting to multitemplate. It parses files in the bham syntax to text/template/parse Tree's, which are generated by converting text, tags and simple template code directly to parse.Nodes, while handing off more complex lines individually to Go's standard template library and extracting the resulting Nodes to insert into it's parse Tree's.

Plain Text

Most web templates are mostly html content, or just plain text content, as opposed to executable code. If bham doesn't think a line can be executed, it will just output the raw version to html.

%first
  %second
    %final
      <a href="#">Showdown</a>

The following code is how it would be output, with the a tag passed through unescaped.

<first>
  <second>
    <final>
      <a href="#">Showdown</a>
    </final>
  </second>
</first>

You should notice that placing a % before a word will turn it into a tag, while at the same time the a tag was sent though as well. For those of us wondering about automatic escaping, putting plain html into a bham template will not escape any characters.

Attributes

The normal attribute syntax is the html style attributes form used in haml, which looks like

%script(type="text/javascript" src="/js/{{.script}}.js")

Classes and ID's

You can use # and . to create id and classes for tags. Multiple class names are each added to the class attribute, while multiple id parts will be joined with underscores in the compiled form.

%span#tesla(id="tower")
  %img.watt.ohm(src="img1.png")

would be compiled into

<span id="tesla_tower">
  <img class="watt ohm" src="img1.png"></img>
</span>

If you do not specify the tag, it will be assumed to be a div. The following two lines are identical after compilation.

%div.classy#restaurant
.classy#restaurant

Doctypes

Doctypes are placed near the beginning of files and start with three exclamation marks. There is a list of default Doctypes in the Doctypes variable. You can add or modify your own doctypes. Here's an example:

!!! Strict

would be compiled into

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Available doctypes are are available in the Doctypes variable, with the HTML5 doctype as the default.

Filters

Sometimes you need to embed a bit of javascript or css into your html template, in this case you can use a filter to automatically turn it into a different format. For example

:javascript
  $(".name").html("Hello World");

would compile into

<script type="text/javascipt">
  $(".name").html("Hello World");
</script>

Currently you cannot embed values into lines that are embedded into filters, this will be fixed.

Template logic statements

Logic statements are identical to the statements available in the standard template library, along with output statements. Simpler statements like a field or variable are parsed by bham, while more complex statements are broken up and parsed individually with the text/template parser. The main difference from the stdlib template library is that logic statements can use = signs to signify that the line is executable, instead of {{ }}, though the curly's are supported as a second means of logic embedding. Bham will also autoclose if's, range's, with's, and else statements found in the templates. Because Go's template library differentiates between returning and non-returning lines, bham does not have the = and - lines found in haml.

Examples

%a(href="{{ url "Home" }}") Home Page

= .CurrentUser.Name

Welcome to {{ .Location.Name}}.

= if has_permission .CurrentUser "read_emails"
  %email_list
    ...

Examples

This first example is a layout with a doctype, multiple yields and blocks, tags with dynamic attributes, if statements, and divs with id's.

!!!
%html
  %head
    = yield "head"
  %body
    = block "header"
      #header
        = if .user
          #options
            Connected as {{.user.Username}} |
            %a(href="{{ url "Hotels.Index" }}") Search
            |
            %a(href="{{ url "Hotels.Settings" }}") Settings
            |
            %a(href="{{ url "Application.Logout" }}") Logout
    = end_block
    = yield "content"
    = block "footer"
      #footer
        Created with the
        %a(href="http://github.com/revel/revel") Revel framework
        and really inspirated from the booking sample application provided by
        %a(href="http://www.playframework.org") play framework,
        which was really inspired by the booking sample application provided by the
        %a(href="http://seamframework.org/") seam framework.
    = end_block

Index

Constants

This section is empty.

Variables

View Source
var (
	// Strict determines whether only tabs will be considered
	// as indentation operators (Strict == true) or whether
	// two spaces can be counted as an indentation operator
	// (Strict == false), this is included for haml
	// semi-comapibility
	Strict bool

	// To add multiple id declarations, the outputter puts them together
	// with a join string, by default this is an underscore
	IdJoin = "_"

	// Like the template library, you need to be able to set code delimeters
	LeftDelim  = "{{"
	RightDelim = "}}"
)
View Source
var Doctypes = map[string]string{
	"":             `<!DOCTYPE html>`,
	"Transitional": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`,
	"Strict":       `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">`,
	"Frameset":     `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">`,
	"5":            `<!DOCTYPE html>`,
	"1.1":          `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`,
	"Basic":        `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">`,
	"Mobile":       `<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">`,
	"RDFa":         `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`,
}
View Source
var Filters = []FilterHandler{
	FilterHandler{
		Trigger: ":javascript",
		Open:    `<script type="text/javascript">`,
		Close:   "</script>",
		Handler: Transformer(func(s string) string { return s }),
	},
	FilterHandler{
		Trigger: ":css",
		Open:    `<style>`,
		Close:   "</style>",
		Handler: Transformer(func(s string) string { return s }),
	},
}

Functions

func Parse

func Parse(name, text string, funcs template.FuncMap) (map[string]*parse.Tree, error)

parse will return a parse tree containing a single

Types

type FilterHandler

type FilterHandler struct {
	Trigger     string
	Open, Close string
	Handler     Transformer
}

type Transformer

type Transformer func(string) string

Jump to

Keyboard shortcuts

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