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

Difference "Templates: Rendering Commands (v 1.x)" ver. 2 versus ver. 3

Content:

<!--toc-->
## Variable
The simplest templates command is showing variable. The syntax is trivial:
NAME
For example, if you want to show a content value `message` just write:
<% message %>
It is actually translated to C++ code:
out()<<cppcms::filters::escape(content.message);
Where `escape` function writes the `message` member variable
of the content to the stream and escapes special HTML
characters `<`, `>`, `&`, `"` with their HTML representations like `&lt;`
## Variable with filters
You can add arbitrary filters to the variable you show. The syntax is following:
VARIABLE [ '|' filter [ '|' filter ] ... ]
Where `filter` is
[ 'ext' ] NAME [ '(' ( VARIABLE | STRING | NUMBER ) [ ',' ( VARIABLE | STRING | NUMBER ) ] ... ]
### Description
If no filters, are given the variable is automatically escaped, but if you provide any filter they are used instead
of escaping.
Each variable is actually a creation or call
of special filter object with given parameters,
where first parameter is the "filtered" variable and
others are filter parameters. For example:
<% birthday | strftime("%d/%m/%Y") %>
Is translated into:
out()<<cppcms::filters::strftime(content.birthday,"%d/%m/%Y");
You can also concatenate filters using pipe symbol:
<% birthday | strftime("%d %m %Y") | urlencode %>
Would be rendered as:
out()<<cppcms::filters::urlencode(cppcms::filters::strftime(content.birthday,"%d/%m/%Y"));
There is a set of filters predefined available for you, see [cppcms::filters](/cppcms_ref_v0_99_1/namespacecppcms_1_1filters.html) namespace reference.
One of the filters that are worth to notice is `raw` filter
that allows write an output variable as-is without
HTML escaping.
<% ready_html | raw %>
The `read_html` variable would be written as-is to output
stream without escaping HTML symbols like `<`.
You can specify arbitrary external filters that are defined in `content` class. For example:
namespace data {
struct page {
...
virtual string bbcode(string const &);
...
};
}
Then in template we can specify:
<% class page uses data::page %>
<% template render() %>
<% message | ext bbcode %>
<% end %>
<% end %>
__Note:__ Under Windows platform, dynamically loaded libraries (dll) do not allow undefined symbols.
Thus, if you want to support Windows, you should create your filters as virtual functions or
callbacks like `booster::function<>` in order to prevent referring to external symbol.
## Internationalization
### Syntax
'gt' STRING [ 'using' using-options ]
'ngt' STRING ',' STRING ',' VARIABLE [ 'using' using-options
Where `using-options` is:
display-variable-block [ ',' display-variable-block [ ',' ... ] ]
Where `display-variable-block` is any variable is optional filters as described above. For example:
<% gt "Press the button" %>
<% gt "Hello Dear {1}, we are glad to see you!" using name %>
<% gt "Hello <a href=\"{1}\">{2}</a> you have {3}" using link | urlencode , name , something %>
<% ngt "You have one apple","You have {2} apples",n using n %>
### Description
`gt` -- `gettext` translates given string according to locale defined in the output stream. For example:
<% gt "Press the button" %>
Would be translated as "Press the button" in English locale and "Нажмите кнопку" under Russian locale.
You can specify additional parameters using `using` option. You provide a list of comma separated values
as you would display a variable. You can use any other filters as usual. "{1}", "{2}" etc, specify the order of
input variables. It uses [`booster::locale::format`](/cppcms_ref_v0_99_1/classbooster_1_1locale_1_1basic__format.html) or [`cppcms::locale::format`](/cppcms_ref_v0_99_1/classcppcms_1_1locale_1_1basic__format.html) for such substitutions (according to localization backend
CppCMS compiled with.)
`ngt` -- `ngettext` translate plural form for given integer. Where first parameter is single form, second plural and the
third should be the variable is used.
_Notes:_
1. When you use `ngt` you encouraged to use `using` syntax, because you should provide an actual number to display.
2. Exceptions are disables for boost::format in context of `gettext`. So you may provide not matching number of parameters -- this is important for plural `ngettext` forms.
3. You may use `<% if rtl %>` or `<% if not rtl %>` in order to test direction of text. This is actually equivalent to:
<% if (strcmp(gettext("LTR"),"RTL")==0) %>
<% if (cppcms::locale::gettext("LTR",out().getloc())=="RTL") %>
See `if` statement for further description.
## Including other templates
### Syntax
'include' IDENTIFIER '(' [ parameter [ ',' parameter ...] ] ')'
Where `parameter` is one of VARIABLE, STRING or NUMBER. For example:
<% include title() %>
<% include parent::foo() %>
<% include bar(x,"test",-1.0) %>
### Description
This command allows inclusion of other functions in place. You can include existing implementation in parent templates
This command allows inclusion of other templates in place (actually calling other member functions.) You can include existing implementation in parent views
in order to extend them. For example:
<% class master uses data::master %>
<% view master uses data::master %>
<% template menu() %>
<li><a href="#1">Main</a></li>
<li><a href="#2">Products</a></li>
<% end template %>
<% template render() %>
<ul><% include menu() %></ul>
<% end template %>
<% end class %>
<% class sales uses data::sales extends master %>
<% end view %>
<% view sales uses data::sales extends master %>
<% template menu() %>
<% include master::menu() %>
<li><a href="#3">Orders</a></li>
<% end %>
<% end %>
The above code extends menu for class `sales` with an additional option.
The above code extends menu for view `sales` with an additional option.
## Rendering Forms
### Syntax
'form' ('as_p' | 'as_table' | 'as_ul' | 'as_dl' | 'as_space' ) ['no' 'error'] VARIABLE
'form' ('as_p' | 'as_table' | 'as_ul' | 'as_dl' | 'as_space' | 'input' | 'begin' | 'end' ) VARIABLE
Render `cppcms::form`, `widget` or `cppcms::widgetset` with selected delimiter. `no error` disables rendering of error
marks for invalidated form fields.
'form' 'error' VARIABLE
Render widget error message. Used for custom rendering method for widgets.
'form' 'input' VARIABLE
Render HTML input of given widget. Used for custom rendering of widgets.
### Description
Forms are organized as form object or sets of widgets. Each set can be rendered using different delimiters:
- `as_p` -- use HTML paragraphs.
- `as_table` -- use table. _Note_: `<table>...</table>` tags are not added user should specify them.
- `as_ul` -- as list (using `<li>..</li>`). `<ul>..</ul>` are not specified.
- `as_dl` -- as definition list. `<dl>` tags are not specified, `<dt>..</dt>` and `<dd>..</dd>` are used.
- `as_space` -- put blanks between elements.
For example:
<table>
<% form as_table form.fields %>
</table>
<p><% form as_space form.submit_buttons %></p>
May be rendered as:
<table>
<tr><th>User Name:</th><td><input ... /></td></tr>
<tr><th>Password: </th><td><input ... /></td></tr>
</table>
<p><input ... value="Login" /> <input ... value="Forgot Password" /></p>
Usually when certain field is invalid, error message (or '*') is displayed near problematic widget. If you
want provide your own messages at your own position, you may specify `no error` and disable automatic error
messages generation.
You may also render each specific widget in form as you want. You can render only its input filed using:
<% form input some_widget %>
You may generate only error message using:
<% form error some_widget %>
## Selecting HTML/XHTML
You may request generation HTML or XHTML code in forms using following command:
( 'xhtml' | 'html' )
Note, this command has global effect on all template without connection to specific class. You should include it
in the topmost class of your view.
<% c++ #include "all_data.h" %>
<% xhtml %>
<% namespace vary %>
<% class master uses data::master %>
<% template render() %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Currently, this option affects only forms code generation.
## Injecting C++ Code
You can always inject any kind of C++ code using:
"c++" any-c++-statement
For example:
<% c++ #include "all_content.h" %>
__Notes:__
1. If you want to refer to content variables in your code, do not forget to use `content` member of your class. For example:
<% a %> + <% b %> = <% c++ cout<<content.a+content.b; %>
2. Generally you should not use C++ code in template, use it if it is absolutely necessary.
3. If you want use special conditions prefer specialized if statement over `<% c++ if() { %>...<% c++ } %>`

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