示例#1
0
        /// <summary>
        /// Imports the template with the specified name.
        /// </summary>
        /// <param name="name">The template name.</param>
        /// <returns>
        /// The result of the template with the specified name.
        /// </returns>
        public virtual string Import(string name)
        {
            // If helpers is already loaded, then return immediately
            if (_helpersDictionary.ContainsKey(name))
            {
                return("");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("The name of the template to include is required.");
            }


            var template = Razorizer.FindTemplate(name);

            foreach (var method in template.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(LambdaWriter).IsAssignableFrom(method.ReturnType))
                {
                    RegisterHelper(name, template, method);
                }
            }
            template.Model = Model;
            return(template.Run());
        }
示例#2
0
        /// <summary>
        /// Parses the specified template name.
        /// </summary>
        /// <param name="templateName">Name of the template.</param>
        /// <returns></returns>
        public string Parse(string templateName)
        {
            string location = null;

            try
            {
                var template = Razorizer.FindTemplate(templateName);
                template.Model = this;
                return(template.Run());
            }
            catch (TemplateCompilationException ex)
            {
                foreach (var compilerError in ex.Errors)
                {
                    Logger.PushLocation(location, compilerError.Line, compilerError.Column);
                    if (compilerError.IsWarning)
                    {
                        Logger.Warning("{0}: {1}", compilerError.ErrorNumber, compilerError.ErrorText);
                    }
                    else
                    {
                        Logger.Error("{0}: {1}", compilerError.ErrorNumber, compilerError.ErrorText);
                    }
                    Logger.PopLocation();
                }
                Logger.PopLocation();
                Logger.Fatal("Error when compiling template [{0}]", templateName);
            }
            catch (TemplateParsingException ex)
            {
                foreach (var parserError in ex.Errors)
                {
                    Logger.PushLocation(ex.Location, parserError.Location.LineIndex, parserError.Location.CharacterIndex);
                    Logger.Error("{0}: {1}", "R0000", parserError.Message);
                    Logger.PopLocation();
                }
                Logger.PopLocation();
                Logger.Fatal("Error when compiling template [{0}]", templateName);
            }
            catch (Exception ex)
            {
                Logger.PushLocation(location);
                Logger.Error("Unexpected exception", ex);
                Logger.PopLocation();
                throw;
            }
            return("");
        }