From a4ca519e4172144272bdc5b7038df4bb7690651f Mon Sep 17 00:00:00 2001 From: Kota Nishizaki Date: Wed, 28 Dec 2022 07:24:33 +0900 Subject: [PATCH] Add conditional branching based on the existence of an ExtraProperty item and an item value to the template in Demo sample --- samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml | 2 +- .../Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs | 9 +++++++++ samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml b/samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml index 1a62d3537..67203e131 100644 --- a/samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml +++ b/samples/Demo/Beef.Demo.CodeGen/Beef.Demo.xml @@ -1,6 +1,6 @@  - + diff --git a/samples/Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs b/samples/Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs index f226faecf..1bc3573a2 100644 --- a/samples/Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs +++ b/samples/Demo/Beef.Demo.CodeGen/Config/TestConfigEditor.cs @@ -15,6 +15,15 @@ public Task AfterPrepareAsync(IRootConfig config) // Look for the additional property added in the configuration file. if (e.TryGetExtraProperty("TestCodeGen", out JValue val) && val.ToObject()) e.CustomProperties["TestExtra"] = $"XXX.{e.GetExtraProperty("TestExtra")}.XXX"; // Add a new custom property that can be referenced in the template. + + // In order to test the additional properties existence in the template, + // store whether the specified additional properties exist in the configuration file. + foreach (var name in new string[] { "TestExist", "TestNotExist" }) + { + bool defined = e.ExtraProperties?.ContainsKey(name) ?? false; + e.CustomProperties[$"{name}-Defined"] = defined; + e.CustomProperties[name] = defined ? e.GetExtraProperty(name).Value : null; + } } return Task.CompletedTask; diff --git a/samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs b/samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs index ea68dc2bb..a4dce21f2 100644 --- a/samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs +++ b/samples/Demo/Beef.Demo.CodeGen/Templates/Test_cs.hbs @@ -1,4 +1,4 @@ -/* +/* Company: {{Root.Company}} AppName: {{Root.AppName}} Entity name: {{Name}} @@ -8,4 +8,8 @@ Extra loop2: {{#ExtraProperties}}{{@key}},{{@value}}{{#unless @last}};{{/unless} Extra lookup: {{lookup ExtraProperties 'TestExtra'}} Custom lookup: {{lookup CustomProperties 'TestExtra'}} Custom lookup lowercase: {{lower (lookup CustomProperties 'TestExtra')}} +Custom defined(lookup combined with if - true): {{#if (lookup CustomProperties 'TestExist-Defined')}}exist{{else}}not exist{{/if}} +Custom defined(lookup combined with if - false): {{#if (lookup CustomProperties 'TestNotExist-Defined')}}exist{{else}}not exist{{/if}} +Custom conditional(lookup combined with if - true): {{#ifeq (lookup CustomProperties 'TestExist') 'Exist-Value'}}true{{else}}false{{/ifeq}} +Custom conditional(lookup combined with if - false): {{#ifeq (lookup CustomProperties 'TestNotExist') 'Exist-Value'}}true{{else}}false{{/ifeq}} */ \ No newline at end of file