Comments (15)
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.
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!
are the variables at the time.
from chevron.
so is that producing the correct json?
from chevron.
i updated the comment... you might give that a look. Which json are you curious about?
from chevron.
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.
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.
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.
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.
this project doesnt use json.net at all so how is it causing any collision issues for you?
from chevron.
oh, probably because this solution you proposed.
public override string SerializeObject(object context)
{
return JsonConvert.SerializeObject(context);
}
from chevron.
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.
I guess I need to find a way to serialize this object ignoring [JsonIgnore] attributes
from chevron.
it sounds like you are mixing view models and persistence models, if so that will always cause friction
from chevron.
very true. I should just go on vacation and refactor when I get back. Thanks for the walk through.
from chevron.
no worries. have fun on vacation
from chevron.
Related Issues (20)
- Support running "other" js, or exposing engine HOT 8
- Why not disposing the MsieJsEngine? HOT 2
- RegisterTemplate fails if name contains integers HOT 1
- update to v3.0.3 of HandlebarsJS
- Partials drop trailing newline
- different route with the same templateName bug HOT 1
- [Q] What is Approvals? HOT 7
- update to MsieJavaScriptEngine 2.2.4.0
- Update to Jint 2.11.58
- Update to Nancy 1.4.4
- Update to handlebars v4.0.11
- Nancy.ViewEngines.Handlebars.V8.Sample not working HOT 2
- New min framework is 4.5.2
- Update to ClearScript.V8 5.5.0.1
- Handlebars 2 HOT 3
- Run additional scripts after loading handlebars HOT 4
- Non-Windows platforms HOT 4
- Chevron.IE.Merged appends "\r\n"
- Q: life cycle of Handlebars and concurrency 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 chevron.