@@ -24,6 +24,7 @@ public partial class DefaultTemplateFactory : ITemplateFactory
2424 {
2525 private readonly CodeGeneratorSettingsBase _settings ;
2626 private readonly Assembly [ ] _assemblies ;
27+ private readonly Func < string , string , string , string > _templateContentLoader ;
2728
2829 /// <summary>Initializes a new instance of the <see cref="DefaultTemplateFactory"/> class.</summary>
2930 /// <param name="settings">The settings.</param>
@@ -32,6 +33,7 @@ public DefaultTemplateFactory(CodeGeneratorSettingsBase settings, Assembly[] ass
3233 {
3334 _settings = settings ;
3435 _assemblies = assemblies ;
36+ _templateContentLoader = GetLiquidTemplate ;
3537 }
3638
3739 /// <summary>Creates a template for the given language, template name and template model.</summary>
@@ -45,7 +47,7 @@ public ITemplate CreateTemplate(string language, string template, object model)
4547 return new LiquidTemplate (
4648 language ,
4749 template ,
48- GetLiquidTemplate ,
50+ _templateContentLoader ,
4951 model ,
5052 GetToolchainVersion ( ) ,
5153 _settings ) ;
@@ -146,15 +148,18 @@ static LiquidTemplate()
146148 private const string TabCountRegexString = @"(\s*)?\{%(-)?\s+template\s+([a-zA-Z0-9_.]+)(\s*?.*?)\s(-)?%}" ;
147149 private const string CsharpDocsRegexString = "(\n ( )*)([^\n ]*?) \\ | csharpdocs }}" ;
148150 private const string TabRegexString = "(\n ( )*)([^\n ]*?) \\ | tab }}" ;
151+ private const string EmptyTemplateCleanupRegexString = @"^[ ]+__EMPTY-TEMPLATE__$[\n]{0,1}" ;
149152
150153#if NET8_0_OR_GREATER
151154 private static readonly Regex _tabCountRegex = TabCountRegex ( ) ;
152155 private static readonly Regex _csharpDocsRegex = CsharpDocsRegex ( ) ;
153156 private static readonly Regex _tabRegex = TabRegex ( ) ;
157+ private static readonly Regex _emptyTemplateCleanupRegex = EmptyTemplateCleanupRegex ( ) ;
154158#else
155159 private static readonly Regex _tabCountRegex = new ( TabCountRegexString , RegexOptions . Singleline | RegexOptions . Compiled ) ;
156160 private static readonly Regex _csharpDocsRegex = new ( CsharpDocsRegexString , RegexOptions . Singleline | RegexOptions . Compiled ) ;
157161 private static readonly Regex _tabRegex = new ( TabRegexString , RegexOptions . Singleline | RegexOptions . Compiled ) ;
162+ private static readonly Regex _emptyTemplateCleanupRegex = new ( EmptyTemplateCleanupRegexString , RegexOptions . Multiline | RegexOptions . Compiled ) ;
158163#endif
159164
160165 public LiquidTemplate (
@@ -243,7 +248,7 @@ public string Render()
243248 var trimmed = render . Replace ( "\r " , "" ) . Trim ( '\n ' ) ;
244249
245250 // clean up cases where we have called template but it produces empty output
246- var withoutEmptyWhiteSpace = Regex . Replace ( trimmed , @"^[ ]+__EMPTY-TEMPLATE__$[\n]{0,1}" , string . Empty , RegexOptions . Multiline ) ;
251+ var withoutEmptyWhiteSpace = _emptyTemplateCleanupRegex . Replace ( trimmed , string . Empty ) ;
247252
248253 // just to make sure we don't leak out marker
249254 return withoutEmptyWhiteSpace . Replace ( "__EMPTY-TEMPLATE__" , "" ) ;
@@ -275,6 +280,9 @@ public string Render()
275280
276281 [ GeneratedRegex ( TabRegexString , RegexOptions . Compiled | RegexOptions . Singleline ) ]
277282 private static partial Regex TabRegex ( ) ;
283+
284+ [ GeneratedRegex ( EmptyTemplateCleanupRegexString , RegexOptions . Compiled | RegexOptions . Multiline ) ]
285+ private static partial Regex EmptyTemplateCleanupRegex ( ) ;
278286#endif
279287 }
280288
0 commit comments