Skip to content

Commit

Permalink
use kwargs is named tuple to avoid Dict conversion; clean up README (#74
Browse files Browse the repository at this point in the history
)

* use kwargs is named tuple to avoid Dict conversion; clean up README

* fix error in render_from_file
  • Loading branch information
jverzani authored Sep 23, 2018
1 parent 89d4196 commit a85e63d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
32 changes: 15 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Mustache

[![Mustache](http://pkg.julialang.org/badges/Mustache_0.7.svg)](http://pkg.julialang.org/?pkg=Mustache)

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/jverzani/mustache-jl)

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

... a logic-less template syntax. It can be used for HTML,
config files, source code - anything. It works by expanding tags in a
Expand Down Expand Up @@ -82,16 +80,17 @@ You have just won 10000 dollars!
```


Further, keyword
arguments can be used when the variables in the templates are symbols:
Further, keyword arguments can be used when the variables in the
templates are symbols:

```julia
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:
Similarly, a named tuple may be used as a view. As well, one can use
Composite Kinds. This may make writing `show` methods easier:

```julia
using Distributions
Expand Down Expand Up @@ -151,7 +150,7 @@ given by the item.

This is useful for collections of named objects, such as DataFrames
(where the collection is comprised of rows) or arrays of
dictionaries.
dictionaries. For `Tables.jl` objects the rows are iterated over.

For data frames, the variable names are specified as
symbols or strings. Here is a template for making a web page:
Expand Down Expand Up @@ -231,7 +230,8 @@ render(tpl, DF=df)
end
```

(A string is used above -- and not a `mt` macro -- so that string interpolation can happen.)
(A string is used above -- and not a `mt` macro -- so that string
interpolation can happen.)

### Iterating over vectors

Expand Down Expand Up @@ -282,17 +282,18 @@ Partials are used to include partial templates into a template.

Partials begin with a greater than sign, like `{{> box.tpl }}`. In this example, the file `box.tpl` is opened and inserted into the template, then populated. A full path may be specified.

They also inherit the calling context.
They also inherit the calling context.

In this way you may want to think of partials as includes, imports,
template expansion, nested templates, or subtemplates, even though
those aren't literally the case here.

The partial specified by `{{< box.tpl }}` is not parsed, rather included as is into the file. This can be much faster.
The partial specified by `{{< box.tpl }}` is not parsed, rather included as is into the file. This can be faster.

## Alternatives

`Julia` provides some alternatives to this package which are better suited for many jobs:
`Julia` provides some alternatives to this package which are better
suited for many jobs:

* For simple substitution inside a string there is string
[interpolation](https://docs.julialang.org/en/latest/manual/strings/).
Expand All @@ -303,7 +304,7 @@ The partial specified by `{{< box.tpl }}` is not parsed, rather included as is i

* For formatting numbers and text, the
[Formatting.jl](https://github.com/JuliaLang/Formatting.jl) package,
the [Format](https://github.com/JuliaString/Format.jl) package, the
the [Format](https://github.com/JuliaString/Format.jl) package, and the
[StringLiterals](https://github.com/JuliaString/StringLiterals.jl)
package are available.

Expand All @@ -323,17 +324,14 @@ function is passed the unvevaluated section:

```
template = "<{{#lambda}}{{x}}{{/lambda}}>"
data = Dict("x" => "Error!", "lambda" => (txt) -> txt == "{{x}}" ? "yes" : "no")
data = Dict("x" => "Error!", "lambda" => (txt) -> txt == "{{x}}" ? "yes" : "no")
Mustache.render(template, data) ## "<yes>", as txt == "{{x}}"
```

The tag "|" is similar to the section tag "#", but will receive the *evaluated* section:

```
template = "<{{|lambda}}{{x}}{{/lambda}}>"
data = Dict("x" => "Error!", "lambda" => (txt) -> txt == "{{x}}" ? "yes" : "no")
data = Dict("x" => "Error!", "lambda" => (txt) -> txt == "{{x}}" ? "yes" : "no")
Mustache.render(template, data) ## "<no>", as "Error!" != "{{x}}"
```



13 changes: 5 additions & 8 deletions src/Mustache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ Arguments
* `view`: A view provides a context to look up unresolved symbols
demarcated by mustache braces. A view may be specified by a
dictionary, a module, a composite type, a vector, or keyword
arguments.
dictionary, a module, a composite type, a vector, a named tuple, a
data frame, a `Tables` object, or keyword arguments.
"""
function render(io::IO, tokens::MustacheTokens, view)
_writer = Writer()
render(io, _writer, tokens, view)
end
function render(io::IO, tokens::MustacheTokens; kwargs...)
d = Dict(kwargs) # [k => v for (k,v) in kwargs]
render(io, tokens, d)
render(io, tokens, kwargs)
end

render(tokens::MustacheTokens, view) = sprint(io -> render(io, tokens, view))
Expand All @@ -66,8 +65,7 @@ function render(io::IO, template::AbstractString, view)
end
function render(io::IO, template::AbstractString; kwargs...)
_writer = Writer()
view = Dict(kwargs) # [k => v for (k,v) in kwargs]
render(io, _writer, parse(template), view)
render(io, _writer, parse(template), kwargs)
end
render(template::AbstractString, view) = sprint(io -> render(io, template, view))
render(template::AbstractString; kwargs...) = sprint(io -> render(io, template; kwargs...))
Expand Down Expand Up @@ -103,8 +101,7 @@ function render_from_file(filepath, view)
end
end
function render_from_file(filepath::AbstractString; kwargs...)
d = Dict(kwargs) # [k => v for (k,v) in kwargs]
render_from_file(filepath, d)
render_from_file(filepath, kwargs)
end

end
5 changes: 5 additions & 0 deletions test/Mustache_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,8 @@ nt = (a="eh", b="bee", c="see")
rt = [nt, nt, nt] # Tables.istable(rt) == true
expected = "eh and bee"^3
@test Mustache.render(tpl, NT=rt) == expected

## test render_from_file
expected = "Testing 1, 2, 3..."
@test render_from_file(joinpath(@__DIR__, "test.tpl"), (one="1", two="2", three="3")) == expected
@test render_from_file(joinpath(@__DIR__, "test.tpl"), one="1", two="2", three="3") == expected
1 change: 1 addition & 0 deletions test/test.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing {{:one}}, {{:two}}, {{:three}}...

0 comments on commit a85e63d

Please sign in to comment.