home | news | documentation | downloads | discussion | projects | license  

Why Clearsilver?
Clearsilver Basics
   HDF Dataset
   Template Syntax
   CGI Kit
      Config Vars
API Reference
   C API
   Python API
   Perl API
   Java API

Comparison with PHP, ASP, JSP
Comparison with XML/XSLT



Template Expressions

Clearsilver has a generalized expression syntax which can be used in place of any paramater.

Some example expressions:

  Page["Title"] == "Home"
  (#Page.Count > #1) || (?Page.Next)
ClearSilver expressions have four different argument types. They are:
  • A number. A number is any string that can be converted to a number, or you can force a string to be a number using the # operator.
    Examples include: 103, 0x1a, -23, +14, #83
  • A String. A string is any set of characters within either single quotes or double quotes. There is currently no escaping mechanism.
  • A Variable. A variable is either a reference to a local variable (as created by call/def, each, with, loop) or a reference to the global HDF dataset. If your variable name is a number, you must use the $ operator to force variable evaluation.
  • Numeric Variable. This is a variable value converted to a number.

ClearSilver expressions have four different evaluation types: variable, string, numeric and boolean. Which evaluation is used depends on the types involved and the operator. Operators are either boolean, numeric, or numeric/string. For numeric/string operators, if either argument is a number, then they are evaluated as a number. Otherwise, the string version of the operator is used. The only really different operator is +, which is numeric addition and string concatenation.

Evaluation as Boolean:

  • Number: 0 is false, everything else is true
  • String: Empty string is false. String "0" is false. Everything else is true.
  • Variable: Non-existing variable is false. Existing variable is treated the same as a string.
  • Numeric Variable: Non-existing is 0. Doesn't convert to a number is treated as a 0. Otherwise, converted to a number. Evaluated same as a Number.
Evaluation as a Number:
  • Number: as a number
  • String: Converted to a number. If its not a valid number, than it will be converted to 0.
  • Variable: If it doesn't exist, its 0. Otherwise, treated the same as a string.
  • Numeric Variable: Same as Variable.
Evaluation as a String:
  • Number: a number in base 10
  • String: As String
  • Variable: Non-existing variable is the empty string.
  • Numeric Variable: Converted to number, than converted to string in base 10.
Evaluation as a Variable is used when you expect the operator to be a variable. Using a variable operator on a non-variable argument is undefined. In some instances, it will raise an error, in some instances ClearSilver will treat a string as a variable name, in some cases it will just ignore it. This behavior is subject to change at any time, don't rely on the current behavior. The dot and bracket operators are used to refer to lower parts of the HDF dataset.

The list of operators, from low to high precedence:
Operator Operation Evaluation Type
, C Comma Operator n/a
|| Boolean OR Boolean
&& Boolean AND Boolean
== Equal String/Numeric
!= Not Equal String/Numeric
> String Greater Than String/Numeric
>= Greater Than/Equals String/Numeric
< Less Than String/Numeric
<= Less Than/Equals String/Numeric
+ String Concat / Add String/Numeric
- Subtract Numeric
* Multiply Numeric
/ Divide Numeric
% Modulo Numeric
+ (unary) Positive Numeric
- (unary) Negative Numeric
# (unary) Force Numeric Numeric
$ (unary) Force Variable Variable
! (unary) Boolean NOT Boolean
? (unary) Existance Boolean
. Descend Variable Name Variable
[ ] Expand Variable Name Variable
( ) Function Call n/a
Addition does not force numeric evaluation. During string evaluation, + represents string concatination.

Here is an example of rendering data into 2 columns in an HTML table:

    <th>Column 1</th><th>Column 2</th>
  <?cs set:count = #0 ?>
  <?cs each:item = Page.Items ?>
    <?cs if:count % #2 ?>
    <?cs /if ?>
      <td><?cs var:item.Number ?> - 
        <?cs var:item.Name ?></td>
    <?cs set:count = count + #1 ?>
    <?cs if:count % #2 ?>
    <?cs /if ?>
  <?cs /each ?>

Previous: Template Syntax Next: Macros

Copyright © 2017 Brandon Long, All rights reserved.