Deprecated Features

These are features that still exist in Brevé but that are slated for removal at some time in the future.

  1. Conditional Expressions

Conditional Expressions

BIG FAT WARNING Because Brevé templates are expressions, there's no short-circuit behaviour. The conditional expressions below are evaluated inside-out. This means that the body of the condition is evaluated before the condition itself. The truth of the condition does not prevent the code inside the body of the clause from being executed, it only prevents it from being displayed. If you, for example, reference a non-existent variable inside the body of the clause, you will get an error. To some degree I consider the conditional expressions described below to be a mistake and they are slated for removal in Brevé 2.0.

The when conditional

The simplest conditional expression is the when conditional. It's semantically equivalent to Python's if statement except that it doesn't support an else clause.

html [
    body [
        when ( x == 1 ) [ span [ 'x is 1' ] ],
        when ( x != 1 ) [ span [ 'x is not 1' ] ]
    ]
]

The switch/case conditional

The next conditional expression is the switch/case conditional. If you are familiar with languages such as C, then this is a familiar construct (albeit with somewhat different syntax).

An example:

html [
    body [
        switch ( username ) [
            case ( None ) [ span [ "Please login" ] ],
            default [ "Welcome back, %s" % username ]
        ]
    ]
]

In the above example, the switch/case conditional is used to test the value of the "username" variable. If the variable contains None (which we assume means no one is logged in), then we display a message directing the user to login. If it's anything else, then we assume it contains a valid username and we display a welcome message. There are a few things to note about this construct:

  1. There can be as many "case" directives as you need
  2. "case" directives are evaluated from top to bottom. This means you should probably put the most common cases near the top for performance reasons (although if your conditional is long enough to make much difference then probably you should have put this logic in the controller).
  3. The "default" directive is optional and, if present, must be last. Evaluation of the conditional stops whenever a "case" directive meets the criteria or the "default" directive is encountered, so any further "case" directives after the default will never be evaluated.
  4. If no "case" directive satisfies the test and there is no "default" directive, the expression as a whole evaluates to nothing (i.e. it won't appear in the final HTML output).

Back to Main Documentation

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