Main  /  Edit  /  History  /   /  Users Area

Templates: Flow Control

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:

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