GithubHelp home page GithubHelp logo

Comments (8)

asmala avatar asmala commented on July 25, 2024

Based on a quick look at the source, this should be fairly easy to implement. Not sure which direction James wants to take Hiccup, but something like this should do the trick:

(defn include-css
  "Include a list of external stylesheet files."
  [attr-map & styles]
  (if (map? attr-map)
    (for [style styles]
      [:link (merge {:type "text/css", :href (to-uri style), :rel "stylesheet"}
                    attr-map)])
    (apply include-css {} attr-map contents)))

Depending on how important performance is, this could also be implemented as a macro—a là xhtml in the same namespace—for compile time resolution of the (if (map? attr-map)) "dispatch".

And for backwards compatibility, it might be a good idea to add support for the degenerate case of (include-css), i.e. a call with no arguments.

from hiccup.

weavejester avatar weavejester commented on July 25, 2024

Or just change defn to defelem, which automatically adds the optional attr-map.

from hiccup.

asmala avatar asmala commented on July 25, 2024

That wouldn't work since include-css (and include-js) can both return multiple elements. In this case the optional attributes would only be applied to the first of the returned elements.

from hiccup.

weavejester avatar weavejester commented on July 25, 2024

Ah, quite right. So something like defelem but which works for a list of elements.

from hiccup.

asmala avatar asmala commented on July 25, 2024

In that case, the two main options seem to be:

  1. Change include-css and include-css work as if they were created using defelem
  2. Change defelem to add the optional attr-map to all elements in case the function body returns a seq of elements
  3. Create a separate defelemseq to deal with function bodies returning element sequences

Option 1 seems rather limited and option 3 contorted so my preference would be for option 2, but that does break backwards compatibility in one edge case:

(defelem titles [& titles]
  (for [title titles]
    [:h1 title]))

(titles {:class "main-headline"} "Latest news" "Yesterday")

;With current implementation
;=> ([:h1 {:class "main-headline"} "Latest news"]
;    [:h1 "Yesterday"])

;With suggested implementation. Note attributes on second element.
;=> ([:h1 {:class "main-headline"} "Latest news"]
;    [:h1 {:class "main-headline"} "Yesterday"])

I can take a stab at a patch if you think one of the above would work. (Speaking of which, would you mind taking a look at #68? 😉)

from hiccup.

asmala avatar asmala commented on July 25, 2024

I implemented this in the 2.0 branch. Page includes now use defelem and defelem applies attrs to seqs.

from hiccup.

bostonaholic avatar bostonaholic commented on July 25, 2024

Any idea when allowing {:media "screen"} for include-css will be released?

from hiccup.

weavejester avatar weavejester commented on July 25, 2024

Hiccup currently isn't that far up my priority list, I'm afraid. It might be a while until I get around to completing the 2.0 branch.

from hiccup.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.