Fragments

Note: Since version 1.0.26, Brevé has been refactored to support two rendering methods: render( ) and render_partial( ). render_partial( ) is the equivalent of a fragment and the Buffet adapter has been updated to call this method when fragment = True is passed as an argument to render( ). The key difference between the two is that render_partial( ) doesn't prepend the DTD. Aside from this, the following is still relevant.

Many template engines support the notion of a "fragment", which is usually taken as a flag when rendering to not apply any master template or site theme. This is usually used in conjunction with an AJAX request to fill in a portion of an existing page.

Brevé has no separate notion of a single "master template" (which would both complicate and decrease the flexibility of the inheritance mechanism), but it's easy to achieve the same results by placing fragments in their own templates and inheriting from a "master" template. For example, let's say you have a dynamically generated list that you want to use both in a template proper and be able to call as a separate "fragment" from an AJAX request. You could use something like the following:

# index.b - equivalent to a "master template"
html [
    body [
        slot ( 'content' )
    ]
]

# page.b
inherits ( 'index' ) [
    override ( 'content' ) [
        include ( 'list' )
    ]
]

# list.b
ul [
    [ li [ i ]
      for i in list_items ]
]

Now, if the page template is rendered, list will be returned enclosed in the global site template (index). However if you call for just list, you'll get the desired fragment that can be used in an AJAX request.

edit page
Back to top
Rendered using Brevé 1.3.0Copyright © 2007, Cliff Wells