Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Jun 27, 2015
1 parent 0de809f commit 17c9d35
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 46 deletions.
143 changes: 99 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
[![Mustache](http://pkg.julialang.org/badges/Mustache_release.svg)](http://pkg.julialang.org/?pkg=Mustache&ver=release)
[![Mustache](http://pkg.julialang.org/badges/Mustache_nightly.svg)](http://pkg.julialang.org/?pkg=Mustache&ver=nightly)
 
Linux: [![Build Status](https://travis-ci.org/jverzani/Mustache.jl.svg?branch=master)](https://travis-ci.org/jverzani/Mustache.jl)
 
Windows: [![Build Status](https://ci.appveyor.com/api/projects/status/github/jverzani/Mustache.jl?branch=master&svg=true)](https://ci.appveyor.com/project/tkelman/example-jl/branch/master)

# Mustache

[![Build Status](https://travis-ci.org/jverzani/Mustache.jl.svg?branch=master)](https://travis-ci.org/jverzani/Mustache.jl)
[![Coverage Status](https://coveralls.io/repos/jverzani/Mustache.jl/badge.png)](https://coveralls.io/r/jverzani/Mustache.jl)
[![Mustache](http://pkg.julialang.org/badges/Mustache_release.svg)](http://pkg.julialang.org/?pkg=Mustache&ver=release)

[Mustache](http://mustache.github.io/) is

Expand All @@ -12,77 +16,126 @@

This package ports over most of the [mustache.js](https://github.com/janl/mustache.js) implementation for use in [Julia](http://julialang.org). All credit should go there. All bugs are my own.

Some basic examples with `julia`:
## Examples

```julia
using Mustache
Following the main [documentation](http://mustache.github.io/mustache.5.html) for `Mustache.js` we have a "typical Mustache template" defined by:

tpl = mt"the position is {{x}} and tail is {{y}}"

## a dict
render(tpl, {"x"=>1, "y"=>1})
```
using Mustache
Yields
tpl = mt"""
Hello {{name}}
You have just won {{value}} dollars!
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
"""
The values with braces (mustaches on their side) are looked up in a view, such as a dictionary or module. For example,
```julia
the position is 1 and tail is 2
```
d = Dict()
d["name"] = "Chris"
d["value"] = 10000
d["taxed_value"] = 10000 - (10000 * 0.4)
d["in_ca"] = true

The first argument of `render` can be an `IO` instance.
If it is not given, then `sprint` is used to provide one.
render(tpl, d)
```
The non-standard string literal `mt`, used above to make the `tpl`
object, is optional. If used, then the parsing is done at compile time
and should be faster when used in a loop, say. A triple-quoted form is also available.
Yielding
Similarly, we can use a module as a view such as `Main`:
```
Hello Chris
You have just won 10000 dollars!

```julia
x = 1; y = "two"
render(tpl, Main)
Well, 6000.0 dollars, after taxes.
```
gives
The `render` function pieces things together. Like `print`, the first
argument is for an option `IO` instance. In the above example, where
one is not provided, the `sprint` function is employed.
```julia
"the position is 1 and tail is two"
```
Or, with a temporary module:
The second argument is a either a string or a mustache template. As
seen, templates can be made through the `mt` no-standard string
literal. The advantage of using `mt`, is the template will be
processed at compile time and its reuse will be faster.
The templates use tags comprised of matching mustaches (`{}`), either two or three, to
indicate a value to be substituted for.
The third argument is for a view to provide values to substitute into
the template. The above example used a dictionary. A Module may also
be used, such as `Main`:
```julia
module TMP
x = 1; y = "two"
end
render("{{x}} and {{y}}", TMP) |> println
```
name, value, taxed_value, in_ca = "Christine", 10000, 10000 - (10000 * 0.4), false
render(tpl, Main) |> print
```
If the variables in the templates are symbols, then keyword arguments can be used.
Which yields:
```
tpl = mt"{{{:x}}} and {{{:y}}}."
render(tpl, x="Salt", y="pepper")
Hello Christine
You have just won 10000 dollars!
```
One can use Composite Kinds. This may make writing `show` methods easier:
Further, keyword
arguments can be used when the variables in the templates are symbols:
```
goes_together = mt"{{{:x}}} and {{{:y}}}."
render(goes_together, x="Salt", y="pepper")
render(goes_together, x="Bread", y="butter")
```
As well, one can use Composite Kinds. This may make writing `show` methods easier:
```julia
```
using Distributions
tpl = "Beta distribution with alpha={{alpha}}, beta={{beta}}"
render(tpl, Beta(1, 2))
```
gives
```julia
```
"Beta distribution with alpha=1.0, beta=2.0"
```
One can iterate over data frames. Here is a template for making a web page:
### Variables
Tags representing variables have the form `{{varname}}`, `{{:symbol}}`, or their triple-braced versions. (The triple brace prevents HTML substitution for entities such as `<`.)
### Sections
In the main example, the template included:
```
{{#in_ca}}
Well, {{taxed_value}} dollars, after taxes.
{{/in_ca}}
```
Tags beginning with `#varname` and closed with `/varname` create section. The part between them is used only if the variable is defined. Related, if the tag begins with `^varname` and ends with `/varname` the text between these tags is included only if the variable is *not* defined.
```julia
### Iteration
If the section variable binds to an iterable collection, then the text
in the section is repeated for each item in the collection.
This is use for collections of named objects, such as DataFrames
(where the collection is comprised of rows) or arrays of
dictionaries.
For data frames the variable names are specified as
symbols or strings. Here is a template for making a web page:
```
tpl = """
<html>
<head>
Expand All @@ -92,15 +145,15 @@ tpl = """
<table>
<tr><th>name</th><th>summary</th></tr>
{{#d}}
<tr><td>{{names}}</td><td>{{summs}}</td></tr>
<tr><td>{{:names}}</td><td>{{summs}}</td></tr>
{{/d}}
</body>
</html>
"""
```
This can be used to generate a web page for `whos`-like values:
```julia
```
_names = Array(String, 0)
_summaries = Array(String, 0)
m = Main
Expand All @@ -122,7 +175,7 @@ print(out)
This can be compared to using an array of `Dict`s, convenient if you have data by the row:
```julia
```
A = [{"a" => "eh", "b" => "bee"},
{"a" => "ah", "b" => "buh"}]
tpl = mt"{{#A}}Pronounce a as {{a}} and b as {{b}}. {{/A}}"
Expand All @@ -131,7 +184,7 @@ render(tpl, {"A" => A}) |> print
yielding
```julia
```
Pronounce a as eh and b as bee. Pronounce a as ah and b as buh.
```
Expand All @@ -158,9 +211,11 @@ render(tpl, ["df"=>df, "caption"=>caption, "label" => label])
end
```
### Differences from Mustache.js
This project deviates from that of http://mustache.github.com in a few significant ways:
* The partials tag (the `>` tag) is not implemented.
* The tags are only demarked with `{{` and `}}`.
* The tags are only demarcated with mustaches, this is not customizable
* Julian structures are used, not JavaScript objects. As illustrated,
one can use Dicts, Modules, DataFrames
8 changes: 6 additions & 2 deletions src/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ end

Requires.@require DataFrames begin
function lookup_in_view(view::DataFrames.DataFrame, key)
if haskey(view, symbol(key))
return view[1, symbol(key)] ## first element only
if ismatch(r":", key)
key = key[2:end]
end
key = symbol(key)
if haskey(view, key)
return view[1, key] ## first element only
else
return nothing
end
Expand Down

0 comments on commit 17c9d35

Please sign in to comment.