Main  /  Edit version 10  /  Edit version 11  /   /  Users Area

Difference "Templates: Rendering Commands (v 1.x)" ver. 10 versus ver. 11

Content:

<!--toc-->
## Variable
The simplest templates command is showing variable. The syntax is trivial:
VARIABLE
'=' VARIABLE
For example, if you want to show a content value `message` just write:
<% message %>
<%= 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;`
If you want to get a value of some property, you just write.
<% some_property() %>
<%= some_property() %>
Which is translated to:
out()<<cppcms::filters::escape(content.some_property());
## Variable with filters
You can add arbitrary filters to the variable you show. The syntax is following:
VARIABLE [ '|' filter [ '|' filter ] ... ]
'=' 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") %>
<%= 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 %>
<%= 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 %>
<%= ready_html | raw %>
The `ready_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 %>
<%= 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.
## Variable - Deprecated Syntax
CppCMS supports older deprecated syntax (without "="):
VARIABLE [ '|' filter [ '|' filter ] ... ]
However it is not recommended due to fact that new
template keywords may be introduced that would hide the existing variable and cause syntax error.
## 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 of 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.
3. You may use `<% if rtl %>` or `<% if not rtl %>` in order to test direction of text. This is actually equivalent to:
<% 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 templates in place (actually calling other member functions.) You can include existing implementation in parent views
in order to extend them. For example:
<% 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 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 view `sales` with an additional option.
## Rendering Forms
### Syntax
'form' ('as_p' | 'as_table' | 'as_ul' | 'as_dl' | 'as_space' | 'input' | 'begin' | 'end' ) VARIABLE
`as_*` flags allow you to render `cppcms::form`, with
specific delimiters, when `input`, `begin` and `end` flags
allow you to render single part of the widget.
### 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>
Sometimes you want to perform custom rendering
of widgets that is not predefined by this list.
In such case you may render only `<input ...>` field
of the widget by calling
<% form input some_button %>
Which would be rendered like this:
<input type="submit" name="_3" value="Press" />
Note, `input` does not strictly means that it is limited
for widgets using HTML `input` tag, it works for
any other widget just limited to actual content rendering
without "help" messages, for example, for widget
`cppcms::widgets::textarea` it would be `<textarea ...>some text</textarea>`
Sometimes you need even more fine grained access
to widget rendering parts, for example you may
want to add some attributes to the widget in the view.
You may render `begin` or `end` part of the widget, such that you can insert HTML attributes between them or even JavaScript.
<% form begin some_button %> onclick="return foo()" <% form end some_button %>
Which would be rendered as
<input type="submit" name="_3" onclick="return foo()" value="Press" />
For generation of custom HTML for widgets you can
always use widget properties directly:
<input type="submit" name="<% button.name() %>" value="Press" /> ( <% button.help() %> )
See: [`cppcms::widgets` namespace](/cppcms_ref_v0_99_1/namespacecppcms_1_1widgets.html) for documentation of
each specific widget properties.
## 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 %>
<% skin %>
<% view 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++ out()<<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