Exemplo n.º 1
0
        public void TemplateEngine_RenderAsync_Executes()
        {
            var template =
                _templateEngine.RenderAsync("Test.cshtml", "Model", null, CultureInfo.InvariantCulture).Result;

            Assert.IsNotNull(template);
            Assert.IsTrue(template.Length > 0);
        }
        public static Task <MailMessage> RenderEmailAsync(this ITemplateEngine templateEngine, string path, object viewBag = null,
                                                          CultureInfo culture = null)
        {
            Contract.Requires <ArgumentNullException>(templateEngine != null);
            Contract.Requires <ArgumentException>(!string.IsNullOrEmpty(path));
            Contract.Requires <NotSupportedException>(templateEngine.GetType() == typeof(RazorTemplateEngine), "Only razor engine is supported.");

            return(templateEngine.RenderAsync(path, renderer: new EmailRenderer(), viewBag: viewBag, culture: culture));
        }
Exemplo n.º 3
0
        public async ValueTask <string?> FormatAsync(string text, EnrichedEvent @event)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            if (TryGetTemplate(text.Trim(), out var template))
            {
                var vars = new TemplateVars
                {
                    ["event"] = @event
                };

                return(await templateEngine.RenderAsync(template, vars));
            }

            if (TryGetScript(text.Trim(), out var script))
            {
                // Script vars are just wrappers over dictionaries for better performance.
                var vars = new EventScriptVars
                {
                    Event   = @event,
                    AppId   = @event.AppId.Id,
                    AppName = @event.AppId.Name,
                    User    = Admin()
                };

                var result = (await scriptEngine.ExecuteAsync(vars, script)).ToString();

                if (result == "undefined")
                {
                    return(GlobalFallback);
                }

                return(result);
            }

            var parts = BuildParts(text, @event);

            if (parts.Any(x => !x.Var.IsCompleted))
            {
                await ValueTaskEx.WhenAll(parts.Select(x => x.Var));
            }

            return(CombineParts(text, parts));
        }
Exemplo n.º 4
0
        public async ValueTask <string?> FormatAsync(string text, EnrichedEvent @event)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            if (TryGetTemplate(text.Trim(), out var template))
            {
                var vars = new TemplateVars
                {
                    ["event"] = @event
                };

                return(await templateEngine.RenderAsync(template, vars));
            }

            if (TryGetScript(text.Trim(), out var script))
            {
                var vars = new ScriptVars
                {
                    ["event"] = @event
                };

#pragma warning disable MA0042 // Do not use blocking calls in an async method
                var result = scriptEngine.Execute(vars, script).ToString();
#pragma warning restore MA0042 // Do not use blocking calls in an async method

                if (result == "undefined")
                {
                    return(GlobalFallback);
                }

                return(result);
            }

            var parts = BuildParts(text, @event);

            if (parts.Any(x => !x.Var.IsCompleted))
            {
                await ValueTaskEx.WhenAll(parts.Select(x => x.Var));
            }

            return(CombineParts(text, parts));
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public async Task <string> GenerateEmailBody <T>(MessageTemplate template, T model)
        {
            template.ThrowIfNull(nameof(template));

            logger.LogInformation($"Start generate email body by template:{template.Name}, model: {model?.ToJson()}, parent template:{template.Parent}");

            var parentTemplate = await GetParentTemplate(template);

            VerifyTemplateAndParentTemplate(template, parentTemplate);

            ITemplateEngine templateEngine = GetTemplateEngine(template.EngineType);

            templateEngine.AddParentTemplate(parentTemplate);

            logger.LogInformation($"Starting async render template with model. Type template engine: {templateEngine.GetType().Name}");

            return(await templateEngine.RenderAsync(template, model));
        }
Exemplo n.º 6
0
        public async ValueTask <string?> FormatAsync(string text, EnrichedEvent @event)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            if (TryGetTemplate(text.Trim(), out var template))
            {
                var vars = new TemplateVars
                {
                    ["event"] = @event
                };

                return(await templateEngine.RenderAsync(template, vars));
            }

            if (TryGetScript(text.Trim(), out var script))
            {
                var vars = new ScriptVars
                {
                    ["event"] = @event
                };

                return(scriptEngine.Interpolate(vars, script));
            }

            var parts = BuildParts(text, @event);

            if (parts.Any(x => !x.Var.IsCompleted))
            {
                await ValueTaskEx.WhenAll(parts.Select(x => x.Var));
            }

            return(CombineParts(text, parts));
        }