Main  /  Edit version 1  /  Edit version 2  /   /  Users Area

Difference "cppcms::base_view & cppcms::base_content" ver. 1 versus ver. 2

Content:

## cppcms::base\_content
This is simple polymorphic class that every content for cppcms template system should be derided. It allows dynamic casting of the content to correct one for target application:
class base_content {
public:
virtual ~base_content() {};
};
## cppcms::base\_view
### Members
- **`worker_thread &worker`** -- reference to worker thread that called rendering procedure.
- **`ostream &cout`** -- reference to output stream that output is rendered to.
-
### Derived classes:
### How Derived Classes are created:
This is the class that every view generated from the template
system is derived from.
This is the class that every view generated from the template system is derived from.
For example, template:
Every derived class defines important public member --- `content` --- the reference to user defined content class.
It also creates member functions for each implemented template. For example:
<% c++ #include "data.h" %>
<% namespace my_view %>
<% class message uses data::message %>
<% template render() %>
<html>
<body>
<h1><% message %> World!</h1>
</body>
<html>
<% end template %>
<% end class %>
<% end namespace %>
Is rendered to C++ output:
#include "data.h"
namespace my_view {
struct message :public cppcms::base_view
{
data::message &content;
cppcms::transtext::trans const *tr;
message(cppcms::base_view::settings _s,data::message &_content):
cppcms::base_view(_s),content(_content)
{
tr=_s.worker->domain_gettext("my_view");
};
virtual void render() {
cout<<"\n"
"<html>\n"
" <body>\n"
" <h1>";
cout<<escape(content.message);
cout<<" World!</h1>\n"
" </body>\n"
"<html>\n"
"";
} // end of template render
}; // end of class message
} // end of namespace my_view
Every template class has base class it derived from -- `base_view`. This is default one.
Every derived class defines important public member --- reference to user defined content class. In our case:
data::message &content;
It also creates member functions for each implemented template.
### Member functions
#### Default filter
template<typename T>
string escape(T const &v)
This is default filter, performs rendering of input parameter to text. This function is always called when template parameter is substituted without filters, i.e.:
<% param %>
is converted to
cout<<escape(content.param);
By default, it uses `ostream` to render the output, however it provides several specializations:
- **`string escape(string const &s)`** -- performs HTML escaping of string.
- **`string escape(std::tm const &v)`** -- converts `std::tm` time to human readable format.
#### Provided Filters
They are called when some filters are given. Each filter has following possible signatures:
string filter_name(Type const &inp);
string filter_name(Type const &inp,string param);
Where `inp` is given filter input and `param` is filter parameter.
- **`string raw(string s)`** -- raw filter, does nothing. It is used to push some html data without escaping. For example:
<% some_html_page | raw %>
- **`string intf(int val,string f)`** -- format integer number. It uses `boost::format` for this purpose. For example:
Decimal <% num %> is hexadecimal <% num | intf("%x"); %>
- **`string strftime(std::tm const &t,string f)`** -- formats output string using std::strftime call. For example:
<% date | strftime("%d/%m/%Y") %>
It has following "shortcuts" for showing, date only, time or time with seconds:
string date(std::tm const &t);
string time(std::tm const &t);
string timesec(std::tm const &t);
- **`string urlencode(string const &s);`** -- encode string for URL. For example:
<a href="http://site/article/<% link | urlencode %>">
#### Other member functions
boost::format format(string const &f);
Function that returns `boost::format` object constructed with `f` and with disabled exceptions. In generally `boost::format` throw exception in case of incorrect format or parameters list --- this is not such a good behavior for displaying various data.
It is used mostly by gettext and ngettext implementations. For example
<% ngt "We have one apple","We have %1% apples",n using n %>
Is rendered to:
cout<<format(tr->gettext("We have one apple","We have %1% apples",n)) % escape(n);
Thus it would not throw in case of "one apple".

Sidebar:

## Releated
- [cppcms::application](/wikipp/en/page/ref_cppcms_application)

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