Exemple #1
0
        public void CacheTest()
        {
            string template = "A line with @model.Str and @model2.I with @@ignore me";
            Model  model    = new Model()
            {
                Str = "I'm Dynamic!", I = 20
            };
            Model2 model2 = new Model2()
            {
                I = 20
            };

            TemplateEngine eng = new TemplateEngine();

            eng.UsesDynamic();
            string ret = eng.Parse(template, model, model2);

            Assert.AreEqual("A line with I'm Dynamic! and 20 with @ignore me\r\n", ret);
            Assert.IsTrue(eng.IsCached(template));

            model.Str = "Cached!";
            model2.I  = 25;
            ret       = eng.Parse(template, model, model2);
            Assert.AreEqual("A line with Cached! and 25 with @ignore me\r\n", ret);
        }
Exemple #2
0
        protected WorkflowState PostRouterRendering(WorkflowContinuation <PostRouteWorkflowData> wc, PostRouteWorkflowData data)
        {
            string             template    = data.HtmlResponse.Html;
            IWebSessionService sessionSvc  = ServiceManager.Get <IWebSessionService>();
            List <string>      objectNames = new List <string>()
            {
                "session", "context", "cfg"
            };
            List <object> objects = new List <object>()
            {
                sessionSvc, data.Context, ServiceManager.Get <IAppConfigService>()
            };

            objectNames.AddRange(appTemplateObjects.Keys);
            objects.AddRange(appTemplateObjects.Values);
            try
            {
                string newHtml = templateEngine.Parse(template, objectNames.ToArray(), objects.ToArray());
                data.HtmlResponse.Html = newHtml;
            }
            catch (Exception ex)
            {
                // ServiceManager.Get<ILoggerService>().Log(ex);
                ServiceManager.Get <ISemanticProcessor>().ProcessInstance <LoggerMembrane, ST_Exception>(ex2 => ex2.Exception = ex);
            }

            return(WorkflowState.Continue);
        }
Exemple #3
0
        public void DynamicParameterTypePassing()
        {
            string template = "A line with @model.Str and @model.I with @@ignore me";
            Model  model    = new Model()
            {
                Str = "Howdy", I = 15
            };

            TemplateEngine eng = new TemplateEngine();

            // Removed: eng.Usings.Add("using ModelInterface;");
            // Removed: eng.References.Add("ModelInterface.dll");
            eng.References.Add("Microsoft.CSharp.dll");
            eng.References.Add(typeof(System.Runtime.CompilerServices.DynamicAttribute).Assembly.Location);

            string ret = eng.Parse(template, new List <ParamTypeInfo>()
            {
                // new ParamTypeInfo() {ParamName="model", ParamType="IModel", ParamValue = model},
                // changed to:
                new ParamTypeInfo()
                {
                    ParamName = "model", ParamType = "dynamic", ParamValue = model
                },
            });

            Assert.AreEqual("A line with \" + model.Str.ToString() + \" and \" + model.I.ToString() + \" with @ignore me\r\n", ret);
        }
Exemple #4
0
        public async void Resolve_Success()
        {
            var tmpl   = TemplateEngine.Parse(Template);
            var engine = new TemplateEngine(new TagResolver());
            var result = await engine.ResolveAsync(tmpl);

            Assert.Equal(ExpectedResult, result);
        }
Exemple #5
0
        /// <summary>
        /// 创建服务类
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="CommentsValue"></param>
        /// <returns></returns>
        public string CreateServicesCode(string TableName, string CommentsValue)
        {
            TemplateEngine templateEngine = new TemplateEngine();

            templateEngine.SetFile("Tem", templateConfig.ServicesTemplateFile);
            templateEngine.m_noMarkers = "comment";
            templateEngine.SetVal("t_object", TableName, false);
            templateEngine.Parse("out", "Tem", false);
            return(templateEngine.PutOutPageCode("out"));
        }
