Home  /  RSS  /  RSS Comments  /  Enter

Thoughts about template system

8/5/08, by artyom ; Posted in: Templates, Framework; 5 comments

After looking how ASP.Net and J2EE work I thought a lot about current template system.

Today, CppCMS template system is dynamic typed. For example in order to render template:

<% template mycontent %>
<p>You have <% number %> of <% something %></p>
<% end %>

I write something like that (not correct code but idea):

map<boost::any> content; 



The template is compiled to bytecode and than interpreted in rendering engine. If variable title required it checks its type and renders its content.

Another possible approach it to make is statically typed :

So, I create a view interface for template:

struct mycontent: public content {
  string something;
  int number;

And then the above template is compiled to following C++ code:

void mycontent::render()
  cout<<"<p>You have "<<number<<" of "<<escape(something)<<"</p>\n";

That is compiled to shared object that I can load dynamically. And render template as following:

auto_ptr<my_content> content(template.get("my_content"));



  1. Every template should be compiled for each target platform
  2. Dynamic compilation requires availability of g++ on host system
  3. If you create something more then basic (for example add member function that fetch data from DB), than creation of shared object becomes much more complex, specially dll, that does not allow unresolved symbols.


  1. It is much easier to create new types and render them
  2. Easy to extend templates adding native C++ code
  3. The view can fetch data from model and model can push data to view
  4. The inheritance is now much easier to implement and it is native.
  5. Better performance (however not primary reason)

What Do You Think?


Alex, at 8/5/08, 7:46 PM

I like the second approach. The disadvantages you are talking about actually are not really important considering that anyway you should compile the controller source code so you certainly have compiler available. Dynamic template compilation is actually helpful for development only, I would love to see compiled templates statically linked to the controller in a production environment. Please take a look at ctemplate. It is a statically compiled templates for C (without ++) primarily intended to use with apache. The only part missing is dynamic compilation.

artyom, at 8/6/08, 10:12 AM

Hi Alex,

Now I become more and more convinced in using second approach.

BTW: thanks for link to ctemplate, however, for me, it is too much integrated with Apache -- I use server independent API: FastCGI, SCGI or CGI. I think to create some kind of apache_mod_shared_object_cgi however there is still some way to go.

Alex, at 8/6/08, 8:46 PM

Hi Artyom, Actually it is not integrated with apache at all, apache is simple default output mode. Currently ctemplate has 3 options to generate output: apache, CGI and one proprietary server we use internally. I can add a new driver for worker_thread's "out+=" or any other cppcms core interface you prefer.

artyom, at 8/6/08, 9:29 PM

Ok, I think I need to check it out more deeply.

Once more, thanks!

artyom, at 8/6/08, 10:47 PM

Ok, I took a look

  1. No documentaion
  2. CGI mode does not work
  3. The template engine itself too primitive

I had written something like that at the beginning and dropped it.

Sorry, but ctemplate is not the code I'm going to use --- because it does not do the job.

Add Comment:

 the email would not displayed

You can write your messages using Markdown syntax.

You must enable JavaScript in order to post comments.