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

Difference "Templates: Flow Control (v 1.x)" ver. 2 versus ver. 3

Content:

<!--toc-->
## 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 variable or property 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.
## Foreach block
### Sytnax
Major loop:
'foreach' ['widget'] NAME 'in' VARIABLE
'foreach' ['as' IDENTIFIER ] 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:
#### Type detection
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
3. GCC's style `typeof` or `__typeof__`
At least one of them should be supported by the compiler in order to provide correct template generation.
At least one of them should be supported by the compiler in order to provide correct template generation without
using `as` keyword.
But some compilers, most noticeable is MSVC8, MSVC9 still
not support automatic type detection, thus, specific
iterator type should be explicitly specified.
For example:
<% foreach student as students_type::iterator in students %>
Would give you correct solution for MSVC compiler and `student` would a variable of type `students_type::iterator::value_type`
#### Body
`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>";
out()<<"<ul>";
for(auto a_it=content.b.begin();a_it!=content.b.end();++a_it) {
auto &a=*a_it;
cout<<a;
out()<<a;
}
cout<<"</ul>";
out()<<"</ul>";
}else{
cout<<"nothing";
out()<<"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`.
may be needed. In this case, `foreach` can be used to iterate over `cppcms::form` as over ordinary collation.
For example, rendering with "new line" separator:
<% foreach widget w in form.my_set %>
<% foreach w in form %>
<% separator %><br/><% item %>
<% if not empty w.msg %><% w.msg %>:<% end %>
<% if w.has_message() %><% w.message() %>:<% end %>
<% form input w %>
<% if not w.is_valid %>:<% form error w %><% end %>
<% if not w.valid() %>:<% w.error_message() %><% 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