Replies: 1 comment 1 reply
-
Hi @cloudfy ((dynamic)context.Value).Add("switchValue", value); Here's an example how I would approach this using new features. It works for any input data. You can inline helpers declaration to [Fact]
public void SwitchCaseTest()
{
const string template = @"
{{~#switch propertyValue}}
{{~#case 'a'}}a == {{@switchValue}}{{/case~}}
{{~#case 'b'}}b == {{@switchValue}}{{/case~}}
{{~#default}}the value is not provided{{/default~}}
{{/switch~}}";
var handlebars = Handlebars.Create();
handlebars.RegisterHelper(new CaseHelper());
handlebars.RegisterHelper(new SwitchHelper());
handlebars.RegisterHelper(new DefaultCaseHelper());
var handlebarsTemplate = handlebars.Compile(template);
var a = handlebarsTemplate(new {propertyValue = "a"});
var b = handlebarsTemplate(new {propertyValue = "b"});
var c = handlebarsTemplate(new {propertyValue = "c"});
Assert.Equal("a == a", a);
Assert.Equal("b == b", b);
Assert.Equal("the value is not provided", c);
}
private class SwitchHelper : IHelperDescriptor<BlockHelperOptions>
{
public PathInfo Name { get; } = "switch";
public object Invoke(in BlockHelperOptions options, in Context context, in Arguments arguments)
{
return this.ReturnInvoke(options, context, arguments);
}
public void Invoke(in EncodedTextWriter output, in BlockHelperOptions options, in Context context, in Arguments arguments)
{
var value = arguments[0];
var switchFrame = options.CreateFrame(value);
var data = new DataValues(switchFrame);
data["switchValue"] = value;
data["__switchBlock"] = BoxedValues.True;
data["__switchCaseMatched"] = BoxedValues.False;
options.Template(output, switchFrame);
}
}
private class CaseHelper : IHelperDescriptor<BlockHelperOptions>
{
public PathInfo Name { get; } = "case";
public object Invoke(in BlockHelperOptions options, in Context context, in Arguments arguments)
{
return this.ReturnInvoke(options, context, arguments);
}
public void Invoke(in EncodedTextWriter output, in BlockHelperOptions options, in Context context, in Arguments arguments)
{
if (!(bool) options.Data["__switchBlock"]) throw new InvalidOperationException();
if((bool) options.Data["__switchCaseMatched"]) return;
var value = options.Data["switchValue"];
if(!Equals(value, arguments[0])) return;
var data = new DataValues(options.Frame);
data["__switchCaseMatched"] = BoxedValues.True;
options.Template(output, options.Frame); // execute `case` in switch context
}
}
private class DefaultCaseHelper : IHelperDescriptor<BlockHelperOptions>
{
public PathInfo Name { get; } = "default";
public object Invoke(in BlockHelperOptions options, in Context context, in Arguments arguments)
{
return this.ReturnInvoke(options, context, arguments);
}
public void Invoke(in EncodedTextWriter output, in BlockHelperOptions options, in Context context, in Arguments arguments)
{
if (!(bool) options.Data["__switchBlock"]) throw new InvalidOperationException();
if((bool) options.Data["__switchCaseMatched"]) return;
options.Template(output, options.Frame); // execute `case` in switch context
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
can any provide a sample for rendering a
Before 2.0x we could add to the context, which we cannot now.
(with 1.11.5)
context.Add("switchValue", value);
Beta Was this translation helpful? Give feedback.
All reactions