stz object trees
One thing that is always awkward with traditional Smalltalk syntax is making object trees. I'd like to know if stz has improved on that with its new syntactical capabilities. Let's try and build a HTML page using {} and then again using interpolated strings.
page-title = 'Hello World'
welcome-message = 'Hello World'
html = { html-builder |
head: {
title: page-title
}
body: {
p: {
span: { style: 'bold', text: welcome-message }
}
}
}
This works because the type 'html' is going to respond to messages such as head: and body: and their types will dictate what goes in to the {} section after them. We don't need to use lists here because the purpose of the API is to append content in to the structure, not modify it. This is creation code after all.
Let's try it again with interpolated strings:
page-title = 'Hello World'
welcome-message = 'Hello World'
html =
`<html>
<head>
<title>~{page-title}</title>
</head>
<body>
<p>
<span style="bold">~{welcome-message}</span>
</p>
</body>
</html>`
This is also good. They're both good. They're both different too. The second one gives us a string we can send off to a client from a server request but the first one gives us objects we can manipulate and serialise however we want (including turning it in to a string).
That both syntaxes work well is a delight to me. The string version needs to be modified to escape the strings before we append them in there which makes me wonder if we can't do something special with `` syntax to specify how things should be escaped. An idea to pursue another time.
I think we can put this in to the win bucket and move on.