Skip to content

Commit

Permalink
Merge pull request #6 from DelGaylord/master
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
noha committed Sep 28, 2022
2 parents 8cfba84 + c5403f4 commit d791333
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,42 @@ Disclaimer: I’m talking about an implementation for smalltalk but to be honest

A simple Mustache template looks like this (taken from the documentation):

```
templateString := ‘Hello {{ name }}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}’.
```


Given a context object with content

```
context := {
'name' -> 'Chris'.
'value' -> 10000.
'taxed_value' -> (10000 - (10000 * 0.4)).
'in_ca' -> true } asDictionary
```
we execute the template

```
(MustacheTemplate on: templateString) value: context
```
or

```
templateString asMustacheTemplate value: context
```

we get the following output

```
Hello Chris
You have just won $10000!
Well, $6000.0, after taxes.
```

Note: You’ll get a lot more newlines and whitespaces than the string shown here. It is not clear to me what the rules are for condensing whitespace. The will be changed in an upcoming version of mustache
As context object we can use Dictionaries and Objects. Dictionaries need to have a key that is used in the template and Objects need a selector with the same name. In this post I use Dictionaries because they are easier to illustrate with.
Expand All @@ -43,43 +53,44 @@ As context object we can use Dictionaries and Objects. Dictionaries need to have

We can use collections to make loop constructs in templates

```
templateString := 'A list of numbers
{{# list }}
Number: {{ number }}
{{/ list }}'.
```
A context object with content

```
{
'label' -> 'fine'.
'list' -> {
{ 'number' -> 1 } asDictionary.
{ 'number' -> 2 } asDictionary.
}
} asDictionary

```
gives us the output

```
'A list of numbers
Number: 1
Number: 2
'

```
# And Blocks as well

We can use blocks in context objects. They will be evaluated at the time the template is filled out.

```
'The alphabet: {{ alphabet }}' asMustacheTemplate
value: { 'alphabet' -> [ Character alphabet ] } asDictionary

```
prints

```
The alphabet: abcdefghijklmnopqrstuvwxyz

```
# Partial templates

Mustache templates have a notion of sub templates that are called partials. With partials we can nested templates this way
```
templateString := '<h2>Names</h2>
{{# names }}
{{> user }}
Expand All @@ -90,23 +101,41 @@ Mustache templates have a notion of sub templates that are called partials. With
'names' {
{ 'name' -> 'Username' } asDictionary } } asDictionary
partials: {'user' -> userTemplateString} asDictionary

```
prints the output

```
<h2>Names</h2>
<strong>Username</strong>

```
The dictionary given as partials: argument is supposed to be a dictionary that contains MustacheTemplates itself. A dictionary of strings will do as well because the strings are converted internally.

# how about json?
# How about json?

Json is really easy to apply to the templates. If you have a pharo2.0 image just click outside a window. From the upcoming menu select tools and then “Configuration Browser”. Scroll down to NeoJSON and click the install button. After that it is just

```
'I can use {{name}} easily with {{format}}' asMustacheTemplate
value: (NeoJSONReader
fromString: '{ "name" : "mustache", "format" : "json" }')

```
Copy that to pharo workspace and execute to see the result.

# templates made easy
# Templates made easy
Mustache can make template dependent tasks very easy from a simple token replacement up to nested structures to create HTML pages. I use them e.g. for generating SOAP templates. The purpose of this post is to show the basic usage of it. The strength of Mustache lays in the syntax and the combination of context objects. So, there is more for you to find what can be done with it. If what you find is a bug I like to know. Happy templating !

# How to load

```
Metacello new
baseline: 'Mustache';
repository: 'github://noha/mustache:v1.0/repository';
load
```

# How to depend on it

```
spec
baseline: 'Mustache'
with: [ spec repository: 'github://noha/mustache:v1.0/repository' ].
```

0 comments on commit d791333

Please sign in to comment.