Skip to content

Commit

Permalink
Fix MVC view engine and sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Apr 23, 2021
1 parent 3b4a712 commit d06a7c0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Fluid.MvcSample/CustomFluidViewParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class CustomFluidViewParser : FluidViewParser
{
public CustomFluidViewParser()
{
RegisterEmptyBlock("mytag", static async (s, w, e, c) =>
RegisterEmptyTag("mytag", static async (w, e, c) =>
{
await w.WriteAsync("Hello from MyTag");

Expand Down
2 changes: 1 addition & 1 deletion Fluid.MvcSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ConfigureServices(IServiceCollection services)
services.Configure<FluidViewEngineOptions>(options =>
{
options.Parser = new CustomFluidViewParser();
options.TemplateOptions.MemberAccessStrategy.Register<Person>();
options.TemplateOptions.MemberAccessStrategy = UnsafeMemberAccessStrategy.Instance;
});

services.AddMvc().AddFluid();
Expand Down
18 changes: 8 additions & 10 deletions Fluid.MvcViewEngine/FluidRendering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ namespace Fluid.MvcViewEngine
/// </summary>
public class FluidRendering : IFluidRendering
{
private const string ViewStartFilename = "_ViewStart.liquid";
public const string ViewPath = "ViewPath";

static FluidRendering()
{
}
Expand All @@ -47,23 +44,24 @@ public FluidRendering(

public async ValueTask<string> RenderAsync(string path, object model, ViewDataDictionary viewData, ModelStateDictionary modelState)
{
var context = new TemplateContext(model, _options.TemplateOptions);
var context = new TemplateContext(_options.TemplateOptions);
context.SetValue("ViewData", viewData);
context.SetValue("ModelState", modelState);
context.SetValue("Model", model);

// Provide some services to all statements
context.AmbientValues[ViewPath] = path;
context.AmbientValues["Sections"] = new Dictionary<string, IReadOnlyList<Statement>>();
context.AmbientValues[Constants.ViewPathIndex] = path;
context.AmbientValues[Constants.SectionsIndex] = new Dictionary<string, IReadOnlyList<Statement>>();

var template = ParseLiquidFile(path, _options.ViewsFileProvider ?? _hostingEnvironment.ContentRootFileProvider, true);

var body = await template.RenderAsync(context, _options.TextEncoder);

// If a layout is specified while rendering a view, execute it
if (context.AmbientValues.TryGetValue("Layout", out var layoutPath))
if (context.AmbientValues.TryGetValue(Constants.LayoutIndex, out var layoutPath))
{
context.AmbientValues[ViewPath] = layoutPath;
context.AmbientValues["Body"] = body;
context.AmbientValues[Constants.ViewPathIndex] = layoutPath;
context.AmbientValues[Constants.BodyIndex] = body;
var layoutTemplate = ParseLiquidFile((string)layoutPath, _options.ViewsFileProvider ?? _hostingEnvironment.ContentRootFileProvider, false);

return await layoutTemplate.RenderAsync(context, _options.TextEncoder);
Expand All @@ -86,7 +84,7 @@ public List<string> FindViewStarts(string viewPath, IFileProvider fileProvider)
return viewStarts;
}

viewPath = viewPath.Substring(0, index + 1) + ViewStartFilename;
viewPath = viewPath.Substring(0, index + 1) + Constants.ViewStartFilename;

var viewStartInfo = fileProvider.GetFileInfo(viewPath);
if (viewStartInfo.Exists)
Expand Down
4 changes: 2 additions & 2 deletions Fluid.Tests/MvcViewEngine/MvcViewEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public void ShouldParseIndex()
This is the footer
{% endsection %}
{% mytag %}{% endmytag %}
{% mytag %}
";
var parser = new FluidViewParser();

parser.RegisterEmptyBlock("mytag", static async (s, w, e, c) =>
parser.RegisterEmptyTag("mytag", static async (w, e, c) =>
{
await w.WriteAsync("Hello from MyTag");

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ namespace Fluid.MvcSample
{
public CustomFluidViewParser()
{
RegisterEmptyBlock("mytag", static async (s, w, e, c) =>
RegisterEmptyTag("mytag", static async (s, w, e, c) =>
{
await w.WriteAsync("Hello from MyTag");

Expand Down

0 comments on commit d06a7c0

Please sign in to comment.