Comments (11)
Hi there,
Originally we did pass a render function much like the mustache.js version but after looking at the spec it defines that any values returned from lambdas should be interpolated and rendered based on the current context.
If you know the string value in your context that is unchanging you could build a string that will then be interpolated. Another option is to use the lambda signature that takes a dynamic
as the first argument which will be the current context however you won't get any strong typing to help you. The result will still be interpolated.
Hopefully that makes more sense but please feel free to ask more questions and i'll do my best to help.
from stubble.
Hi Thanks for you answer,
In Nustache the returned string is not interpolated in the context, i.e. {{something}}
will render alike.
The first simple solution would work when accessing a dictionary with literal values, but in my case Dictionary contains objects.
So this would work with my previous data example :
object FromDictionary( dynamic data, string text )
{
return data.ValuesDictionary[text];
}
{{#Data}}{{#Dy}}ValueDictionnary.{{Value}}{{/Dy}}{{/Data}}
However If I want to access further data it won't work, or I can't see how.
{{#Data}}{{#Dy}}ValueDictionnary.{{Value}}.Far{{/Dy}}{{/Data}}
Last, it will need one lambda per dictionary of the domain.
The more general solution
Well looking at Stubble implementation vs Nustache, and from your answer, this should indeed work :
object MyLambda( dynamic contextView, string text )
{
return "{{{" + stubble.Render(text, contextView) + "}}}";
}
As it seems that, as you said, passed dynamic data contains the current context view :
( From SectionTokenRenderer :
var functionDynamicValue = value as Func<dynamic, string, object>;
var functionStringValue = value as Func<string, object>;
var sectionContent = obj.SectionContent;
value = functionDynamicValue != null
? functionDynamicValue.Invoke(context.View, sectionContent.ToString())
: functionStringValue.Invoke(sectionContent.ToString());
``` renderer.Render(context.RendererSettings.Parser.Parse(value.ToString(), obj.Tags), context);
)
Please feel free to share your thoughts,
Best
from stubble.
I'm not sure about the second solution, it seems to me that we only get the context view, but not the partials.
Moreover, as we get only context view, it will not recurse up to parent context. On this line
functionDynamicValue.Invoke(context.View, sectionContent.ToString())`
Getting the context instead of the context view would let us do whatever we need in the lambda. Like create a new context from this one.
from stubble.
Unfortunately i'm not entirely sure of the best solution here. Giving you only the current context is sensible since you can access anything inside the context without issues.
If you could give me a slimmed down example of what you're trying to achieve and having issues with i'd be happy to try help however this seems like quite an edge case that Stubble may not be able to accommodate without additional complexity.
from stubble.
I'm trying to achieve exactly the example I've shown. That's why I also cited Mustache.js as here they provide a Render function that will allow rendering in lambdas from context/scope/partials. And it does so without giving access to the context and thus it can't be tampered with.
I'd really like to avoid using a JS parser to use Mustach.js capabilities, adding a 3rd lambda function to the 2 you already handle would do the trick.
from stubble.
I'd like to be able to do this use case : https://github.com/janl/mustache.js#functions
object MyLambda( Func<string, string> renderFunc, string text )
{
return "{{{" + renderFunc(text) + "}}}";
}
from stubble.
@Baccanno thank you for your help with this. I've decided that adding this overload is not a large amount of work, it won't have too much overhead for others using the library and will unlock migrations for people from mustache.js
.
I've updated the name of the issue and added this to a milestone. I'll update here when I have more
Thanks,
from stubble.
I'm very pleased to hear this. In my opinion this is what made mustache very powerful when I used it in js projects.
from stubble.
This was just released with 1.3 on Nuget. Thank you @Baccanno for working through this with me. Hopefully this solves your problem.
from stubble.
Took some time to get a feedback on this, but it works perfectly well ! Thank you so much.
from stubble.
@Baccanno Good to hear that it works for you!
from stubble.
Related Issues (20)
- Get output as object rather than string from Render HOT 4
- Render a tag correctly however it is null! HOT 1
- Not compatible with .Net standard 2.1 and .Net Core 3.1 HOT 3
- SetIgnoreCaseOnKeyLookup does not work HOT 2
- An attribute in the template is "User.Name" Parse error HOT 1
- Parent nested object access HOT 2
- Render pipeline is not passed to partials HOT 1
- Execute method with parameter's default value HOT 4
- Add Cake plugin
- Compiled renderer throws exception when compiling interpolated nullable int HOT 1
- Truthiness on a Dictionary<string, object> HOT 5
- PartialTagParser exhaustion side effect?!
- [Question/Discussion] Rework of the Parser to support more information on its output
- Anonymous object property is not retrieved HOT 4
- Please add Extensions Setting Support for system.text.json HOT 1
- How to count number of variables in a template ?
- Non English character problem for placeholders HOT 2
- System.Security.VerificationException: Operation could destabilize the runtime. HOT 2
- Vue-email-editor relies on external iframe source, problematic for intranet environments HOT 1
- Support for tag/variable async lamdas?
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 stubble.