2017-06-12 52 views
2

我想使用RazorEngine在模板中包含其他文件,但我有點卡住了。我獲得了基本工作,但我希望能夠在我的模板中使用@Include("somefile.html")如何使用RazorEngine包含文件?

這是我到現在爲止:

string tpl = @"@Include(""foo.html"");"; 

ResolvePathTemplateManager r = new ResolvePathTemplateManager(new string[] { "html" }); 
var config = new TemplateServiceConfiguration(); 
config.TemplateManager = r; 
var service = RazorEngineService.Create(config); 

var a = service.RunCompile(tpl, "name", null, new { test = "TEMPLATE" }); 

當前工作目錄下有一個html dir其中foo.html所在,但我得到這個錯誤:

Could not resolve template name

回答

1

Apperently,解決路徑時, ,你不能使用字符串模板。此代碼的工作:

ResolvePathTemplateManager r = new ResolvePathTemplateManager(new string[] { "html" }); 
var config = new TemplateServiceConfiguration(); 
config.TemplateManager = r; 
var service = RazorEngineService.Create(config); 

var a = service.RunCompile("foo.html"); 

foo.html我可以使用:

@Include("otherfile.html"); 

包括來自同一目錄中的文件。

+1

FWIW,我們使用'DelegateTemplateManager',它允許你傳遞一個函數,它只需要一個'string'參數而不是'ResolvePathTemplateManager'。在那個函數中,你可以做任何你想要解析模板的東西 - 我們有一個我們搜索的路徑數組等。無論_that_是否適用於字符串模板... –