Beispiel #1
0
        static void Main()
        {
            var razor = new RazorEngine <RazorTemplateBase>();

            razor.Configuration.CompileToMemory = false;

            try
            {
                var assemblyName = razor.ParseAndCompileTemplate(null, new StringReader(TEMPLATE));

                string output = null;
                if (assemblyName != null)
                {
                    output = razor.RenderTemplateFromAssembly(assemblyName, new Context());
                }
                if (output == null)
                {
                    Console.WriteLine(razor.ErrorMessage);
                    Console.WriteLine(razor.LastGeneratedCode);
                }
                else
                {
                    Console.WriteLine(output);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        /// <summary>
        /// Stock implementation of RenderTemplate that doesn't allow for
        /// any sort of assembly caching. Instead it creates and re-renders
        /// templates read from the reader each time.
        ///
        /// Custom implementations of RenderTemplate should be created that
        /// allow for caching by examing a filename or string hash to determine
        /// whether a template needs to be re-generated and compiled before
        /// rendering.
        /// </summary>
        /// <param name="reader">TextReader that points at the template to compile</param>
        /// <param name="context">Optional context to pass to template</param>
        /// <param name="writer">TextReader passed in that receives output</param>
        /// <returns></returns>
        public bool RenderTemplate(TextReader reader, object context, TextWriter writer)
        {
            var assemblyId = Engine.ParseAndCompileTemplate(ReferencedAssemblies.ToArray(), reader);

            if (assemblyId == null)
            {
                ErrorMessage = Engine.ErrorMessage;
                return(false);
            }

            return(RenderTemplateFromAssembly(assemblyId, context, writer));
        }
        private void tbRunLowLevel_Click(object sender, EventArgs e)
        {
            // we can pass any object as context - here create a custom context
            var context = new CustomContext()
            {
                WinForm = this,
                Entered = DateTime.Now.AddDays(-10)
            };

            var engine = new RazorEngine<RazorTemplateBase>();
            string assId = null;

            using (StringReader reader = new StringReader(this.txtSource.Text))
            {
                assId = engine.ParseAndCompileTemplate(new string[] { "System.Windows.Forms.dll" }, reader);
            }

            string output = engine.RenderTemplateFromAssembly(assId, context);

            if (output == null)
                this.txtResult.Text = "*** ERROR:\r\n" + engine.ErrorMessage;
            else
                this.txtResult.Text = output;
        }
Beispiel #4
0
		/////////////////////////////////////////////////////////////////////////////

		private string Runner( string text, bool returnSource, RazorRunnerException.RunHow runContext )
		{
			// ******
			//dynamic context = new ExpandoObject();
			//context.Version = 2.0;

			// ******
			var asmId = string.Empty;
			var result = string.Empty;
			var reader = new StringReader( text );

			// ******

//	as arguments to ctor pass the PrePostHandler() and a method to customize
//	the host and whatever else
//			 
//	 for the host we set the names for section, etc

			var razor = new RazorEngine<NmpRazorTemplateBase>( DefaultNamespaces, PrePostRenderHandler, CreateHostHandler );
			razor.SetError();

			try {
				asmId = razor.ParseAndCompileTemplate( assemblyPaths, reader );
			}
			catch ( Exception ex ) {
				ThreadContext.MacroError( "error Parsing and Compiling Razor template: {0}", ex.Message );
			}
			if( null == asmId ) {
				//
				// error 
				//
				const string ErrMsg = "Razor failed to compile the generated source.";
				throw new RazorRunnerException( runContext, ErrMsg, razor.ErrorMessage, razor.LastGeneratedCode );
			}

			// ******
			try {
				result = razor.RenderTemplateFromAssembly( asmId, null );
			}
			catch ( Exception ex ) {
				ThreadContext.MacroError( "error Rendering Razor Template from Assembly template: {0}", ex.Message );
			}
			if( null == result ) {
				const string ErrMsg = "Razor failed to execute the template.";
				throw new RazorRunnerException( runContext, ErrMsg, razor.ErrorMessage, razor.LastGeneratedCode );
			}

			// ******
			if( RazorRunnerException.RunHow.Run == runContext ) {
				//
				// (#block `razor'
				//
			}
			
			// ******
			return FileReader.FixText( returnSource ? RazorRunnerException.AddLineNumbers(razor.LastGeneratedCode) : result );
		}