rikitiki  v0.1.67
Build C++ web server modules that allow easy routing and deployment.
template_emitter_ext.h
1 /* Copyright (C) 2012-2013 Justin Berger
2  The full license is available in the LICENSE file at the root of this project and is also available at http://opensource.org/licenses/MIT. */
3 #pragma once
4 #include <ctemplate/template.h>
5 #include <sys/types.h>
6 #include <string>
7 #include <sstream>
8 
9 namespace ctemplate {
15  class StringStreamEmitter : public ExpandEmitter {
16  std::stringstream* const outbuf_;
17  public:
18  StringStreamEmitter(std::stringstream* outbuf) : outbuf_(outbuf) {}
19  virtual void Emit(char c) { *outbuf_ << c; }
20  virtual void Emit(const std::string& s) { *outbuf_ << s; }
21  virtual void Emit(const char* s) { *outbuf_ << s; }
22  virtual void Emit(const char* s, size_t slen) { outbuf_->write(s, slen); }
23  };
24 
25  inline bool ExpandTemplate(const TemplateString& filename, Strip strip,
26  const TemplateDictionaryInterface *dictionary,
27  std::stringstream* output) {
28  if(output == NULL) return false;
29  StringStreamEmitter e(output);
30  return mutable_default_template_cache()->ExpandWithData(filename, strip, dictionary, NULL, &e);
31  }
32 }
33 
34