Giter VIP home page Giter VIP logo

Comments (15)

SimonCropp avatar SimonCropp commented on July 19, 2024
public class MyHandleBars : Handlebars
{
    public override string SerializeObject(object context)
    {
        return JsonConvert.SerializeObject(context);
    }
}
[Test]
public void CanUseJArrayValues()
{
    var jObject = new JObject { { "County", new JArray { "Kane", "Salt Lake" } } };

    string actual;

    using (var handleBars = new MyHandleBars())
    {
        handleBars.RegisterTemplate("county", "{{County.length}}");
        actual = handleBars.Transform("county", jObject);
    }

    Assert.That(actual, Is.EqualTo("2\r\n"));
}

from chevron.

steveoh avatar steveoh commented on July 19, 2024

Thanks that worked for that case. But my actual case is still failing. It's a bit more complex but it looks like

            [Test]
            public void CanRenderTemplate()
            {
                const string template = "### Hello {{User.FullName}},\n\n **Good News!** " +
                                        "You have been granted permission to [login]({{Config.BaseUrl}}) " +
                                        "to the {{Config.Description}}! " +
                                        "{{#if User.AccessRules.HasRestrictions}}You have access to data " +
                                        "in {{#each User.AccessRules.Options.county}}{{#if @first}}" +
                                        "{{this}}{{else}}{{#if @last}} and {{this}}{{else}}, {{this}}" +
                                        "{{/if}}{{/if}}{{/each}} until {{User.AccessRules.PrettyEndDate}}." +
                                        "{{else}} {{/if}}\n\n You can access the {{Config.Description}} at " +
                                        "`{{Config.BaseUrl}}`.\n\n Your user name is: **{{User.Email}}**  " +
                                        "\n Your assigned role is: **{{User.Role}}**  \n Your password is" +
                                        " what you provided when you registered.  \n - _Don't worry, you " +
                                        "can reset your password if you forgot._\n\n If you have any " +
                                        "questions, you may reply to this email.\n\n Thank you and enjoy " +
                                        "the rest of your day!";

                string actual;
                dynamic obj = new ExpandoObject();
                obj.Config = new Config(new[] {"[email protected]", "[email protected]"},
                                       new[] {"admin", "role2", "role3", "role4"}, "unit test description",
                                        "admin.html", "http://testurl.com/")
                    {
                        CustomEmails = new CustomEmails
                            {
                                NotifyUserAccepted = template
                            }
                    };
                dynamic options = new ExpandoObject();
                options.county = new[] {"Kane", "Salt Lake"};
                obj.User = new User("Not Approved", " but Active", "[email protected]",
                                    "AGENCY", "password", "SALT", null,
                                    null, null, null, null)
                    {
                        AccessRules = new User.UserAccessRules
                            {
                                StartDate = 0,
                                EndDate = 0,
                                Options = options
                            }
                    };

                using (var handleBars = new ExtendedHandlebars())
                {
                    handleBars.RegisterTemplate("county", template);
                    actual = handleBars.Transform("county", obj);
                }

                Debug.Print(actual);
                Assert.That(actual.Contains("Kane and Salt Lake"));
            }

This is printing

### Hello ,

 **Good News!** You have been granted permission to [login](http://testurl.com/) to the unit test description!  

 You can access the unit test description at `http://testurl.com/`.

 Your user name is: **[email protected]**  
 Your assigned role is: ****  
 Your password is what you provided when you registered.  
 - _Don't worry, you can reset your password if you forgot._

 If you have any questions, you may reply to this email.

 Thank you and enjoy the rest of your day!

image
are the variables at the time.

from chevron.

SimonCropp avatar SimonCropp commented on July 19, 2024

so is that producing the correct json?

from chevron.

steveoh avatar steveoh commented on July 19, 2024

i updated the comment... you might give that a look. Which json are you curious about?

from chevron.

SimonCropp avatar SimonCropp commented on July 19, 2024

well there is nothing smart about this lib. it simply converts the object to json and passes that to the js lib https://github.com/SimonCropp/Chevron/blob/master/Chevron.IE/Handlebars.cs#L96

so is your use of json.net producing the expected json?

from chevron.

steveoh avatar steveoh commented on July 19, 2024

yes I think it is. It would be the options property in that image. I don't understand why the #if statement isn't showing the county part anymore. I wonder if it's being double encoded now?

from chevron.

steveoh avatar steveoh commented on July 19, 2024

ahh it is not producing the correct json because I have some json ignore properties going on... They are required for the api to look right for the client.

from chevron.

steveoh avatar steveoh commented on July 19, 2024

This would work if you bundled json.net within your project like raven db so it would be a different jsonignore property. Since they are colliding now.

from chevron.

SimonCropp avatar SimonCropp commented on July 19, 2024

this project doesnt use json.net at all so how is it causing any collision issues for you?

from chevron.

steveoh avatar steveoh commented on July 19, 2024

oh, probably because this solution you proposed.

public override string SerializeObject(object context)
    {
        return JsonConvert.SerializeObject(context);
    }

from chevron.

SimonCropp avatar SimonCropp commented on July 19, 2024

in your initial request you asked for something that worked with jObject so i gave you code that would.

you can use any mechanism you want to serialize objects. but if you to pass in jObject and get sensible output you are stuck with json.net.

from chevron.

steveoh avatar steveoh commented on July 19, 2024

I guess I need to find a way to serialize this object ignoring [JsonIgnore] attributes

from chevron.

SimonCropp avatar SimonCropp commented on July 19, 2024

it sounds like you are mixing view models and persistence models, if so that will always cause friction

from chevron.

steveoh avatar steveoh commented on July 19, 2024

very true. I should just go on vacation and refactor when I get back. Thanks for the walk through.

from chevron.

SimonCropp avatar SimonCropp commented on July 19, 2024

no worries. have fun on vacation

from chevron.

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.