public async Task Start()
        {
            do
            {
                var tableName = this.Project.GeneratorModeConfig.GeneratorMode == GeneratorMode.CodeFirst ? CurrentTable.CsName : CurrentDbTable.Name;
                foreach (var builder in Project.Builders) //构造器
                {
                    CurrentBuilder = builder;             //记录当前执行的构建器
                    var content = await _engine.Render(this, builder.Template.TemplatePath);

                    await this.OutPut(tableName, content);

                    _logger.LogInformation($"生成文件{builder.GetName(tableName)}");
                    _logger.LogInformation($"内容:{content}");
                }
            }while (Next());
            foreach (var value in Project.GlobalBuilders) //全表构造器
            {
                CurrentBuilder = value;                   //记录当前执行的构建器
                var content = await _engine.Render(this, value.Template.TemplatePath);

                await this.OutPut(this.Project.ProjectInfo.NameSpace, content);

                _logger.LogInformation($"生成文件{value.GetName(this.Project.ProjectInfo.NameSpace)}");
                _logger.LogInformation($"内容:{content}");
            }
        }
        public void ExecuteStringModelTest()
        {
            string          template       = @"@Model.Text";
            ITemplateEngine templateEngine = new RazorTemplateEngine();
            string          result         = templateEngine.Render(template, new { Text = "Hello" });

            Assert.AreEqual("Hello", result);
        }
        public void ExecuteGenericTemplateBaseTest()
        {
            string             path           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "template2.cshtml");
            string             template       = File.ReadAllText(path);
            TemplateEngineBase templateEngine = new RazorTemplateEngine(Console.Out);
            string             result         = templateEngine.Render(template, 2);

            Assert.AreEqual("110", result);
        }
        public void ExecuteAnonymousModelTemplateTest()
        {
            string          path           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "anonymous-model-template.cshtml");
            string          template       = File.ReadAllText(path);
            ITemplateEngine templateEngine = new RazorTemplateEngine();
            string          result         = templateEngine.Render(template, new { Num1 = 1, Num2 = 2 });

            Assert.AreEqual("<span>3</span>", result);
        }
        public void ExecuteListModelTemplateTest()
        {
            string          path           = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", "template.cshtml");
            string          template       = File.ReadAllText(path);
            ITemplateEngine templateEngine = new RazorTemplateEngine(new StreamWriter(new FileStream("debug.txt", FileMode.OpenOrCreate)));
            string          result         = templateEngine.Render(template, new List <Product>
            {
                new Product {
                    ID = 1, Name = "Name1"
                },
                new Product {
                    ID = 2, Name = "Name2"
                },
            });

            Assert.AreEqual("<table><tr><td>1</td><td>Name1</td></tr><tr><td>2</td><td>Name2</td></tr></table>", result);
        }
Exemple #6
0
        public string Generate <TEntity>(TEntity entity, string templatePath)
        {
            //ITemplateEngine engine = new RazorTemplateEngineBuilder()
            //    .FindTemplatesInDirectory("Models")
            //    .CacheExpiresIn(TimeSpan.FromSeconds(20))
            //    .UseSharedCache()
            //    .Build();

            var embeddedResourceConfig = new RazorTemplateEngineConfiguration
            {
                CodeLanguage     = Language.CSharp,
                ResourceProvider = new EmbeddedResourceProvider(Assembly.GetExecutingAssembly()),
                CachePolicy      = CachePolicy.Shared
            };

            var engine   = new RazorTemplateEngine(embeddedResourceConfig);
            var template = engine.Render(templatePath, entity);

            // Replace HTML line breaks with new lines and trim the white space
            return(Regex.Replace(template, @"<br\s?/>", Environment.NewLine).Trim());
        }
Exemple #7
0
        public void Execute()
        {
            // Prepare sample data
            StatisticsDataService       statisticsDataService = new StatisticsDataService();
            List <SimpleStatisticsData> statisticsDatas       = statisticsDataService.GetSimpleStatisticsDatas();

            // Convert list to the td matrix
            Td[][] array = HtmlTableHelper.GetTdMatrix(statisticsDatas,
                                                       t => new Td[] {
                new Td {
                    Text = t.Dimension1
                },
                new Td {
                    Text = t.Dimension2
                },
            }, t => new Td[] {
                new Td {
                    Text = t.Denominator.ToString()
                },
                new Td {
                    Text = t.Numerator.ToString()
                },
                new Td {
                    Text = t.Quotient.ToString()
                },
            });

            // Read template content from template file
            string path     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", TEMPLATE_FILE);
            string template = File.ReadAllText(path);

            // Construct the razor template engine
            ITemplateEngine templateEngine = new RazorTemplateEngine(new StreamWriter(new FileStream(DEBUG_FILE, FileMode.OpenOrCreate)));

            // Render by template and data
            string result = templateEngine.Render(template, array);

            Console.WriteLine(result);
            File.WriteAllText(GENERATED_FILE, result);
        }