Comments (5)
I think the best way is to accept a callback that allows you handle the unresolved key yourself, which is more flexible than an option flag.
What do you plan to do with inverted section?
from bustache.
Indeed, a callback is more flexible, I'll see what I can do.
For the inverted section, I plan to also trigger the error/callback if the inverted section name is missing. The idea is really to have no room for typo being sneaked in the template/data. Basically, having the absence of something (variable, section or partial) should trigger an error/callback and not default to an empty/false value. So, one would have to explicitly set an empty list or a false value to trigger an inverted section.
What about the API? Should I add an additional parameter after option_type flag
or refactor the option_type
parameter?
from bustache.
For the inverted section, I plan to also trigger the error/callback if the inverted section name is missing
Hmm, I think this is a common idiom in mustache:
{{#item}}{{field}}{{/item}}
{{^item}}No item to show.{{/item}}
Where the second line is the fallback if item
is absent.
But yes, using custom callback should cover this case as well.
What about the API? Should I add an additional parameter after option_type flag
Yup, just add another parameter, e.g.
struct default_unresolved_handler
{
value operator()(std::string const& /*key*/) const
{
return nullptr;
}
};
template<class Sink, class UnresolvedHandler = default_unresolved_handler>
inline void generate
(
Sink& sink, format const& fmt, value::view const& data,
option_type flag = normal, UnresolvedHandler&& f = {}
);
Another option is adding such a callback in the "model" instead, e.g.
object data
{
{"", // A special unresolved callback
[](std::string const& key)
{
// Custom behavior
}
},
...
};
But I'm not sure whether this is a good idea.
from bustache.
Thanks a lot for the work!
I started to work on it but did not have the time to finish it. Mostly because I didn't know how to handle name misses on partials, which this patch does not handle right?
The issue with partials is that you can't return a value
, to be consistent one should return format
.
I tried to require UnresolvedHandler
to have 3 overloads that would take unresolved ast::variable
, ast::section
and ast::partial
then return value
and format
accordingly. But this seems way to complicated and exposes ast
to the user.
I don't know if you'll see an easy way to do that. I guess one solution would be to use a custom Context
container and do what I want in a custom find
method.
Another thing: this feature is not exposed by the format
class. Does this means I need to use the generate_*
function instead?
Anyway, thanks again for this.
from bustache.
I didn't know how to handle name misses on partials, which this patch does not handle right?
Partials are treated differently than variables/sections, actually bustache already allows custom behavior on lookup. Let's see...
I guess one solution would be to use a custom Context container and do what I want in a custom find method.
Exactly. The context can be any type that satisfied this requirement:
- a member type
value_type
- a member type
iterator
- a member function
iterator find(std::string const& key) const
- a member function
iterator end() const
So, if you want to wrap an existing context and make it throw on unresolved key, you can do this:
template<class Context>
struct strict_context_wrapper
{
using value_type = typename Context::value_type;
using iterator = typename Context::const_iterator;
iterator find(std::string const& key) const
{
auto it = ctx.find(key);
if (it == ctx.end())
throw std::runtime_error("unresolved partial: " + key);
return it;
}
iterator end() const
{
return ctx.end();
}
Context& ctx;
};
template<class Context>
inline strict_context_wrapper<Context> make_strict_context(Context& ctx)
{
return {ctx};
}
See full example here.
this feature is not exposed by the format class. Does this means I need to use the generate_* function instead?
Yes. Bustache is designed to be modular, that means, if one doesn't like the object model and generator, he can just use the format
part of this lib and use his own object and generating function (though it'll be a huge effort). As you can see, the UnresolvedHandler
is coupled with the object model, but format
doesn't depend on the object model by design, so you have to use the generate_* function directly or write some helper functions to ease the use.
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
- Trouble getting Partials (embedded templates) working when using the Nlohmann Json adapter. HOT 5
- 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.