Code Monkey home page Code Monkey logo

Comments (11)

Romanx avatar Romanx commented on August 26, 2024

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.

Baccanno avatar Baccanno commented on August 26, 2024

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.

Baccanno avatar Baccanno commented on August 26, 2024

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.

Romanx avatar Romanx commented on August 26, 2024

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.

Baccanno avatar Baccanno commented on August 26, 2024

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.

Baccanno avatar Baccanno commented on August 26, 2024

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.

Romanx avatar Romanx commented on August 26, 2024

@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.

Baccanno avatar Baccanno commented on August 26, 2024

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.

Romanx avatar Romanx commented on August 26, 2024

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.

Baccanno avatar Baccanno commented on August 26, 2024

Took some time to get a feedback on this, but it works perfectly well ! Thank you so much.

from stubble.

Romanx avatar Romanx commented on August 26, 2024

@Baccanno Good to hear that it works for you!

from stubble.

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.