Exemple #6
0
        /// <summary>
        /// 创建控制器类
        /// </summary>
        /// <returns></returns>
        public string CreateControllersCode(string TableName, string CommentsValue, List <modelProperty> Propertys)
        {
            TemplateEngine templateEngine = new TemplateEngine();

            templateEngine.SetFile("Tem", templateConfig.ControllersTemplateFile);
            templateEngine.m_noMarkers = "comment";
            templateEngine.SetVal("t_object", TableName, false);
            if (Propertys.Any())
            {
                StringBuilder builder   = new StringBuilder();
                StringBuilder parameter = new StringBuilder();
                foreach (var item in Propertys)
                {
                    var whereItem = string.Empty;
                    if (item.ColumnType == typeof(string).Name)
                    {
                        whereItem = @"           if (!string.IsNullOrEmpty({t_item}))
                {
                    whereExpressionAll = whereExpressionAll.And(p => p.{t_item} == {t_item});
                }
                        " + "\r\n";
                    }
                    else if (item.ColumnType == typeof(int).Name || item.ColumnType == typeof(System.Enum).Name)
                    {
                        whereItem = @"            if ((int){t_item} != 0)
                    {
                        whereExpressionAll = whereExpressionAll.And(p => p.{t_item} == {t_item});
                    }" + "\r\n";
                    }
                    else
                    {
                        whereItem = "          whereExpressionAll = whereExpressionAll.And(p => p.{t_item} == {t_item});" + "\r\n";
                    }
                    if (item.ColumnType == typeof(System.Enum).Name)
                    {
                        parameter.Append($",{item.propertyInfo.PropertyType.Name}  {item.ColumnName}");
                    }
                    else
                    {
                        parameter.Append($",{item.ColumnType}  {item.ColumnName}");
                    }
                    builder.Append(whereItem.Replace("{t_item}", item.ColumnName));
                }
                var parameterWhere = parameter.ToString() + ",";
                templateEngine.SetVal("t_parameter", parameterWhere.Remove(0, 1), false);
                templateEngine.SetVal("f_parameter", builder.ToString(), false);
            }
            else
            {
                templateEngine.SetVal("t_parameter", "", false);
                templateEngine.SetVal("f_parameter", "", false);
            }
            templateEngine.Parse("out", "Tem", false);
            return(templateEngine.PutOutPageCode("out"));
        }
Exemple #7
0
        /// <summary>
        /// 创建模型
        /// </summary>
        /// <param name="modelName"></param>
        /// <param name="Brief"></param>
        /// <returns></returns>
        public string CreateModelCode(string modelName, string Brief)
        {
            var            modelPropertys = GetProperty(modelName);
            TemplateEngine templateEngine = new TemplateEngine();

            templateEngine.SetFile("Tem", templateConfig.ModelsTemplateFile);
            templateEngine.SetBlock("Tem", "FieldList1", "list1", "<!--\\s+BEGIN FieldList1\\s+-->([\\s\\S.]*)<!--\\s+END FieldList1\\s+-->");
            templateEngine.m_noMarkers = "comment";
            templateEngine.SetVal("t_name", modelName, false);
            templateEngine.SetVal("t_note", Brief, false);
            templateEngine.SetVal("t_object", modelName, false);
            templateEngine.SetVal("t_namespace", modelName, false);
            templateEngine.SetVal("t_DateTime", DateTime.Now.ToString(), false);
            foreach (var f in modelPropertys)
            {
                templateEngine.SetVal("f_name", f.ColumnName, false);
                templateEngine.SetVal("f_type", f.ColumnType, false);
                templateEngine.SetVal("f_note", f.ColumnDescription, false);
                templateEngine.Parse("list1", "FieldList1", true);
            }
            templateEngine.Parse("out", "Tem", false);
            return(templateEngine.PutOutPageCode("out"));
        }
Exemple #8
0
        public void FriendlyCompileTemplate()
        {
            string template = @"
@{
  string str = ""Hello World"";
  int i = 10;
  @:Literal
}
A line with @str and @i with @@ignore me
";

            TemplateEngine eng = new TemplateEngine();
            string         ret = eng.Parse(template);

            Assert.AreEqual("  Literal\r\nA line with \" + str.ToString() + \" and \" + i.ToString() + \" with @ignore me\r\n", ret);
        }
Exemple #9
0
        public void VarReplacementInLiteral()
        {
            string template = @"
@{
@:alert(""<@model.Str@>"")
}";

            Model model = new Model()
            {
                Str = "Welcome!"
            };
            TemplateEngine eng = new TemplateEngine();

            eng.UsesDynamic();
            string ret = eng.Parse(template, model);

            Assert.AreEqual("alert(\"\" + model.Str.ToString() + \"\r\n", ret);
        }
Exemple #10
0
        public void SimplerParameterPassing()
        {
            string template = "A line with @model.Str and @i with @@ignore me";
            Model  model    = new Model()
            {
                Str = "Howdy"
            };

            TemplateEngine eng = new TemplateEngine();

            eng.Usings.Add("using ModelInterface;");
            eng.References.Add("ModelInterface.dll");

            // An example of non-native and native type passing.
            string ret = eng.Parse(template, new string[] { "model", "i" }, model, 15);

            Assert.AreEqual("A line with Howdy and 15 with @ignore me\r\n", ret);
        }
Exemple #11
0
        public void ParameterPassing()
        {
            string template = "A line with @str and @i with @@ignore me";

            TemplateEngine eng = new TemplateEngine();
            string         ret = eng.Parse(template, new List <ParamTypeInfo>()
            {
                new ParamTypeInfo()
                {
                    ParamName = "str", ParamType = "string", ParamValue = "Hello World"
                },
                new ParamTypeInfo()
                {
                    ParamName = "i", ParamType = "int", ParamValue = 10
                },
            });

            Assert.AreEqual("A line with \" + str.ToString() + \" and \" + i.ToString() + \" with @ignore me\r\n", ret);
        }
