Comments (5)
Here's a self-contained working version of your example:
#include <iostream>
#include <bustache/render/ostream.hpp>
#include <nlohmann/json.hpp>
template<>
struct bustache::impl_model<nlohmann::json> {
impl_model() = delete;
};
template<>
struct bustache::impl_compatible<nlohmann::json> {
static value_ptr get_value_ptr(nlohmann::json const& self) {
nlohmann::json::value_t const kind(self);
switch (kind) {
case nlohmann::json::value_t::boolean:
return value_ptr(self.get_ptr<nlohmann::json::boolean_t const*>());
case nlohmann::json::value_t::number_integer:
return value_ptr(
self.get_ptr<nlohmann::json::number_integer_t const*>());
case nlohmann::json::value_t::number_unsigned:
return value_ptr(
self.get_ptr<nlohmann::json::number_unsigned_t const*>());
case nlohmann::json::value_t::number_float:
return value_ptr(
self.get_ptr<nlohmann::json::number_float_t const*>());
case nlohmann::json::value_t::string:
return value_ptr(self.get_ptr<nlohmann::json::string_t const*>());
case nlohmann::json::value_t::array:
return value_ptr(self.get_ptr<nlohmann::json::array_t const*>());
case nlohmann::json::value_t::object:
return value_ptr(self.get_ptr<nlohmann::json::object_t const*>());
}
return value_ptr();
}
};
struct my_context : std::unordered_map<std::string, bustache::format> {
using unordered_map::unordered_map;
bustache::format const* operator()(std::string const& key) const {
auto it = find(key);
return it == end() ? nullptr : &it->second;
}
};
int main() {
using namespace bustache::literals;
my_context context{{"EMBEDDED_TEMPLATE", ", goodbye {{goodbyeTag}}"_fmt}};
nlohmann::json data;
data["helloTag"] = "Bustache";
data["goodbyeTag"] = "Mustache";
bustache::format format(
"Hello {{helloTag}}{{>EMBEDDED_TEMPLATE}} templating system.\n");
// Prints: Hello Bustache templating system.
std::cout << format(data) << "\n";
// Should print: Hello Bustache, goodbye Mustache templating system.
auto str = format(data).context(context);
std::cout << str
<< '\n'; // 'render_ostream': no matching overloaded function
}
HTH
from bustache.
Are you using some old version? I took your example and it works fine on the current version.
Note that Context Handler is a bit different in new API, see here for example.
from bustache.
I did have a slightly old version and have now installed the latest source. This is how I've been accessing the processed data successfully:
auto str1 = bustache::to_string(format(data));
But this is where I get a compiler error when I try to use partials:
auto str2 = bustache::to_string(format(data).context(context)); // 'render_string': no matching overloaded function found
I looked at your context handler and example link but I'm lost, I just can't figure out the proper syntax to use, please help.
from bustache.
Also note that using a variable to store the "manipulator" is not recommended.
auto str = format(data).context(context); // it's a manipulator, not a string
Albeit it's fine in your example, it'd cause dangling reference if the data
or context
is temporary.
Instead, consume the manipulator directly:
std::cout << format(data).context(context);
auto str = to_string(format(data).context(context));
from bustache.
That worked so nice, thank you so much! Very slick code, I wouldn't have ever figured it out.
from bustache.
Related Issues (20)
- Compilation error with C++17 variant conflict
- Insert a template into another template? HOT 2
- `boost::unordered_map<>` vs `std::unordered_map<>` HOT 1
- Improve CMake package integration HOT 1
- Add line/column info to `format_error` HOT 2
- Bustache RVO HOT 1
- How to search Bustache Object for data? HOT 2
- Search Bustache Object for data - enhansement HOT 2
- thank you and may I have some more, please HOT 2
- Empty string on one case but not a similar other one HOT 1
- Is interface for Bustache C++20 incomplete? HOT 10
- Bustache compiles fine using nlohmann::json but not with nlohmann::ordered_json containers HOT 2
- Is it possible to execute a function when a specific replacement tag is rendered? HOT 1
- Bustache data adapter for Nlohmann Json? HOT 2
- Cannot compile with clang 14 HOT 3
- New kid on the block HOT 4
- Examples using headers from test HOT 1
- Compiling Error HOT 1
- Will latest versions of Bustache compile on gcc 7.4? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bustache.