Main  /  Edit version 2  /  Edit version 3  /   /  Users Area

Difference "Templates: Flow Control" ver. 2 versus ver. 3

Content:

## Table of Contents
- [Conditions --- `if`](#if)
- [Loops --- `foreach`](#foreach)
<!--toc-->
## <span id="if"></span>Conditions
## Conditions
### Syntax
Begin or continue conditional statement:
( 'if' | 'elif' ) [ 'not' ] [ 'empty' ] ( VARIABLE | 'rtl' )
( 'if' | 'elif' ) '(' any-c++-expression ')'
Else block:
'else'
Each if/else block should be ended with `end` statement. For example:
<% if not empty username %>
<h1>Hello <% username %></h1>
<% else %>
<h1>Hello Visitor</h1>
<% end %>
<% if ( content.n % 2==0 ) %>
<% n %> is odd.
<% elif ( content.n % 3 ==0) % >
<% n %> can be divided by 3 without reminder.
<% end %>
### Description
This is ordinary `if`/`else if` statement. You can specify following conditions:
- By specifying variable you check whether it is true.
- By specifying `empty` keyword you may check if specific STL container is empty by calling its member `empty()`.
- You can negate the result using `not`
- You can also check whether current locale referred to Right-To-Left language like Hebrew, Arabic or Persian by specifying `rtl` keyword.
This condition actually checks in dictionary if string "LTR" is translated as "RTL".
- You can specify arbitrary C++ conditional expression withing round brackets `()`. _Note_: Do not forget to refer to content variables using `content.` prefix.
## <span id="foreach"></span>Foreach block
## Foreach block
### Sytnax
Major loop:
'foreach' ['widget'] NAME 'in' VARIABLE
In case of empty collection:
'empty'
Specify central body:
'item'
'separator'
For example:
<% foreach student in students %>
<ul>
<% item %>
<li><% student.id %>, <% student.name %></li>
<% end %>
</ul>
<% empty %>
<h2>No students</h2>
<% end %>
You can specify delimiter for elements between `separator` and `item` block:
<% foreach student in students %>
<% separator %>, <% item %><% student.name %><% end %>
<% end %>
Would generate a list like: "Ron, John, Moshe"
### Description
`foreach` loop creates a for loop that iterates any STL collection. The given name is a reference to
the type returned by iterator. For example, if students is `std::list<student_t>` then `student` in above
example is defined as `student_t &student`.
_Note:_ Template system uses one of following method for automatic type detection:
1. C++0x `auto`
2. C++0x `decltype`
3. GCC `typeof`
4. Boost Typeof
At least one of them should be supported by the compiler in order to provide correct template generation.
`empty` statement is equivalent to "else" so:
<% foreach a in b %>
<ul>
<% item %><% a %><% end %>
</ul>
<% empty %>
nothing
<% end %>
Is generated into code like:
if(!content.b.empty()) {
cout<<"<ul>";
for(auto a_it=content.b.begin();a_it!=content.b.end();++a_it) {
auto &a=*a_it;
cout<<a;
}
cout<<"</ul>";
}else{
cout<<"nothing";
}
You must provide `<% item %>...<% end %>` block for any `foreach` statement.
If `separator` is used it should be added between `separator` and `item` blocks.
### Custom form rendering
CppCMS forms system provides several options for rendering form widgets, but sometimes custom rendering
may be needed. In this case, `foreach widget` statement is provided. In this case, widget is a reference
to `cppcms::widget` and the collection should be `cppcms::form` or `cppcms::widgetset`.
For example, rendering with "new line" separator:
<% foreach widget w in form.my_set %>
<% separator %><br/><% item %>
<% if not empty w.msg %><% w.msg %>:<% end %>
<% form input w %>
<% if not w.is_valid %>:<% form error w %><% end %>
<% end %>
<% end %>

About

CppCMS is a web development framework for performance demanding applications.

Support This Project

SourceForge.net Logo

Поддержать проект

CppCMS needs You


Navigation

Main Page


Valid CSS | Valid XHTML 1.0