diff --git a/README.md b/README.md index 74e92ea..fe04135 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,112 @@ -# mustache -A mustache template implementation in smalltalk +# Mustache Templates for Smalltalk +Mustache is a popular templating engine that is supported in many programming languages. I first encountered them in javascript while researching web toolkits. The syntax is small and covers a wide range of use cases. Maybe I should say that I didn’t find something I can’t do with it so far. Although it was designed to be a templating engine for HTML pages it is useful in different areas. -Documentation is at +I have done an implementation for Mustache in smalltalk. It is available at smalltalkhub (update: The source has moved to github: https://github.com/noha/mustache). There is documentation available on how the syntax is to be used. Here are a few examples to show the smalltalk methods you have to call in order to make it work. -https://medium.com/@norbert.hartl/mustache-templates-for-smalltalk-141786191b6 +# How to use? + +Disclaimer: I’m talking about an implementation for smalltalk but to be honest I developed and tested it only on pharo smalltalk 2.0. If you need it for another dialect drop me note and we see what we can do. + +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. + +# Working with lists + +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 := '