/// <summary>
        /// Reads the content of the template specified by the <paramref name="templateName"/> parameter.
        /// </summary>
        /// <param name="context">The <see cref="Context"/> of the call.</param>
        /// <param name="templateName">The name of the template to read.</param>
        /// <exception cref="liquid.Exceptions.FileSystemException">The specified template could not be located.</exception>
        /// <returns>The content of the template.</returns>
        public string ReadTemplateFile(liquid.Context context, string templateName)
        {
            IRenderContext renderContext = context.Registers["nancy"] as IRenderContext;

            if (renderContext != null)
            {
                // Clean up the template name
                templateName = GetCleanTemplateName(templateName);

                // Try to find a matching template using established view conventions
                ViewLocationResult viewLocation = null;
                if (extensions.Any(
                        s => templateName.EndsWith(s, StringComparison.OrdinalIgnoreCase)))
                {
                    // The template name does end with a valid extension, just try to find it
                    viewLocation = renderContext.LocateView(templateName, null);
                }
                else
                {
                    // The template name does not end with a valid extension, try all the possibilities
                    foreach (string extension in extensions)
                    {
                        viewLocation = renderContext.LocateView(String.Concat(templateName, ".", extension), null);
                        if (viewLocation != null)
                        {
                            break;
                        }
                    }
                }

                // If we found one, get the template and pass it back
                // Eventually, it would be better to pass back the actual template from the cache if it's already been parsed
                // Or to parse here and store it in the cache before passing it back in not
                if (viewLocation != null)
                {
                    using (var reader = viewLocation.Contents.Invoke())
                        return(reader.ReadToEnd());
                }
            }
            throw new FileSystemException("Template file {0} not found", new[] { templateName });
        }
コード例 #2
0
 public new virtual string ReadTemplateFile(liquid.Context context, string templateName)
 {
     return(base.ReadTemplateFile(context, templateName));
 }