These are features that still exist in Brevé but that are slated for removal at some time in the future.
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 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 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:
Back to Main Documentation