Giter Site home page Giter Site logo

Comments (5)

jamboree avatar jamboree commented on May 28, 2024 1

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.

jamboree avatar jamboree commented on May 28, 2024

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.

luz-arreola avatar luz-arreola commented on May 28, 2024

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.

jamboree avatar jamboree commented on May 28, 2024

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.

luz-arreola avatar luz-arreola commented on May 28, 2024

That worked so nice, thank you so much! Very slick code, I wouldn't have ever figured it out.

from bustache.

Related Issues (20)

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.