Esempio n. 1
0
/**
 * @brief method to render sections with a given map
 *
 * @param tmplate template std::string to render
 * @param ctx map of values
 *
 * @return rendered std::string
 */
std::string template_t::render_sections(const std::string& tmplate,
                                        const Context& ctx)
{
    // initialize data structures
    std::string ret = "";
    std::string rest = "";
    std::string::const_iterator start, end;
    boost::match_results<std::string::const_iterator> matches;
    start = tmplate.begin();
    end = tmplate.end();
    // return the whole template if no sections are found
    if (!boost::regex_search(start, end, matches, section,
                             boost::match_default | boost::format_all))
    {
        ret = tmplate;
    }
    // loop through sections and render
    while (boost::regex_search(start, end, matches, section,
                               boost::match_default | boost::format_all))
    {
        // std::string assignments
        std::string text(start, matches[0].second);
        std::string key(matches[2].first, matches[2].second);
        std::string modifier(matches[1]);
        // trimming
        boost::algorithm::trim(key);
        boost::algorithm::trim(modifier);
        std::string repl = "";
        std::string show = "false";
        CollectionType values;
        values = ctx.get(key);
        if (values.size() == 1)
        {
            // if we don't have a collection, we find the key and an
            // empty map bucket means false
            if (values[0].find(key) != values[0].end())
            {
              show = values[0][key] != "" ? values[0][key] : "false";
            }
            // if we have a collection, we want to show it if there is
            // something to show
            else
            {
              show = values[0].size() > 0 ? "true" : "false";
            }
        }
        else if(values.size() > 1)
        {
            show = "true";
        }
        // inverted section?
        if (modifier == "^" && show == "false") show = "true";
        else if (modifier == "^" && show == "true") show = "false";
        // assign replacement content
        if (show == "true")
        {
            if (boost::regex_search(matches[3].first, matches[3].second, section,
                                    boost::match_default | boost::format_all))
            {
                repl.assign(template_t::render_sections(matches[3], ctx));
            }
            else
            {
                for(CollectionType::iterator it = values.begin();
                    it != values.end(); ++it)
                {
                  Context small_ctx;
                  small_ctx = ctx;
                  small_ctx.add(*it);
                  repl += template_t::render_tags(matches[3], small_ctx);
                }
            }
        }
        else repl.assign("");
        ret += boost::regex_replace(text, section, repl,
                                    boost::match_default | boost::format_all);
        rest.assign(matches[0].second, end);
        start = matches[0].second;
    }
    // append and return
    ret += rest;
    return ret;
}