6 ms·
I think that this is an interesting experiment, and reminds me of Clojure's hiccup[1] library. In hiccup, you model html to be rendered as nested vectors and m
by zachallaun 14y ago
I think that this is an interesting experiment, and reminds me of Clojure's hiccup[1] library.
In hiccup, you model html to be rendered as nested vectors and maps, such that
<span class="foo">bar</span>
becomes
(html [:span {:class "foo"} "bar"])
Where this is a real win is when you realize that it's just data:
(html [:ol
(for [x (range 1 4)]
[:li x])])
Lisps are well suited to model html.
[1] https://github.com/weavejester/hiccup https://github.com/weavejester/hiccup
- JimmyRuska 14y agoThe table and lists parsers are exactly that. Syntax transformations after I have a list made of dom nodes and binary elements. I can make a DSL parser attached to any tag I make. I just don't know what else would be useful.
- judofyr 14y agoRuby has had these type of libraries for years. The original is probably Markaby, but today there are alternatives that are both faster (https://github.com/camping/mab/ https://github.com/camping/mab/) and has more features (http://erector.rubyforge.org/ http://erector.rubyforge.org/). It's an interesting concept, but I find that (1) it's hard to work on it together with designers and (2) for larger templates I don't find it that elegant.
- autodidakto 14y agoThe theme between these efforts seems to me: Instead of a separate language in a separate world, our views are normal objects/data (in all their glory and power) that happen to know how to print themselves as html.
- slurgfest 14y agoI'm certainly no cultist of MVC, but I would tend to agree with them that tightly coupling presentation details to the basic model is usually unwise.
- judofyr 14y agoYou're not supposed to put these into your models. You should create a different class/method that accepts models and returns HTML: class TweetWidget def initialize(tweet) @tweet = tweet end def content; … end end
- chalst 14y agoOleg Kiselyov has done some nice work where he defines SXML, a representation of XML in Scheme, and parsers/ pretty printers between XML and Scheme. This allows you to use hygienic macros (hygiene becomes important in the absence of namespaces). See http://www.okmij.org/ftp/Scheme/xml.html#SXML-spec http://www.okmij.org/ftp/Scheme/xml.html#SXML-spec Lua's table syntax is well-suited to this style of embedding html/xml syntax in a programming language, e.g., if you define the appropriate functions a and i to represent anchors and italics, then you can define an anchor element: a {href=url, name="next", "Go to the", i "next", "element"}
- draegtun 14y agoMost languages will have literals that can be used to easily represent HTML data: [a => {href=>url, name=>'next'}, 'Go to the', [i => 'next'], 'element']; # perl5 [a => {:href(url), :name<next>}, 'Go to the', [:i<next>], 'element']; # perl6 [:a => {:href=>url, :name=>'next'}, 'Go to the', [:i => 'next'], 'element'] # ruby Your Lua example is very similar to the HTML::AsSubs CPAN module (https://metacpan.org/module/HTML::AsSubs https://metacpan.org/module/HTML::AsSubs). Here is your example using this module: use HTML::AsSubs; sub url { 'http://www.anythingfornow.com' } my $h = html( a( {href=>url, name=>'next'}, 'Go to the', i('next'), 'element', ), ); say $h->as_HTML;
- james2vegas 14y agoWhich is, of course, just CGI.pm's html functions again.
- chalst 14y agoa { ... } and i "next" are not table elements, they are function applications. All of your examples use more magic characters than the Lua example I gave.
- draegtun 14y agoThe HTML::AsSubs example uses functions and works exactly (see [1]) the same way your Lua code does. The first set of examples are just to show that you can model HTML data within common Hash/Array literals available in most languages. re: magic characters - The perl6 example does use some extra shortcut niceties :) However the perl5 example will actually work unchanged in perl6. The Ruby example is only slightly different to perl5 because it uses symbol sigil (Perl like Lua allows barewords on LHS of a table/hash key assignment). [1] - The only difference is the Lua a() function receives all its content via a single Table. Whereas HTML::AsSubs a() is a variadic function and uses the Hash literal {} for assigning HTML attributes.