Esempio n. 1
0
 /// <summary>
 /// Compile the text into a dynamic class.
 /// </summary>
 /// <param name="content">the context of the text</param>
 /// <param name="name">Unique key of the template</param>
 /// <param name="action">The parameter setting method.</param>
 /// <param name="environment">The options of the engine.</param>
 /// <returns></returns>
 public static ICompilerResult Compile(this IHostEnvironment environment, string name, string content, Action <CompileContext> action = null)
 {
     if (string.IsNullOrEmpty(name))
     {
         name = content.GetHashCode().ToString();
     }
     using (var ctx = environment.GenerateContext(name))
     {
         if (action != null)
         {
             action(ctx);
         }
         return(ctx.Compile(content));
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Compile the text into a dynamic class.
 /// </summary>
 /// <param name="path"></param>
 /// <param name="name">Unique key of the template</param>
 /// <param name="action">The parameter setting method.</param>
 /// <param name="environment">The options of the engine.</param>
 /// <returns></returns>
 public static ICompilerResult CompileFile(this IHostEnvironment environment, string name, string path, Action <CompileContext> action = null)
 {
     using (var ctx = environment.GenerateContext(name))
     {
         if (string.IsNullOrEmpty(path))
         {
             throw new ArgumentNullException(nameof(path));
         }
         if (action != null)
         {
             action(ctx);
         }
         var res = environment.Loader.Load(ctx, path);
         if (res == null)
         {
             throw new TemplateException($"Path:\"{path}\", the file could not be found.");
         }
         if (string.IsNullOrEmpty(ctx.Name))
         {
             ctx.Name = res.FullPath;
         }
         return(ctx.Compile(res.Content));
     }
 }