コード例 #1
0
ファイル: ChevronViewEngine.cs プロジェクト: yannisgu/Chevron
        /// <summary>
        /// Renders the view.
        /// </summary>
        /// <param name="viewLocationResult">A <see cref="ViewLocationResult"/> instance, containing information on how to get the view template.</param>
        /// <param name="model">The model to be passed into the view</param>
        /// <param name="renderContext"></param>
        /// <returns>A response</returns>
        public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
        {
            return(new HtmlResponse
            {
                Contents = stream =>
                {
                    var templateName = viewLocationResult.Name;

                    handlebars.RegisterTemplate(templateName, () =>
                    {
                        using (var textReader = viewLocationResult.Contents())
                        {
                            return textReader.ReadToEnd();
                        }
                    });
                    foreach (var partial in viewLocator.GetAllCurrentlyDiscoveredViews().Where(x => x.Name.StartsWith("_")))
                    {
                        var localPartial = partial;
                        var partialName = localPartial.Name.TrimStart('_');
                        handlebars.RegisterPartial(partialName, () =>
                        {
                            using (var textReader = localPartial.Contents())
                            {
                                return textReader.ReadToEnd();
                            }
                        });
                    }
                    using (var writer = new StreamWriter(stream))
                    {
                        dynamic output;
                        try
                        {
                            output = handlebars.Transform(templateName, model);
                        }
                        catch (Exception)
                        {
                            //TODO: remove this exception handling after a few versions
                            var templateContents = viewLocationResult.Contents().ReadToEnd();
                            if (templateContents.Contains("{{> _") || templateContents.Contains("{{>_"))
                            {
                                throw new Exception(string.Format("Template '{0}' contains and underscore prefixed partial name. This is no longer required. Search for the string '{{>_' or '{{> _' in your template and remove the '_'.", templateName));
                            }
                            throw;
                        }
                        writer.Write(output);
                    }
                }
            });
        }