Rheo
Rheo is a template library built on top of html-tokenize
and html-select
it is heavily inspired by hyperstream
and hyperspace
.
Usage
Rheo uses streams everywhere, and where it can't use streams it uses callbacks that recieves and returns streams, with few exceptions.
Simple example
The simplest and not very interesting you can do is to just parse and then render html, like so:
\\ or html_stream
Using CSS selectors
That was not really exciting, right? Let's do something more fun and change the message from the last "exercise".
html_stream
or the short hand:
html_stream
Outer and inner both return a new rheo stream and you could chain multiple of these calls and shape your template.
Changing attributes
A lot of the things that happen in a webpage are defined by attributes. This is the only place where the stream/callback pattern breaks in Rheo. But don't worry it's still not complicated. And in theory we are only a pull request away from unifying the interface.
html_stream
Or shorthand if there is no other classes.
html_stream
A little rainbow in your headings could never hurt, could it?
If you would like to change multiple attributes on the same element there is a shorthand for that to.
html_stream attributes'a' 'class': 'rainbow' 'href': '#rainbow_power'
Design by Example
Sometimes hardcoded is just plain better then handcrafting a machinery that does the same thing. Therefore its easy to pick one of many options when replacing a content with one of its elements.
Your code will read like well writen prose. You will be stuck in the debugger all day.
html_stream
Iteration, map
HTML is a tree structure writen in a serial format. Sooner or later we want map some stream of data into a stream of html.
Take a look at this view controller.
{ return template }
This function binds data to one pet template. This could be used for a pet profile, with the correct HTML. But it can also be reused for building list of pets.
It works fine now to render one, but how do we render all list items?
html_stream
Assuming that pet_data_stream
is a stream of pet data objects and that
html_stream
is a text stream of html with a list with the class pet-list
and that that list contains one example of a pet list item, then this will
render a list of pets in that list.
Super Modularity
As you can see there is enormus potential to decouple your layout and templating from logic. And that is both good and dangerous so please be careful! But one more neat trick needs to be shared.
How many times have a templating librarie let you down when doing something as
trivial as setting the title
of your page. I have seen realy nasty ways to
deal with that and selecting the current page in a menu.
Changing things that have already been renderd in a layout
template is
normaly frustrating and hackish. With Rheo? No, it's simple!
Rheo templates can be piped together like so:
{ var selector = '.menu .menu-item.' + menu_name return rheo} { return rheo} page_template
As long as the element with the selector that you want to alter is put into the stream it doesn't matter when you add the menu or change the title, except for performence. Neat huh!?
Advanced example
Todo
var rheo = html_stream