Skip to content

Commit

Permalink
Add renderer.hpp, render.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Dec 27, 2022
1 parent d1022f5 commit 8651cd6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/boost/mustache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/mustache/mustache.hpp>
#include <boost/mustache/render.hpp>

#endif // #ifndef BOOST_MUSTACHE_HPP_INCLUDED
18 changes: 0 additions & 18 deletions include/boost/mustache/mustache.hpp

This file was deleted.

26 changes: 26 additions & 0 deletions include/boost/mustache/render.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef BOOST_MUSTACHE_RENDER_HPP_INCLUDED
#define BOOST_MUSTACHE_RENDER_HPP_INCLUDED

// Copyright 2022 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/mustache/renderer.hpp>

namespace boost
{
namespace mustache
{

template<class T1 = json::value, class T2 = json::value> void render( core::string_view tmpl, output_ref out, T1 const& data, T2 const& partials, json::storage_ptr sp = {} )
{
mustache::renderer rd( data, partials, sp );

rd.render( tmpl, out );
rd.finish( out );
}

} // namespace mustache
} // namespace boost

#endif // #ifndef BOOST_MUSTACHE_RENDER_HPP_INCLUDED
44 changes: 44 additions & 0 deletions include/boost/mustache/renderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef BOOST_MUSTACHE_RENDERER_HPP_INCLUDED
#define BOOST_MUSTACHE_RENDERER_HPP_INCLUDED

// Copyright 2022 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/mustache/output_ref.hpp>
#include <boost/json/value.hpp>
#include <boost/json/array.hpp>
#include <boost/json/object.hpp>
#include <boost/json/storage_ptr.hpp>
#include <boost/json/value_from.hpp>
#include <boost/core/detail/string_view.hpp>

namespace boost
{
namespace mustache
{

class renderer
{
private:

json::array context_stack_;
json::object partials_;

public:

renderer( json::value const& data, json::value const& partials );

template<class T1, class T2> explicit renderer( T1 const& data, T2 const& partials, json::storage_ptr sp = {} ):
renderer( json::value_from( data, sp ), json::value_from( partials, sp ) )
{
}

void render( core::string_view tmpl, output_ref out );
void finish( output_ref out );
};

} // namespace mustache
} // namespace boost

#endif // #ifndef BOOST_MUSTACHE_RENDERER_HPP_INCLUDED
2 changes: 2 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ run quick.cpp ;

run output_ref.cpp ;
run output_ref2.cpp ;

run render_literal.cpp ;
18 changes: 18 additions & 0 deletions test/render_literal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/mustache/render.hpp>
#include <boost/core/lightweight_test.hpp>
#include <string>

int main()
{
std::string st;

boost::mustache::render( "no-tags", st, {}, {} );

BOOST_TEST_EQ( st, std::string( "no-tags" ) );

return boost::report_errors();
}

0 comments on commit 8651cd6

Please sign in to comment.