GithubHelp home page GithubHelp logo

mrtazz / plustache Goto Github PK

View Code? Open in Web Editor NEW
203.0 17.0 46.0 1.84 MB

{{mustaches}} for C++

Home Page: mustache.github.com

License: MIT License

C++ 90.31% Shell 0.30% Makefile 6.38% M4 1.89% Batchfile 1.12%

plustache's Introduction

plustache - mustache templates for C++

Build Status Coverage Status Packagecloud MIT license

Basic port of mustache templating to C++.

Motivation

I just wanted to port mustache and build simple templating for C++. And I am still trying hard to keep it simple.

Usage

Simple Usage

Create a template:

<h1>{{title}}</h1>
Hi I am {{name}}.
I like {{thing}}.

Fill the context:

#include <string>
#include <plustache/plustache_types.hpp>
#include <plustache/template.hpp>

using std::string;
using PlustacheTypes::ObjectType;
using Plustache::template_t;

ObjectType ctx;
ctx["title"] = "About";
ctx["name"] = "Daniel";
ctx["thing"] = "turtles";

Instantiate template class and render the template:

template_t t;
string template("<h1>{{title}}</h1>\nHi I am {{name}}.\nI like {{thing}}.");

string result = t.render(template, ctx);

Result:

<h1>About</h1>
Hi I am Daniel.
I like turtles.

Advanced Usage

Create the template:

<h1> {{title}} </h1>
<ul>
    {{# friends}}
      <li> {{name}}</li>
      <li> {{job}}</li>
      <li> {{status}}</li>
    {{/ friends}}
</ul>

Create the context:

// create types
context ctx;
CollectionType c;
ObjectType jim;
ObjectType john;
ObjectType jack;
// Fill values
ctx.add("title", "My friends");
jim["name"] = "Jim";
jim["job"] = "Wizard";
jim["status"] = "Eating";
john["name"] = "John";
john["job"] = "Rainbow Painter";
john["status"] = "Sleeping";
jack["name"] = "Jack";
jack["job"] = "Unicorn Trainer";
jack["status"] = "Riding";
// enter data
c.push_back(jim);
c.push_back(john);
ctx.add("friends", c);
// also possible
ctx.add("friends", jack);

Render the template:

template_t t;
string result = t.render(template, ctx);

Installation

There are packages available for some Linux distributions on Packagecloud.

Otherwise clone this repository and run the manual install task:

git clone git://github.com/mrtazz/plustache.git
autoreconf -i
./configure
make
make install

On OSX you can get it via my homebrew tap:

brew tap mrtazz/oss
brew install plustache

Running the unit tests

Build the google test library:

cd vendor/gtest-1.7.0
./configure
make

On OS X, you may get an error:

vendor/gtest-1.7.0/include/gtest/internal/gtest-port.h:499:13: fatal error:
'tr1/tuple' file not found
#   include <tr1/tuple>  // NOLINT

If so, re-run configure with the following argument

./configure CPPFLAGS=-DGTEST_USE_OWN_TR1_TUPLE=1
make

Build the test program

Run this from the top-level plustache source directory:

make test-program

If you get the tr1/tuple error, do:

./configure CPPFLAGS=-DGTEST_USE_OWN_TR1_TUPLE=1
make test-program

Run the test program

./test-program

Building with Microsoft Visual Studio

The supplied MSBuild files will look for an installation of boost in the parent directory of this repo. The boost regex library must be built. For example:

cd boost_1_55_0
bootstrap
b2 -j8 toolset=msvc-12.0 address-model=64 --with-regex --prefix=../boost install

The boost directories and other build parameters can be configured by creating a Directories.targets file.

The gtest project files are generated by cmake. Use a command like this to recreate the gtest project files:

cd msvc/x64/msvc120/gtest
cmake -G "Visual Studio 12 Win64" ../../../../vendor/gtest-1.6.0 -Dgtest_force_shared_crt=ON

Supported Functionality (as described in the man page)

  • Variables
  • Sections
    • False Values/Empty Lists
    • Non-Empty-Lists
  • Inverted Sections
  • Comments
  • Partials
  • Set Delimiter
  • HTML escape syntax (triple mustaches)

TODO

  • plustache executable

Dependencies

  • boost for regex and some other things
  • google test for unit testing (included)

plustache's People

Contributors

chesles avatar de-vri-es avatar g12mcgov avatar joshuanapoli avatar mrtazz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plustache's Issues

spec compliance

the implementation should already be pretty compliant, but this has to be tested and completed.

Allow to disable html escape

To use this library to generate simple ascii files (not html files) it would be better to have a flag like
template.html_escape = true; // default
-> &quot;sample&quot;
template.html_escape = false
-> "sample"

Thanks a lot for your library

Nested iterators

It appears that nested iterators are not fully supported. I want to do something like the below mock up, but that doesn't seem possible given that ObjectTypes cannot have CollectionTypes or ObjectTypes as entries.

{
  "families" : [
        {
          "surname": "Jones",
          "members": [
            {"given": "Jim"},
            {"given": "John"},
            {"given": "Jill"}
          ]
        },
        {
          "surname": "Smith",
          "members": [
            {"given": "Steve"},
            {"given": "Sally"}
          ]
        }
      ]
}

<ul>
    {{#families}}
    <li>{{surname}}
      <ul>
        {{#members}}
        <li>{{given}}</li>
        {{/members}}
      </ul>
    </li>
    {{/families}}
</ul>

Round bracket in substitution text disapears

When substituting an non-html escaped piece of text that has a round bracket, the round bracket does not make it into the rendered output.

Boost regex replace is eating the character and it cant be escaped because plustache escapes the regex escape character.

My quick fix was to escape both brackets at the sample place where the \ is getting escaped. (line 110ish of template.cpp on the current version)

This happens on BCC32 inside Rad Studio XE7.

Not sure if I am overlooking an obvious way around this?

Cheers

Build error: uninitialized constant DYNFLAGS

I'm trying to build plustache on Ubuntu 11.10 64-bit. I am getting a "uninitialized constant DYNFLAGS" build error. I have included build output.

I am using the following command: rake --trace install prefix=../zulu

(in /home/jmckinzie/Development/plustache)
g++ -M -MF .depend.mf -I . -I/usr/local/include -I/usr/include src/template.cpp src/context.cpp tests/test_change_delimiter.cpp tests/test_simple_plustache.cpp tests/test_html_escape_plustache.cpp tests/tests.cpp tests/test_nested_sections_plustache.cpp tests/test_sections_plustache.cpp tests/test_multiple_plustache.cpp tests/test_partials_plustache.cpp tests/test_inverted_sections.cpp tests/test_collections_plustache.cpp
mkdir -p build
g++ -O3 -Wall -fPIC -I . -I /usr/local/include -I /usr/include -c src/template.cpp -o build/template.o
g++ -O3 -Wall -fPIC -I . -I /usr/local/include -I /usr/include -c src/context.cpp -o build/context.o
rake aborted!
uninitialized constant DYNFLAGS
/home/jmckinzie/Development/plustache/rakefile:133
(See full trace by running task with --trace)
jmckinzie@jmckinzie:~/Development/plustache$ rake --trace install prefix=../zulu
(in /home/jmckinzie/Development/plustache)
** Invoke .depend.mf (first_time, not_needed)
** Invoke install (first_time)
** Invoke dynamic (first_time)
** Invoke  (first_time)
** Invoke lib (first_time, not_needed)
** Invoke build/template.o (first_time, not_needed)
** Invoke src/template.cpp (first_time, not_needed)
** Invoke build (first_time, not_needed)
** Invoke build/context.o (first_time, not_needed)
** Invoke src/context.cpp (first_time, not_needed)
** Invoke build (not_needed)
** Execute 
rake aborted!
uninitialized constant DYNFLAGS
/usr/lib/ruby/1.8/rake.rb:2503:in `const_missing'
/home/jmckinzie/Development/plustache/rakefile:133
/usr/lib/ruby/1.8/rake.rb:634:in `call'
/usr/lib/ruby/1.8/rake.rb:634:in `execute'
/usr/lib/ruby/1.8/rake.rb:631:in `each'
/usr/lib/ruby/1.8/rake.rb:631:in `execute'
/usr/lib/ruby/1.8/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:607:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:604:in `each'
/usr/lib/ruby/1.8/rake.rb:604:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:596:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:607:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:604:in `each'
/usr/lib/ruby/1.8/rake.rb:604:in `invoke_prerequisites'
/usr/lib/ruby/1.8/rake.rb:596:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:583:in `invoke'
/usr/lib/ruby/1.8/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2029:in `each'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:2023:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2001:in `run'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:1998:in `run'
/usr/bin/rake:28

fatal error: 'include/plustache_types.hpp' file not found

I installed plustache via homebrew and tried to build an example app:

#include <iostream>
#include <plustache_types.hpp>
#include <string>
#include <template.hpp>

using std::cout;
using std::string;
using PlustacheTypes::ObjectType;
using Plustache::template_t;

int main(int argc, char *argv[])
{
    ObjectType ctx;
    ctx["title"] = "About";
    ctx["name"] = "Daniel";
    ctx["thing"] = "turtles";
    template_t t;
    string template("<h1>{{title}}</h1>\nHi I am {{name}}.\nI like {{thing}}.");
    string result = t.render(template, ctx);
    cout << result;
    return 0;
}

When I try to compile with clang++ -o hello hello.cpp, I get:

In file included from hello.cpp:4:
/usr/local/include/template.hpp:14:10: fatal error: 'include/plustache_types.hpp' file not found
#include "include/plustache_types.hpp"

It looks like context.hpp and template.hpp contain local include directives that don't resolve properly since those files are all in /usr/local/include:

context.hpp:#include "include/plustache_types.hpp"
template.hpp:#include "include/plustache_types.hpp"
template.hpp:#include "include/context.hpp"

How about a namespace?

To facilitate easy integration, you may want to consider putting all classes into a namespace s.a. "plustache", just like boost, STL and others. I am already inventing one for my own use to resolve an ambiguity with "Context".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.