Exemple #1
0
 public static Template FromFile(string filePath)
 {
     if (File.Exists(filePath))
     {
         FileInfo fileInfo = new FileInfo(filePath);
         Template t = new Template(fileInfo.Name, File.ReadAllText(filePath, Encoding.Default));
         t.Dir = fileInfo.DirectoryName;
         t.SetValue("Dir", t.Dir);
         return t;
     }
     else
         throw new Exception("文件未找到!:" + filePath);
 }
Exemple #2
0
        public string Process()
        {
            if (Elements == null)
            {
                lexer = new TemplateLexer(this.Content);
                parser = new TemplateParser(lexer);
                tagParser = new TagParser(parser);
                //TagParser tagParser=new TagParser(parser);
                //List<Element> elements = tagParser.Parse();

                Elements = tagParser.Parse();
            }

            for (int i = 0; i < Elements.Count; i++)
            {
                if (Elements[i] is Function)
                {
                    sw.Write(ProcessFunction(Elements[i] as Function));
                }
                else if (Elements[i] is TagFunction)
                {
                    var item = Elements[i] as TagFunction;
                    if (item.FunctionName == "Include")
                    {
                        string templateName = string.Empty;
                        Expression attrExp = item.GetAttribute("file");
                        templateName = EvalExpression(attrExp).ToString();
                        var temp = new Template(templateName, ProcessTagFunction(item));
                        for (int j = 0; j < Variables.Count; j++)
                        {
                            string key = Variables.GetKey(j);
                            string value = Variables.GetValue(j).ToString();
                            temp.SetValue(Variables.GetKey(j), Variables.GetValue(j));
                        }
                        sw.Write(temp.Process());
                    }
                    else
                        sw.Write(ProcessTagFunction(Elements[i] as TagFunction));
                }
                else if (Elements[i] is Text)
                {
                    sw.Write((Elements[i] as Text).Data);
                }
                else if (Elements[i] is Field)
                {
                    sw.Write(EvalField(Elements[i] as Field));
                }
                else if(Elements[i] is Variable)
                {
                    sw.Write(GetVariableValue((Elements[i] as Variable).Name));
                }
                else if (Elements[i] is TagClose)
                {

                }
            }
            return sw.GetStringBuilder().ToString();
        }