guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

sfx: baby steps of an html templating engine based on skribillo's skribe


From: Amirouche Boubekki
Subject: sfx: baby steps of an html templating engine based on skribillo's skribe reader and sxml
Date: Thu, 30 Jul 2015 13:55:00 +0200
User-agent: Roundcube Webmail/1.1.2

Héllo,


I've been lurking around skribillo and artanis. I don't really like the rails-like syntax of artanis, even if it has its use-cases and I wanted to hack on something "small", so
I've put together sfx.

The code of skribe reader is included in sfx.scm. So the only dependency is guile (2.0.11) and
guile-reader that you can install using `guix package -i guile-reader`.

This bare template language has the following features:

- wanna be simpler sxml syntax
- templates with custom environment
- external libraries loading inside the template


# Wanna be simpler sxml syntax

Skribe reader (implemented with guile-reader) provide a handy syntax to
both define keywords and quasiquote. In an sxml context those features
are used to implemented attributes and text nodes.

## attributes

Attributes in sxml are defined as follow:

  (div (@ (id "shell")) "This the main area")

Instead of requiring the nesting of `(attribute-name attribute-value)` sfx use
the simpler keyword syntax `:keyword`. The above snippet becomes:

  (div (@ :id "shell") "This the main area")

I'm not sure it's worth the trouble of diverting from sxml standard. That said, it looks
more like plain xml.

## text nodes

Text nodes can be defined as

  (p [héllo hacker])

This is looks the same as the default reader. It becomes handy when you include an
inline element inside the text node:

  (p [héllo ,(b [hacker])

`,()` is a special syntax of skribe reader which provides `(unquote)` inside [bracket] `quasiquote`.

With the default guile reader, this must be written as:

  (p "héllo " (b "hacker"))

This is looks verbose and prone to error. One must not forget the space in the
string before the `(b)` element.


# templates with custom environment

Right now this part of the template language is not really userfriendly. But you can pass custom variables to the template but those must be parameters. In the example sfx.scm (which includes example use of the procedures) the environment in which the template is evaled is defined as follow:

  (define value (make-parameter 42))
  (define amirouche (make-person "amirouche" 30))
  (define env (let ((value value)
                    (amirouche amirouche))
                (the-environment)))


Then `value` can be echo'ed inside the template using the unquote syntax `,()`, e.g.

   (p [Here is a lucky number for you «,(value)»])

As you can see the previous snippet, there is also a `<record>` record inside the environment. One can (maybe) provide in the environment the required procedures to echo the correct fields but this is verbose. Instead sfx use `(use-modules)` inside the template definition
file. This is presented in the following and last part.

# external libraries loading inside the template

Currently it's (only) possible to do `(use-modules)` inside the template file. The template
file looks like the following:

```
(use-modules (person))

`(html
  (body
    (p [My name is ,(person-name amirouche)])))
```

I could not make procedure definition work inside the template, this my be linked to the way I eval the template. It's shame because for quick and dirty hacks it can be handy like defining
mini-templates inside the big template.





This is my second try at this and having a look at the code of haunt [1] was helpful.

Hope this helps!


[1] https://git.dthompson.us/haunt.git



--
Amirouche ~ amz3 ~ http://www.hyperdev.fr

Attachment: index.scm
Description: Text document

Attachment: sfx.scm
Description: Text document

Attachment: person.scm
Description: Text document


reply via email to

[Prev in Thread] Current Thread [Next in Thread]