Cubictemp - Blocks

Named blocks

Cubictemp blocks are chunks of text that are defined in the template body, and can be referenced in various ways. The simplest block variety is the named block:

<!--(block bar)-->
    The time has come the walrus said
<!--(end)-->
one
@!bar!@
two
@!bar!@
three

Output:

one
    The time has come the walrus said...
two
    The time has come the walrus said...
three

Like a function, named blocks only produce output when they are referenced. The block definition itself does not appear in the template output.

Named blocks are callable objects that accept an over-riding namespace argument:

<!--(block bar)-->
    The time has come the @!foo!@ said...
<!--(end)-->
@!bar(foo="walrus")!@

@!bar(foo="carpenter")!@

Output:

    The time has come the walrus said

    The time has come the carpenter said

Blocks can be nested to arbitrary depths. Block namespaces and scopes work similarly to Python namespaces and scopes.

Named blocks have the _cubictemp_unescaped attribute defined by default, so they will go unescaped when inserted using the standard @!...!@ escaped substitution syntax.

Repeat Blocks

Cubictemp provides a repeat construct to allow traversal of iterables:

<!--(for foo in bar)-->
    @!foo!@
<!--(end)-->

Like a Python for loop, the template above loops through all elements of "bar", setting the value of "foo" to each element in turn. As in simple substitutions, any valid expression can be used as the sequence definition:

<!--(for foo in range(3))-->
    Counting: @!foo!@
<!--(end)-->

Output:

Counting: 0
Counting: 1
Counting: 2

Playing nice with your designers: closed and open tags

There is a minor variation on the block definition syntax to help preserve document structure during template design. Since the start and end directives of Cubictemp blocks look like HTML comments, they are not visible when the document is viewed as HTML. It is often convenient to be able to also comment everything inside the block. This is accomplished using the open tag variation:

<!--(block foo)
    The time has come the walrus said...
(end)-->

There is no functional difference between the two flavours.

Copyright Nullcube 2008