| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>Ctemplate (formerly Google Template) System</title> |
| 5 |
|
| 6 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 7 |
<link href="designstyle.css" type="text/css" rel="stylesheet"> |
| 8 |
<style type="text/css"> |
| 9 |
ol.bluelist li { |
| 10 |
color: #3366ff; |
| 11 |
font-family: sans-serif; |
| 12 |
} |
| 13 |
ol.bluelist li p { |
| 14 |
color: #000; |
| 15 |
font-family: "Times Roman", times, serif; |
| 16 |
} |
| 17 |
ul.blacklist li { |
| 18 |
color: #000; |
| 19 |
font-family: "Times Roman", times, serif; |
| 20 |
} |
| 21 |
</style> |
| 22 |
</head> |
| 23 |
<body> |
| 24 |
|
| 25 |
<h1> <a name="Ctemplate_System"></a>Ctemplate System </h1> |
| 26 |
<center><strong>Status: Current</strong> |
| 27 |
<small>(as of 25 April 2008)</small></center> |
| 28 |
<br> |
| 29 |
|
| 30 |
<p>Welcome to the C++ CTemplate system! (This project was originally |
| 31 |
called Google Templates, due to its origin as the template system used |
| 32 |
for Google search result pages, but now has a more general name |
| 33 |
matching its community-owned nature.)</p> |
| 34 |
|
| 35 |
<p>As a quick start, here's a small but complete program that uses this |
| 36 |
template library. For more details see, the links below.</p> |
| 37 |
|
| 38 |
<h3>Template file <code>example.tpl</code></h3> |
| 39 |
<pre> |
| 40 |
Hello {{NAME}}, |
| 41 |
You have just won ${{VALUE}}! |
| 42 |
{{#IN_CA}}Well, ${{TAXED_VALUE}}, after taxes.{{/IN_CA}} |
| 43 |
</pre> |
| 44 |
|
| 45 |
<h3>C++ program <code>example.cc</code></h3> |
| 46 |
<pre> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <string> |
| 49 |
#include <iostream> |
| 50 |
#include <ctemplate/template.h> |
| 51 |
int main(int argc, char** argv) { |
| 52 |
ctemplate::TemplateDictionary dict("example"); |
| 53 |
dict.SetValue("NAME", "John Smith"); |
| 54 |
int winnings = rand() % 100000; |
| 55 |
dict.SetIntValue("VALUE", winnings); |
| 56 |
dict.SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.83); |
| 57 |
// For now, assume everyone lives in CA. |
| 58 |
// (Try running the program with a 0 here instead!) |
| 59 |
if (1) { |
| 60 |
dict.ShowSection("IN_CA"); |
| 61 |
} |
| 62 |
std::string output; |
| 63 |
ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP, &dict, &output); |
| 64 |
std::cout << output; |
| 65 |
return 0; |
| 66 |
} |
| 67 |
</pre> |
| 68 |
|
| 69 |
<h3>Compiling and linking (using gcc)</h3> |
| 70 |
<pre> |
| 71 |
gcc -o example example.cc -lctemplate_nothreads |
| 72 |
</pre> |
| 73 |
|
| 74 |
<p>I can use the "nothreads" library because <code>example.cc</code> |
| 75 |
doesn't use threads. If <code>example.cc</code> were threaded, I |
| 76 |
would do something like this instead:</p> |
| 77 |
<pre> |
| 78 |
gcc -o example example.cc -lctemplate -pthread |
| 79 |
</pre> |
| 80 |
|
| 81 |
<p>See the README for more details about the two different ctemplate |
| 82 |
libraries.</p> |
| 83 |
|
| 84 |
|
| 85 |
<h2>In-depth Documentation</h2> |
| 86 |
|
| 87 |
<ol> |
| 88 |
<li> <A HREF="howto.html">Howto</A>: Introduction to the |
| 89 |
Ctemplate system, and a tutorial for using it. </li> |
| 90 |
|
| 91 |
<li> <A HREF="auto_escape.html">Auto Escape</A>: Guide to using |
| 92 |
the optional Auto Escape mode to protect your web application |
| 93 |
better against XSS.</li> |
| 94 |
|
| 95 |
<li> <A HREF="tips.html">Tips</A>: Advice, tips, and recommendations |
| 96 |
for best practices with templates, to make them easier to write |
| 97 |
and maintain, and to avoid common template mistakes. </li> |
| 98 |
|
| 99 |
<li> <A HREF="example.html">Examples</A>: Some example templates and |
| 100 |
application code that uses them. These are taken from actual |
| 101 |
Google applications. </li> |
| 102 |
</ol> |
| 103 |
|
| 104 |
<hr> |
| 105 |
<address> |
| 106 |
Craig Silverstein<br> |
| 107 |
Last modified: Mon Feb 22 10:59:03 PST 2010 |
| 108 |
</address> |
| 109 |
|
| 110 |
</body> |
| 111 |
</html> |