Exemple #1
0
        /// <summary>
        /// Compiles and renders a template.
        /// </summary>
        /// <param name="path">The fully qualified path of the file to load.</param>
        /// <param name="context">The <see cref="TemplateContext"/>.</param>
        /// <returns></returns>
        public static IResult CompileFile(this TemplateContext context, string path)
        {
            var full = context.FindFullPath(path);

            if (full == null)
            {
                throw new TemplateException($"\"{ path }\" cannot be found.");
            }
            var template = context.Environment.Results.GetOrAdd(full, (fullName) =>
            {
                var res = context.Load(fullName);
                if (res == null)
                {
                    throw new TemplateException($"Path:\"{path}\", the file could not be found.");
                }
                return(context.Environment.Compile(fullName, res.Content, (c) => context.CopyTo(c)));
            });

            return(template);
        }