|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Axuno.TextTemplating; |
| 5 | +using Microsoft.Extensions.Localization; |
| 6 | +using Scriban; |
| 7 | +using Scriban.Parsing; |
| 8 | +using Scriban.Runtime; |
| 9 | +using Scriban.Syntax; |
| 10 | + |
| 11 | +namespace TextTemplatingDemo.Demos.CustomRenderer |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Custom template renderer. |
| 15 | + /// </summary> |
| 16 | + /// <remarks> |
| 17 | + /// Inject as transient. |
| 18 | + /// </remarks> |
| 19 | + public class CustomTemplateRenderer : TemplateRenderer |
| 20 | + { |
| 21 | + public CustomTemplateRenderer( |
| 22 | + ITemplateContentProvider templateContentProvider, |
| 23 | + ITemplateDefinitionManager templateDefinitionManager, |
| 24 | + IStringLocalizerFactory stringLocalizerFactory) : base(templateContentProvider, templateDefinitionManager, stringLocalizerFactory) |
| 25 | + { } |
| 26 | + |
| 27 | + /* |
| 28 | + protected override async Task<string> RenderTemplateContentWithScribanAsync( |
| 29 | + TemplateDefinition templateDefinition, |
| 30 | + string? templateContent, |
| 31 | + Dictionary<string, object> globalContext, |
| 32 | + object? model = null) |
| 33 | + { |
| 34 | + var context = CreateScribanTemplateContext( |
| 35 | + templateDefinition, |
| 36 | + globalContext, |
| 37 | + model |
| 38 | + ); |
| 39 | +
|
| 40 | + return await Template |
| 41 | + .Parse(templateContent) |
| 42 | + .RenderAsync(context); |
| 43 | + }*/ |
| 44 | + |
| 45 | + protected override TemplateContext CreateScribanTemplateContext( |
| 46 | + TemplateDefinition templateDefinition, |
| 47 | + Dictionary<string, object> globalContext, |
| 48 | + object? model = null) |
| 49 | + { |
| 50 | + var baseContext = base.CreateScribanTemplateContext(templateDefinition, globalContext, model); |
| 51 | + |
| 52 | + // The ScriptObject is already registered as global |
| 53 | + baseContext.StrictVariables = true; // Note: has no effect for child members |
| 54 | + baseContext.MemberRenamer = member => member.Name; // Use .NET object without changing the cases |
| 55 | + // will be called if the member could not be found: |
| 56 | + baseContext.TryGetMember = (TemplateContext context, SourceSpan span, object target, string member, |
| 57 | + out object value) => |
| 58 | + { |
| 59 | + value = $"## Member '{member}' not found ##"; |
| 60 | + return true; |
| 61 | + }; |
| 62 | + // will be called if the variable could not be found |
| 63 | + baseContext. |
| 64 | + TryGetVariable = |
| 65 | + (TemplateContext context, SourceSpan span, ScriptVariable variable, out object value) => |
| 66 | + { |
| 67 | + value = value = $"## Variable '{variable}' not found ##"; |
| 68 | + return true; |
| 69 | + }; |
| 70 | + |
| 71 | + baseContext.BuiltinObject.SetValue(nameof(MyCustomFunction), new MyCustomFunction(), true); |
| 72 | + baseContext.BuiltinObject.Import("MySimpleFunction", new Func<string>(() => "Text from MySimpleFunction")); |
| 73 | + |
| 74 | + return baseContext; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments