Git Product home page Git Product logo

Comments (5)

mbutterick avatar mbutterick commented on June 24, 2024 2

At one point I thought about having templates for templates but it all got too ridiculous. In my experience most of what you describe is a matter of importing a common header and footer, which is easy enough to do with file->string, for instance:

;; test.html.pm
#lang pollen
hello world
;; template.html.p
◊(local-require racket/file)
◊(file->string "header.rktd")
◊(->html doc)
;; header.rktd
<h1>Included!</h1>

from pollen-users.

odanoburu avatar odanoburu commented on June 24, 2024

would defining the header as a variable that you insert in every template work? it's not nesting, but…

from pollen-users.

basus avatar basus commented on June 24, 2024

Hmm, I suppose I could define each component of the template separately (possibly as Pollen code) and then each template.html.p file combines the components I want.

from pollen-users.

otherjoel avatar otherjoel commented on June 24, 2024

One approach I’ve used was to create a separate module, and all the reusable HTML snippets I might want in a template are implemented there as functions. (Example) Then I require and re-provide this module from pollen.rkt so it is available for use inside templates.

My understanding is that the more code you put inside Racket modules as opposed to putting it inside the template itself, the better, because Racket modules can be precompiled and cached while templates are evaluated fresh every time they are used.

Note the use of #lang pollen/mode to allow using Pollen syntax inside a Racket module (more info in the Pollen docs). This is just a convenience that comes in handy for templatey stuff. Another way of representing HTML snippets as functions is to use scribble/html, as shown in @soegaard's web-tutorial project (see this file in particular).

from pollen-users.

basus avatar basus commented on June 24, 2024

I ended up addressing this by doing as much as possible with X-expressions in Racket. I have a bunch of snippets which look like:

;; Default code snippets, mostly for inclusion in templates
(define (default-head)
  `(head
    ,@(meta:defaults)
    ,(head:title)
    ,@(head:stylesheets "/css/fonts.css" "/css/style.css")))

(define (default-navigation)
  `(header ,(navigation "Colophon" "Posts" "Drafts" "Series")))

(define (body-with #:id [id #f]
                   #:class [cls #f]
                   #:navigation [nav (default-navigation)]
                   #:contents contents)
  (let ((attrs (match* (id cls)
                 [ [#f #f] '() ]
                 [ [id #f] `((id ,id)) ]
                 [ [#f cls] `((class ,cls)) ]
                 [ [id cls] `((class ,cls) (id ,id)) ]
                 )))
  `(body ,attrs ,nav ,contents)))

and then I can build templates like so:

<!DOCTYPE html>
<html lang="en">
  ◊(->html (default-head))
  ◊(->html (body-with
            #:class "theme-dark"
            #:contents `(div ((id "content"))
                             ,@(select* 'root doc))))
</html>

from pollen-users.

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.