jdiamond / Nustache

Logic-less templates for .NET
MIT License
596 stars 125 forks source link

Where are lambda's renderer and context gone in new syntax ? #127

Closed Baccanno closed 7 years ago

Baccanno commented 7 years ago

In reference of : https://github.com/jdiamond/Nustache/pull/80

We are not able to use the current renderer and thus context in new Lambda ? Or I may not be able to find how as they aren't any documentation

We should be able to do a (Lambda<string, object>)((body, rendererWithContext ) => rendererWithContext .StringToString(body)

Because for now (Lambda<string, object>)((body) => Render.StringToString(body, new { })

Does take into account current context. Thanks !

Romanx commented 7 years ago

Hi there,

I've not really looked at this in awhile so i'm likely rusty. Looking at the tests it's got a quite good example:

var result = Render.StringToString("{{#wrapped}}{{name}} is awesome.{{/wrapped}}", new
{
    wrapped = (Lambda)((body, context, render) => string.Format("<b>{0}</b>", render(context))),
    name = "Lukasz"
});
Assert.AreEqual("<b>Lukasz is awesome.</b>", result);

Could you perhaps post an example of what you're doing and I can try help you debug? As an alternate i'm working on a successor to Nustache called Stubble if you'd forgive the plug. This library follows the mustache spec much more explicitly in that all returns from lambdas are rendered without any interaction from the caller.

Baccanno commented 7 years ago

This syntax with (body, context, render) was available in 14.X but removed with the new syntax in 15.X Lambda is now either delegate< TResult > or delegate<T, TResult>

I'll try Stubble then ! I'm really more into mustache than handlebar or anything, the spec is really self sufficient and for me addons adds complexity because people doesn't know how to use the paradigm.

Maybe you could notify mustache website manager, it is still pointing to Nustache. I'll try this one right away.

My aim is to do a prerender, it was working till then by adding again all the data to render.StringToString (when I made my i18n fwk with mustache.js it was using lambda without having to put data back in the context), but it can't handle recursivity

//Is null after 2nd recursive call is encountered because can't specify Dy function to itself
var l_Dy = (Lambda<string, object>)((body) => "{{" + Render.StringToString(body, new { Lang = m_Dictionnary, Data = data }) + "}}");
Baccanno commented 7 years ago

I do remember now, Unity3D does not support the .net version that uses stubble (Unity is ~C# 3.5 compliant, no string interpolation, older Linq, etc ... )

So being able to use Nustache is only solution or having to change code to compile against C#3.5.

Baccanno commented 7 years ago

I though of using helpers to mimic Lambda but it does not seems to work.

Baccanno commented 7 years ago

Using latest 14.X the following works, but is both very slow and ugly

    private static readonly List<string> ContextVariableList = new List<string>{"Dy", "Lang", "Data", "Sk", "SkBase", "SkCol", "SkDesc", "Skill", "Skills"};

        private IDictionary<string, object> GetDataFromCtx(RenderContext context)
        {
            return ContextVariableList
                .Select(l => new {key = l, data = context.GetValue(l)})
                .ToDictionary(l => l.key, l => l.data);
        }

        private void CreateData(object data)
        {
           Lambda l_Dy = (body, context, render) =>
            {
                var l_Data = GetDataFromCtx(context);
                return Render.StringToString("{{" + Render.StringToString(body, l_Data) + "}}", l_Data);
            };

I'd rather be able to use Stubble indeed. Do you know if there is a way of compiling Stubble to 3.5 without having to refactor everything ?

Romanx commented 7 years ago

Hi @Baccanno

I'm glad you found a way to make it work but I agree that's not a great solution. Stubble currently won't compile to 3.5 as I use dynamic to make Lambdas easier (for the user) I may be able to look at the option of allowing a Dictionary<string, object> context lookup.

If you'd like to submit a pull request bringing back the 1.14.* logic i'd be happy to take a look in the meantime?

Baccanno commented 7 years ago

I've manage to have a Stubble fork that compiles and work with Unity3D,

I've added back a lambda with more arguments, (But I lost everything because of wrong manipulation ...)

It was not a big work so I'll do it again.

But, There was an issue, sometimes compiling a lambda expression in the Registry class (line 319) causes an access violation error and crashe everything. It was repeatable but I couldn't easily debug it. I'll keep you informed. And in the meantime Unity3D will probably have support for c#7 anyway so it'll ease the port and probably get the error off.

For now I'm back with Nustache 1.14.x hack slow but working.

2017-04-18 15:13 GMT+02:00 Alex McAuliffe notifications@github.com:

Hi @Baccanno https://github.com/Baccanno

I'm glad you found a way to make it work but I agree that's not a great solution. Stubble currently won't compile to 3.5 as I use dynamic to make Lambdas easier (for the user) I may be able to look at the option of allowing a Dictionary<string, object> context lookup.

If you'd like to submit a pull request bringing back the 1.14.* logic i'd be happy to take a look in the meantime?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jdiamond/Nustache/issues/127#issuecomment-294838256, or mute the thread https://github.com/notifications/unsubscribe-auth/ADfo0GzUF_GLGW-86CUFy9dbaGPCM0UFks5rxLcXgaJpZM4M6szc .

-- Michaël Picovschi, Morphiks http://www.morphiks.com

Romanx commented 7 years ago

@Baccanno i'd hold off with your fork for the moment as i'm currently rewriting the underlying parsing structure. You could look at the work being done in the parser-overhaul branch to see what's changing but i'll have a comprehensive changelog and migration guide when I do. Hopefully shouldn't be much. For stubble issues feel free to open an issue over there and discuss it rather than clog Nustaches issues.

I'm going to close this now as you have a working solution (albeit a slow hack) and Unity3D is getting modern dotnet support (Yay!)