Exemple #12
0
        public void DynamicNonNativeTypeOnlyParameterPassing()
        {
            string template = "A line with @model.Str and @model2.I with @@ignore me";
            Model  model    = new Model()
            {
                Str = "I'm Dynamic!", I = 20
            };
            Model2 model2 = new Model2()
            {
                I = 20
            };

            TemplateEngine eng = new TemplateEngine();

            eng.UsesDynamic();
            string ret = eng.Parse(template, model, model2);

            Assert.AreEqual("A line with I'm Dynamic! and 20 with @ignore me\r\n", ret);
        }
Exemple #13
0
        public void NonNativePassing()
        {
            string template = "A line with @model.Str and @model.I with @@ignore me";
            Model  model    = new Model()
            {
                Str = "Howdy", I = 15
            };

            TemplateEngine eng = new TemplateEngine();

            eng.Usings.Add("using ModelInterface;");
            eng.References.Add("ModelInterface.dll");
            string ret = eng.Parse(template, new List <ParamTypeInfo>()
            {
                new ParamTypeInfo()
                {
                    ParamName = "model", ParamType = "IModel", ParamValue = model
                },
            });

            Assert.AreEqual("A line with \" + model.Str.ToString() + \" and \" + model.I.ToString() + \" with @ignore me\r\n", ret);
        }
Exemple #14
0
        /// <summary>
        /// 创建前端页面
        /// </summary>
        /// <param name="modelName"></param>
        /// <param name="Brief"></param>
        /// <param name="codeView"></param>
        /// <returns></returns>
        public string CreateVueCode(string modelName, string Brief, CodeView codeView)
        {
            List <modelProperty> Propertys = codeView.Propertys;
            var            modelPropertys  = GetProperty(modelName);
            TemplateEngine templateEngine  = new TemplateEngine();

            templateEngine.SetFile("Tem", templateConfig.VueTemplateFile);
            templateEngine.SetBlock("Tem", "FieldList1", "list1", "<!--\\s+BEGIN FieldList1\\s+-->([\\s\\S.]*)<!--\\s+END FieldList1\\s+-->");
            templateEngine.m_noMarkers = "comment";
            templateEngine.SetVal("t_name", modelName, false);
            if (!codeView.tableToolOn)
            {
                templateEngine.SetVal("m_Istool", "", false);
            }
            else
            {
                templateEngine.SetVal("m_Istool", "v-if='false'", false);
            }
            if (Propertys.Any())
            {
                StringBuilder queryparameter = new StringBuilder();
                foreach (var item in Propertys)
                {
                    if (item.ColumnType == typeof(string).Name)
                    {
                        queryparameter.Append($"{item.ColumnName}: '',\r\n");
                    }
                    else
                    {
                        queryparameter.Append($"{item.ColumnName}: null,\r\n");
                    }
                    item.propertyInfo = modelPropertys.FirstOrDefault(p => p.ColumnName.ToLower() == item.ColumnName.ToLower()).propertyInfo;
                }
                templateEngine.SetVal("b_queryparameter", queryparameter.ToString(), false);
                templateEngine.SetVal("f_queryformitem", GroupFormItem(Propertys, "QueryForm", false), false);
            }
            else
            {
                templateEngine.SetVal("b_ queryparameter", "", false);
            }
            var DlogForm = GroupFormItem(modelPropertys.Where(p => p.ColumnName != "RootEntity").ToList(), "ruleForm", true);

            templateEngine.SetVal("dlog_formItem", DlogForm, false);
            StringBuilder verification = new StringBuilder();

            foreach (var item in modelPropertys)
            {
                var ColName = item.ColumnName.Substring(0, 1).ToLower() + item.ColumnName.Remove(0, 1);
                verification.Append("{item}: [{ required: true, message: '不能为空', trigger: 'blur' }],".Replace("{item}", ColName));
                if (item.ColumnName.ToLower() == "id")
                {
                    continue;
                }
                if (item.ColumnType == typeof(int).Name)
                {
                    templateEngine.SetVal("f_type", ".number", false);
                }
                else
                {
                    templateEngine.SetVal("f_type", "", false);
                }
                templateEngine.SetVal("f_name", ColName, false);
                templateEngine.SetVal("f_note", item.ColumnDescription, false);
                templateEngine.Parse($"list1", $"FieldList1", true);
            }
            string RowVerification = verification.ToString();

            templateEngine.SetVal("p_verification", RowVerification.Remove(RowVerification.Length - 1, 1), false);
            templateEngine.Parse("out", "Tem", false);
            return(templateEngine.PutOutPageCode("out"));
        }