Composing snippets

63 views
Skip to first unread message

Edwin Park

unread,
Jan 21, 2014, 5:21:32 PM1/21/14
to enliv...@googlegroups.com
I'm having some trouble finding a nice way to compose snippets for my use case. I may be approaching this wrong too, would love to hear better suggestions.

I have a base html page that represents the basic structure of a page, and an associated snippet that fills in common stuff like title and description:

(defsnippet base-snippet "base.html" [:html]
  [title desc]
  [:head :title] (content title)
  [:.desc] (content desc))

I want to use this as the basis for various other page types. As a contrived example, say there are two other pages that show Fruits and Vegetables that both use the base snippet to show their title and description. The plan is to use the base snippet to set the common fields, and then apply whatever additional fruit- or veggie-specific modifications are needed. The trick is that the base snippet is _parameterized_. I need to pass it values for title and desc to get the nodes for the base page, so I can't use (deftemplate (base-snippet name desc) [name desc] ...). This is the best I could come up with:

(defn fruit-template
  [name desc sweetness-factor]
  ((template (base-snippet name) []
    [:.sweet] (content sweetness-factor)))

(defn veggie-template
  [name desc fiber-content]
  ((template (base-snippet name) []
    [:.fiber] (content fiber-content)))

This works but it feels pretty hokey. I'm thinking there must be a better way. Can someone please enlighten me?

Thanks,
Edwin

Edwin Park

unread,
Jan 22, 2014, 11:24:54 AM1/22/14
to enliv...@googlegroups.com
I realized I was fighting with syntax, so I made a simple macro to let me rewrite things the way I wanted to:

(defmacro transform-snippet
  [base-snippet & forms]
  `((transformation ~@forms) ~base-snippet))


(defn fruit-template
  [name desc sweetness-factor]
  (emit*
    (transform-snippet (base-snippet name)
      [:.sweet] (content sweetness-factor))))
Reply all
Reply to author
Forward
0 new messages