Google Closure Templates' C++ adapter.
It calls the official java interpreter from a jar ball through java jni interface.
- Download soy closure-templates source files:
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://closure-templates.googlecode.com/svn/trunk/ closure-templates-read-only
- Modify build.xml to pack the template files, global parameters file and SoyHandler.java with closure-templates' stuff into the final jar ball.
- Modify three macros in
src/cpp/soy_handler.h
:
MAPX_SOY_JAR_FILE
MAPX_RESOURCE_NAMESPACE
MAPX_SOYHANDLER_IN_JAR
- Use
SoyHandler::render()
to render a template of tpl_name with data json_map and locale. Then useSoyHandler::ngx_str()
to fetch the result string in a special object (it's nginx's inner string type). The string is available until the next render call. No need to release the result string's memory.
Q: Why return an nginx string object rather than a null ended string?
There is no end_of_string character like C in Java. By returning an nginx string we make zero copy of the result string.
Q: Why do you have these large functions kept in .h file, not in .cpp file? It's not good to inline large function.
Just for convenience. A modern c++ compile can tell whether to inline a function and make a good choice.
Q: Will you write a native c++ templates interpreter?
It depends. I wish to write it, too.
Q: The code is ugly.
Indeed.