Skip to content

Commit 5f4bf50

Browse files
committed
Update README
1 parent 1bb7979 commit 5f4bf50

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,38 @@ The animal, Chewy, is not a dog.
160160
*/
161161
```
162162

163+
### Registering Decorators
164+
165+
```c#
166+
[Fact]
167+
public void BasicDecorator(IHandlebars handlebars)
168+
{
169+
string source = "{{#block @value-from-decorator}}{{*decorator 42}}{{@value}}{{/block}}";
170+
171+
var handlebars = Handlebars.Create();
172+
handlebars.RegisterHelper("block", (output, options, context, arguments) =>
173+
{
174+
options.Data.CreateProperty("value", arguments[0], out _);
175+
options.Template(output, context);
176+
});
177+
178+
handlebars.RegisterDecorator("decorator",
179+
(TemplateDelegate function, in DecoratorOptions options, in Context context, in Arguments arguments) =>
180+
{
181+
options.Data.CreateProperty("value-from-decorator", arguments[0], out _);
182+
});
183+
184+
var template = handlebars.Compile(source);
185+
186+
var result = template(null);
187+
Assert.Equal("42", result);
188+
}
189+
```
190+
For more examples see [DecoratorTests.cs](https://github.com/Handlebars-Net/Handlebars.Net/tree/master/source/Handlebars.Test/DecoratorTests.cs)
191+
192+
#### Known limitations:
193+
- helpers registered inside of a decorator will not override existing registrations
194+
163195
### Register custom value formatter
164196

165197
In case you need to apply custom value formatting (e.g. `DateTime`) you can use `IFormatter` and `IFormatterProvider` interfaces:
@@ -262,7 +294,7 @@ Will not encode:\
262294
` (backtick)\
263295
' (single quote)
264296

265-
Will encode non-ascii characters `â`, `ß`, ...\
297+
Will encode non-ascii characters ``, ``, ...\
266298
Into HTML entities (`<`, `â`, `ß`, ...).
267299

268300
##### Areas
@@ -277,12 +309,12 @@ public void UseCanonicalHtmlEncodingRules()
277309
handlebars.Configuration.TextEncoder = new HtmlEncoder();
278310

279311
var source = "{{Text}}";
280-
var value = new { Text = "< â" };
312+
var value = new { Text = "< " };
281313

282314
var template = handlebars.Compile(source);
283315
var actual = template(value);
284316

285-
Assert.Equal("&lt; â", actual);
317+
Assert.Equal("&lt; ", actual);
286318
}
287319
```
288320

@@ -301,8 +333,6 @@ Nearly all time spent in rendering is in the routine that resolves values agains
301333
- Rendering starts to get slower (into the tens of milliseconds or more) on dynamic objects.
302334
- The slowest (up to hundreds of milliseconds or worse) tend to be objects with custom type implementations (such as `ICustomTypeDescriptor`) that are not optimized for heavy reflection.
303335

304-
~~A frequent performance issue that comes up is JSON.NET's `JObject`, which for reasons we haven't fully researched, has very slow reflection characteristics when used as a model in Handlebars.Net. A simple fix is to just use JSON.NET's built-in ability to deserialize a JSON string to an `ExpandoObject` instead of a `JObject`. This will yield nearly an order of magnitude improvement in render times on average.~~
305-
306336
## Future roadmap
307337

308338
TBD

0 commit comments

Comments
 (0)