toddams/RazorLight

How does it work with DisplayTemplates ?

Closed this issue · 1 comments

How does this work with DisplayTemplates on the file system ?
e.g. i have this directory structure

/Project/Views/Home/Index.cshtml 
/Project/Views/Home/DisplayTemplates/T_BlogPost.cshtml 

And these dummy classes

public class T_BlogPost
{
  public string BP_UID;
  public string BP_Title;
  public string BP_HtmlContent;
}

public class ViewModel
{
  public string Name;
  public T_BlogPost BlogPost;
}

Then I want to compile Index.cshtml:

RazorLightEngine engine = new RazorLightEngineBuilder()
  .UseFileSystemProject(@"D:/username/Documents/Visual Studio 2017/Projects/RazorLight/TestRazorLight/Views")
  // .UseEmbeddedResourcesProject(typeof(Program)) // exception without this
  .UseMemoryCachingProvider()
  .Build();

ViewModel model = new ViewModel { Name = "John Doe", BlogPost= new T_BlogPost() { BP_UID="hoo", BP_Title="Foobar", BP_HtmlContent="<h1>Hello</h1>" } };
string result = await engine.CompileRenderAsync("Home/Index.cshtml", model);

System.Console.WriteLine(result);

with Index.cshtml having this content:


@*
	For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@


@model TestRazorLight.ViewModel


@{
	Layout = null;
}

Hello, @Model.Name. Welcome to RazorLight repository

@Html.DisplayFor(m => m.BlogPost)

and /DisplayTemplates/T_BlogPost.cshtml having this content:

@model TestRazorLight.T_BlogPost

@{
	Layout = null;
}

<div>
	<h3>@Model.BP_Title  <a href="@Url.Action("EditEntry", "Blog")/@Model.BP_UID" style="margin-left: 1cm; font-size: 13px;">[&nbsp;Edit&nbsp;]</a> </h3>
	@Html.Raw(Model.BP_HtmlContent)
</div>

<hr />

The only thing I get is an exception "The name 'HTML' does not exist in the current context".

RazorLight does not support HTML Helpers. But, luckily we have @Raw() function, which does the same, so you can use it for your case