Thoughts about template system
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;
content["something"]=string("orange");
content["number"]=10;
template.render(content,output);
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"));
content->number=10;
content->something="orange";
content->render(output);
Disadvantages:
- Every template should be compiled for each target platform
- Dynamic compilation requires availability of g++ on host system
- 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.
Advantages:
- It is much easier to create new types and render them
- Easy to extend templates adding native C++ code
- The view can fetch data from model and model can push data to view
- The inheritance is now much easier to implement and it is native.
- Better performance (however not primary reason)
What Do You Think?
Comments
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.
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.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.
Ok, I think I need to check it out more deeply.
Once more, thanks!
Ok, I took a look
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:
You must enable JavaScript in order to post comments.