ecpp - template language for tntnet(8)
Contents
Description
ecpp is the template language used by the tntnet system to generate dynamic content.
A template consists of normal content (normally html data) enriched with special tags, which trigger some
special handling.
One ecpp file is compiled into a C++ class. The C++ class is placed into the namespace component. A ecpp
file compiled into a C++ class is called component. The name of the class is the basename of the file.
request, reply, qparam
Each component has 3 parameters: request, reply and qparam. request holds information about the client
request like http headers and the url, but also additional parameters specified in the config file
tntnet.xml(7). The type of request is tnt::HttpRequest.
reply receives the answer from the component. The component can set additional http headers here, set
cookies and - most important - generate output. The most important methods here are reply.out() and
reply.sout(). Both return a std::ostream, which receives the output of the component. reply.sout() has a
filter installed, which translates some characters, with special meanings in html to the corresponding
html entities. The characters are <, >, , " and '. This is useful for printing values from variables to
the html code.
qparam holds the query parameters parsed from GET- or POST-parameters or received from other components.
The type of qparam is tnt::QueryParams. Normally you use a <%args> block to specify the parameters, but
there are special cases, where it is useful to access these directly.
componentaddressing
Each component has a unique name. The name is composed from the class name, the character '@' and the
name of the shared library, it is located. Components can have internal sub components. The name of the
internal sub component is appended to the class name separated by a dot (.).
specialruleforlinefeedsaftera</%something>-tag
A line feed immediately after a closing tag for all <%something> blocks are ignored. Hence blocks
followed immediately one after another does not generate white space in output, which is often
undesirable.
errorhandling
Error handling is done by exception. Tntnet catches all exceptions thrown by components and handles them
properly. Exceptions must be derived from std::exception. Exceptions derived from tnt::HttpError, are
handled separately. They carry a http return code, which is sent to the client. Other exceptions derived
from std::exception, result in a http error code 500 (Internal Server Error).
Name
ecpp - template language for tntnet(8)
Scoped Variables
Scoped variables are c++ variables, whose lifetime is handled by tntnet. These variables has a lifetime
and a scope. The lifetime is defined by the tag, used to declare the variable and the scope is passed as
a parameter to the tag.
There are 5 different lifetimes for scoped variables:
request
The variable is valid in the current request. The tag is <%request>.
application
The variable is valid in the application. The tag is <%application>. The
application is specified by the shared library of the top level component.
session
The variable is valid for the current session. The tag is <%session>. If at
least session variable is declared in the current request, a session cookie is
sent to the client.
thread
The variable is valid in the current thread. The tag is <%thread>.
param
The variable receives parameters. The tag is <%param>.
And 3 scopes:
component
The variable is only valid in the same component. This is the default scope.
page
The variable is shared between the components in a single ecpp file. You can
specify multiple internal sub components in a <%def> block. Variables,
defined in page scope are shared between these sub components.
global or shared
Variables are shared between all components. If you define the same variable
with shared scope in different components, they must have the same type. This
is achieved most easily defining them in a separate file and include them
with a <%include> block. The global and shared are just synonyms.
Variables are automatically locked as needed. If you use session variables,
tntnet ensures, that all requests of the same session are serialized. If you
use application variables, tntnet serializes all requests to the same
application scope. Request and thread scope variables do not need to be
locked at all, because they are not shared between threads.
Syntaxofscopedvariables
Scoped variables are declared with exactly the same syntax as normal variables in c++ code. They can be
of any type and are instantiated, when needed. Objects, which do not have default constructors, need to
be specified with proper constructor parameters in brackets or separated by '='. The parameters are only
used, if the variable need to be instantiated. This means, that parameters to e.g. application scope
variables are only used once. When the same component is called later in the same or another request, the
parameters are not used any more.
Examples
Specify a application specific shared variable, which is initialized with 0:
<%application>
unsigned count = 0;
</%application>
Specify a variable with a user defined type, which holds the state of the session:
<%session>
MyClass sessionState;
</%session>
Specify a persistent database connection, which is initialized, when first needed and hold for the
lifetime of the current thread. This variable may be used in other components:
<%thread scope="shared">
tntdb::Connection conn(dburl);
</%thread>
See Also
tntnet(8), ecppc(1)
Tntnet 2006-07-23 ecpp(7)
