Language

Sytes is a Scheme-like mini-language designed for writing text. Contrary to popular template languages like PHP, in Sytes each expression is well formed. For example PHP operates in two modes: plain text and PHP, and you alternate between modes using the special <?php, <? and ?> markers. Sytes is not much different here, except that the default markers are easier to type (the brackets), but it does differ in one fundamental way that I'll try to describe below.

A common if expression in PHP would look like this:

<?php if ($a == $b) { ?>
  <h1>equal</h1>
<? } else { ?>
  <h1>not equal</h1>
<? } ?>
Of course, for such a simple case you'd probably prefer to use echo rather than splitting the expression with those monstrous markers, but if you need to generate more text then echo is cumbersome.

Now what happens there is that you cut off the IF expression in the middle—it has an open bracket, then it's suddently back to text mode. To me it simply looks like an invalid program, but that's how PHP works. Seems there's only two modes—either TEXT, or PHP—but there's no nesting.

Sytes operates differently. An expression like the above could be written as follows:

{(if (eq a b) {
   <h1>equal</h1>
} {
   <h1>not equal</h1>
})}

Note that the text bits are nested into the IF expression using parentheses/brackets; the code is not flat. The if takes 3 arguments: the condition, an expression to evaluate if the condition stands true, and optionally a second expression to evaluate if the condition is false.

What happens here is that brackets do commute parser mode between HTML and Lisp, but it does so recursively—the result of a {text} part is entered into the current Lisp expression as a string, and the result of a {lisp} part is included into the current text expression.

Functions

Thanks to the elegance of the syntax, you can for example define a function that returns a piece of text (and by that I mean you can write that text freely, rather than inside a string):

{[defun section (title body) {
  <div class="section">
    <div class="title">{\title}</div>
    <div class="body">{body}</div>
  </div>
}]}

Now here's how you can call it:

{(section "The <pre> tag" {
  <p>
    The PRE element is useful for displaying source code.
    Blah blah blah.
  </p>
})}

Note that the function displays the title as {\title} — by prefixing it with a backslash we request HTML-escaping the value into the output. That's syntactic sugar for {(esc title)}.

The ability to easily include text, pass it around or return it makes writing documents much more pleasurable. I'm not sure it's possible to write a function like the above in PHP, but if it is, the syntax must be horrid.

Finally, combining escaping with the ability to easily include text in a Lisp expression makes it possible to write something like this:

<p>
  XML example:
</p>
<pre>{\{
  <foo>
    <bar value="10" />
  </foo>
}}</pre>

I'm using this hack all over these pages in order to include source code examples. Basically, I don't need to escape code manually because {\{ ... }} does it for me. Actually, I can't think of any use for {{...}}—that would commute to Lisp mode then immediately to text mode, so I thought I'd escape by default in this situation. There you go, just did it. {{<p>}} will output &lt;p&gt;.

Fork me on Github