Wow, thanks for the guidance, Enlive is improving at a rapid clip! ;)

On Friday, April 17, 2009, Christophe Grand <christo...@cgrand.net> wrote:
>
> Updating to reflect last changes:
>
> (def snipgets
>   (let [divs (select (html-resource "widget.html")
>                [[:div (attr? :tiptree:widget)]])]
>     (into {}
>       (for [div divs]
>         [(-> div :attrs :tiptree:widget)
>          (snippet div [root]
>           [widget]
>            [:div.value] (content (if widget
>                                    (:value widget)
>                                    "foo")))))))
>
>
>
> Christophe Grand a écrit :
>> select, snippet, at and template are macro sugar. If you _really_ want
>> to use first-class selectors then you'll have to deal directly with
>> states machines and use at*, select* and snippet* (I'm going to improve
>> their usability).
>>
>> While selectors aren't first class, they can be parametrized (as you did
>> in [[:div (attr= :tiptree:widget (str widgetType))]]). You can play with
>> compile-selector to see how selectors are expanded.
>>
>> Regarding your code, I'd like to warn you against using snippet like
>> that for performance reason (and it won't get any better when I make
>> templates rendering faster).
>> It's better to extract snippets once for all:
>>
>> (def snipgets
>>   (let [divs (mapcat #(select % [[:div (attr? :tiptree:widget)]])
>>                (html-resource "widget.html"))]
>>     (into {}
>>       (for [div divs]
>>         [(-> div :attrs :tiptree:widget)
>>          (snippet div [root]
>>           [widget]
>>            [:div.value] (content (if widget
>>                                    (:value widget)
>>                                    "foo")))))))
>>
>> (deftemplate my-app6 "app2.html"
>>  [widgets]
>>   [[:div (attr? :tiptree:replace)]]
>>     (fn [node]
>>       (let [widgetType (:tiptree:replace (:attrs node))]
>>         ((snipgets widgetType) ((keyword widgetType) widgets)))))
>>
>>
>> David Nolen a écrit :
>>
>>> On second thought, this is actually not that critical for what I'm
>>> trying to accomplish, and I'm not sure yet if I'll ever use such a
>>> feature. Macros that define snippets will probably suffice.
>>>
>>> (deftemplate my-app6 "app2.html"
>>>   [widgets]
>>>   [[:div (attr? :tiptree:replace)]]
>>>     (fn [node]
>>>       (let [widgetType (:tiptree:replace (:attrs node))]
>>> ((snippet "widget.html" [[:div (attr= :tiptree:widget (str widgetType))]]
>>> [widget]
>>> [:div.value] (content (if widget
>>> (:value widget)
>>> "foo")))
>>> ((keyword widgetType) widgets)))))
>>>
>>> (apply str
>>>        (my-app6 {:widgetA {:value "0"},
>>> :widgetB {:value "1"}}))
>>>
>>> Works for me, and this was in general the use case I was thinking of.
>>>
>>>
>>> On Thu, Apr 16, 2009 at 4:24 PM, David Nolen <dnolen.li...@gmail.com
>>> <mailto:dnolen.li...@gmail.com>> wrote:
>>>
>>>     Because predicates in selectors no longer need to be quoted it
>>>     seems you can't use Enlive selectors in a first class way with
>>>     snippets:
>>>
>>>     (let [aselector [[:div (attr= :tiptree:widget "widgetA")]]]
>>>       ((snippet "widget.html" aselector
>>>        [some-map]
>>>        [:div.value] (content "foo")) {}))
>>>
>>>     I believe this might be one of my final big requests :) I
>>>     personally don't mind the quoted predicate forms, especially if
>>>     this would simplify making selectors first class.  This would
>>>     allow templates to dynamically generate snippets based on the
>>>     properties of a particular node.
>>>
>>>     There's been a wild flurry of updates to Enlive recently and I am
>>>     extremely excited about the possibilities.  Enlive is an idea
>>>     which should be ripped off by every web framework worth talking
>>>     about! ;) Thanks again for creating and maintaining it.
>>>
>>>     On Thu, Apr 16, 2009 at 7:15 AM, Christophe Grand
>>>     <christo...@cgrand.net <mailto:christophe@

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to