This class stores the template string and content type and has a method, eval(), for replacing the escaped variable names, e.g. "%foo%", in the string with the variables' corresponding values, if any. Variables that are not defined are not replaced (resulting in the escaped names remaining in the string).
Note that the exception class has provisions for both exception member data as variables and application-defined variables. The templating mechanism is designed primarily for the automatic creation of error response content from exceptions. Some of the status codes, however, should not produce any output content (see status_code_e documentation).
Variable names include the chars: ASCII alpha upper and lower, digits, underscore, hyphen. A name must begin with either ASCII alpha or underscore. The escape char must immediately precede and follow the name in the template.
Variable names are looked up in the following places, in order:
Example:
#include <restcgi/ctmpl.h> using namespace restcgi; ... // method m is received for /cgi-bin/mycgi/myurl ... ctmpl ct("hello, %foo% from %SCRIPT_URL%!"); ctmpl::map_type map; map.insert(std::make_pair("foo", "world")); std::string out = ct.eval(m->env(), map); // out == "hello, world from /cgi-bin/mycgi/myurl!"
Public Types | |
typedef std::map< std::string, std::string > | map_type |
Public Member Functions | |
ctmpl (const std::string &s, const std::string &content_type="text/plain") | |
Construct from template string and content type. | |
std::string | eval (const env &e, const map_type &map=map_type()) const |
std::string | eval (const map_type &map) const |
const std::string & | content_type () const |
Static Public Member Functions | |
static bool | find (const std::string &name, std::string &value, const env *e, const map_type &map) |
Find variable value for name returning whether found. | |
Static Public Attributes | |
static const char | ESCAPE_CHAR |
variable name escape char ('%') |
typedef std::map<std::string, std::string> restcgi::ctmpl::map_type |
map type
const std::string& restcgi::ctmpl::content_type | ( | ) | const [inline] |
Get content_type.
std::string restcgi::ctmpl::eval | ( | const map_type & | map | ) | const |
Evaluate the constent template string in the context of the map. See class description.
Evaluate the constent template string in the context of the env and map (if any). See class description.