hargettp / hh-web

Framework for building modern web applications in Lisp
MIT License
36 stars 4 forks source link

Question: Updating just portions of the page? #8

Closed sabracrolleton closed 10 years ago

sabracrolleton commented 10 years ago

The wiki states that "All templates that generate HTML end in a single page expression in order to generate full HTML pages." Does this mean that you can't update just a portion of a page?

Thanks,

Sabra

hargettp commented 10 years ago

Not really. Sounds like the wiki is a little misleading.

I was really trying to communicate little bit about how templates are expected to work: they really are just files with Lisp expressions in them, so the last expression is what renders the output--and for a typical page, it renders a full page.

You can update a portion of the page, if you use AJAX just as in any ordinary web app. Use AJAX to get content from an URL, then use that content to update a portion of a page. The key is instead of using the page macro, use the html macro.

So, for example, if you define a new template in file fragment.lisp like this:

(+tag-library :html)
(+tag-library :ui-theme)

(html
   (div "hello world!"))

Then define the template in your templates.lisp:

;; inside the deftemplates expression
(fragment-html "fragment.lisp")

Then define an URL for your partial template in urls.lisp:

;; Dispatch to home page template
(defurl "^/fragment$" :handler (fragment-html))

Then you can just hit http://localhost:8000/fragment, and you will see the rendered content (in this case it's just a simple <div>hello world!</div>).

You still have to write the Javascript to make the AJAX call and then use the retrieved content to update some portion of your page, but otherwise that's all that would be necessary.

sabracrolleton commented 10 years ago

Thank you.

Sabra