Ejemplo n.º 1
0
 public void Dispose()
 {
     mProperties.Clear();
     mSections.Clear();
     TemplateHost   = null;
     ChildTemplate  = null;
     ParentTemplate = null;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Binds the data for the template. Before accessing data outside a OnBind event, you must call this method, otherwise ItemIndex and Item might
        /// return false data since the template is reused and bound on occasion.
        /// </summary>
        public void Bind()
        {
            ITemplateHost host = Container as ITemplateHost;

            if (host != null)
            {
                host.Bind(this);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Binds this template with a specified item.
        /// </summary>
        /// <param name="itemIndex">The index of the item for which to bind.</param>
        public void Bind(int itemIndex)
        {
            ITemplateHost host = Container as ITemplateHost;

            if (host != null)
            {
                host.Bind(this, itemIndex);
            }
        }
Ejemplo n.º 4
0
 public GenerationResult Generate(TextReader document, ITemplateHost host)
 {
     RazorTemplateEngine engine = CreateEngine(host);
     var result = engine.GenerateCode(document);
     return new GenerationResult()
     {
         Success = result.Success,
         Document = result.Document.ToRazorSpy(),
         Code = CodeGenerator.GenerateCode(host, result.GeneratedCode),
     };
 }
Ejemplo n.º 5
0
 public ITemplate GetTemplate(ITemplateHost host)
 {
     lock (this)
     {
         if (!mIsInit)
         {
             Builder(host);
             mIsInit = true;
         }
         return((ITemplate)Activator.CreateInstance(TemplateType));
     }
 }
Ejemplo n.º 6
0
        public static void BuilderTemplate(ITemplateInfo templateinfo,ITemplateHost host)
        {
            Type type = host.BaseType;

            string typename = "_" + Guid.NewGuid().ToString("N");
            RazorTemplateEngine engine = CreateHost(type, "_" + type.Name, typename);

            AddNamespace(type.Namespace);
            GeneratorResults razorResults = null;
            using (System.IO.StreamReader reader = templateinfo.GetCode())
            {
                razorResults = engine.GenerateCode(reader);
                CSharpCodeProvider codeProvider = new CSharpCodeProvider();
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                string LastGeneratedCode = null;
                using (StringWriter writer = new StringWriter())
                {
                    codeProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, writer, options);
                    templateinfo.Host.LastCompileSource = writer.ToString();
                }
                CompilerParameters compilerParameters = new CompilerParameters(new string[0]);
                compilerParameters.OutputAssembly = "tmp_assembly" + typename;
                compilerParameters.GenerateInMemory = false;
                AddDomainAssemblies();
                foreach (string item in ReferencedAssemblies)
                    compilerParameters.ReferencedAssemblies.Add(item);
                compilerParameters.GenerateInMemory = true;
                CompilerResults compilerResults = codeProvider.CompileAssemblyFromDom(compilerParameters, razorResults.GeneratedCode);
                if (compilerResults.Errors.Count > 0)
                {
                    string errormessage = null;

                    int throwindex = 1;
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    using (System.IO.StreamReader line = new StreamReader(reader.BaseStream, reader.CurrentEncoding))
                    {

                        ParserResults presult = engine.ParseTemplate(line);

                        if (presult.ParserErrors.Count > 0)
                        {
                            throwindex = presult.ParserErrors[0].Location.LineIndex + 1;
                            errormessage = presult.ParserErrors[0].Message;
                            reader.BaseStream.Seek(0, SeekOrigin.Begin);
                            using (System.IO.StreamReader readcode = new StreamReader(reader.BaseStream, reader.CurrentEncoding))
                            {
                                string code = readcode.ReadLine();
                                while (code != null)
                                {
                                    sb.AppendLine(code);
                                    code = readcode.ReadLine();

                                }
                            }

                        }
                        else
                        {
                            throwindex = compilerResults.Errors[0].Line;
                            errormessage = compilerResults.Errors[0].ErrorText;
                            sb.Append(LastGeneratedCode);
                        }

                    }

                    templateinfo.CompileError = errormessage;
                    throw new RazorException(templateinfo.CompileError);

                }
                templateinfo.Assembly = compilerResults.CompiledAssembly;
                templateinfo.TemplateType = compilerResults.CompiledAssembly.GetType("_" + type.Name + "." + typename);

            }
        }
Ejemplo n.º 7
0
 public void Dispose()
 {
     mProperties.Clear();
     mSections.Clear();
     TemplateHost = null;
     ChildTemplate = null;
     ParentTemplate = null;
 }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            ITemplateHost mHost = Razor.CreateHost();

            mHost.LoadTemplateFiles();

            mHost.AddTemplate("/master", "<h1>主页面</h1>@RenderBody()");

            mHost.AddTemplate("/user", "<div><h3>用户信息</h3><p>email:@DataContext.EMail</p><p>name:@DataContext.Name</p></div>");

            mHost.AddTemplate("/users", "<div class=\"user-list\"><h2>用户列表</h2>@Each(\"/user\",Model.Items)<div>");


            string value = mHost.Parse("/user", new User {
                EMail = "*****@*****.**", Name = "henry"
            });

            Console.WriteLine(value);


            value = mHost.Parse("/users", new { Items = new User[] {
                                                    new User {
                                                        EMail = "*****@*****.**", Name = "henry"
                                                    },
                                                    new User {
                                                        EMail = "*****@*****.**", Name = "henry1"
                                                    }
                                                } });
            Console.WriteLine(value);

            value = mHost.Parse("/users", "/master", new
            {
                Items = new User[] {
                    new User {
                        EMail = "*****@*****.**", Name = "henry"
                    },
                    new User {
                        EMail = "*****@*****.**", Name = "henry1"
                    }
                }
            });
            Console.WriteLine(value);


            mHost.AddTemplate("/test", "@Raw(DataContext.EMail)");
            value = mHost.Parse("/test", new { EMail = "[email protected]<a>" });
            Console.Write(value);


            value = mHost.Parse("/views/raw", null);
            Console.Write(value);

            value = mHost.Parse("/views/index", "/views/master",
                                new { EMail = "*****@*****.**", Name = "henryfan" });
            Console.Write(value);

            value = mHost.Parse("/views/foreachSection", new { Items = new User[] { new User(), new User() } });
            Console.Write(value);

            Console.Read();
        }
Ejemplo n.º 9
0
 public void Builder(ITemplateHost host)
 {
     Common.RazorComplie.BuilderTemplate(this, host);
 }
Ejemplo n.º 10
0
 public string GenerateCode(ITemplateHost host, CodeCompileUnit ccu)
 {
     return GenerateString(ccu, host.Language.CreateCodeDomProvider());
 }
Ejemplo n.º 11
0
 public void Builder(ITemplateHost host)
 {
     Common.RazorComplie.BuilderTemplate(this,host);
 }
Ejemplo n.º 12
0
 public RazorCompiler(IRazorEngineReference engine, ITemplateHost host)
 {
     _engine = engine;
     _host = host;
 }
Ejemplo n.º 13
0
        public static void BuilderTemplate(ITemplateInfo templateinfo, ITemplateHost host)
        {
            Type type = host.BaseType;

            string typename            = "_" + Guid.NewGuid().ToString("N");
            RazorTemplateEngine engine = CreateHost(type, "_" + type.Name, typename);

            AddNamespace(type.Namespace);
            GeneratorResults razorResults = null;

            using (System.IO.StreamReader reader = templateinfo.GetCode())
            {
                razorResults = engine.GenerateCode(reader);
                CSharpCodeProvider   codeProvider = new CSharpCodeProvider();
                CodeGeneratorOptions options      = new CodeGeneratorOptions();
                string LastGeneratedCode          = null;
                using (StringWriter writer = new StringWriter())
                {
                    codeProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, writer, options);
                    templateinfo.Host.LastCompileSource = writer.ToString();
                }
                CompilerParameters compilerParameters = new CompilerParameters(new string[0]);
                compilerParameters.OutputAssembly   = "tmp_assembly" + typename;
                compilerParameters.GenerateInMemory = false;
                AddDomainAssemblies();
                foreach (string item in ReferencedAssemblies)
                {
                    compilerParameters.ReferencedAssemblies.Add(item);
                }
                compilerParameters.GenerateInMemory = true;
                CompilerResults compilerResults = codeProvider.CompileAssemblyFromDom(compilerParameters, razorResults.GeneratedCode);
                if (compilerResults.Errors.Count > 0)
                {
                    string errormessage = null;

                    int throwindex = 1;
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    using (System.IO.StreamReader line = new StreamReader(reader.BaseStream, reader.CurrentEncoding))
                    {
                        ParserResults presult = engine.ParseTemplate(line);

                        if (presult.ParserErrors.Count > 0)
                        {
                            throwindex   = presult.ParserErrors[0].Location.LineIndex + 1;
                            errormessage = presult.ParserErrors[0].Message;
                            reader.BaseStream.Seek(0, SeekOrigin.Begin);
                            using (System.IO.StreamReader readcode = new StreamReader(reader.BaseStream, reader.CurrentEncoding))
                            {
                                string code = readcode.ReadLine();
                                while (code != null)
                                {
                                    sb.AppendLine(code);
                                    code = readcode.ReadLine();
                                }
                            }
                        }
                        else
                        {
                            throwindex   = compilerResults.Errors[0].Line;
                            errormessage = compilerResults.Errors[0].ErrorText;
                            sb.Append(LastGeneratedCode);
                        }
                    }

                    templateinfo.CompileError = errormessage;
                    throw new RazorException(templateinfo.CompileError);
                }
                templateinfo.Assembly     = compilerResults.CompiledAssembly;
                templateinfo.TemplateType = compilerResults.CompiledAssembly.GetType("_" + type.Name + "." + typename);
            }
        }
Ejemplo n.º 14
0
 public UnitTest1()
 {
     mHost = Razor.CreateHost();
     mHost.LoadTemplateFiles();
 }
Ejemplo n.º 15
0
 public ITemplate GetTemplate(ITemplateHost host)
 {
     lock (this)
     {
         if (!mIsInit)
         {
             Builder(host);
             mIsInit = true;
         }
         return (ITemplate)Activator.CreateInstance(TemplateType);
     }
 }
Ejemplo n.º 16
0
 private static RazorTemplateEngine CreateEngine(ITemplateHost host)
 {
     RazorTemplateEngine engine = new RazorTemplateEngine(host.CreateHost());
     return engine;
 }
Ejemplo n.º 17
0
 public GenerationResult Generate(TextReader reader, ITemplateHost host)
 {
     return _inner.Value.Generate(reader, host);
 }