Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added _includes support for Razor engine #77

Merged
merged 2 commits into from

2 participants

@AndreiMarukovich

Razor template engine is modified to provide support for _include folder

@shiftkey shiftkey merged commit 45bb7e3 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  src/Pretzel.Logic/Pretzel.Logic.csproj
@@ -117,6 +117,7 @@
<Compile Include="Templating\Jekyll\PostDrop.cs" />
<Compile Include="Templating\Jekyll\SiteContextDrop.cs" />
<Compile Include="Templating\Jekyll\Paginator.cs" />
+ <Compile Include="Templating\Razor\IncludesResolver.cs" />
<Compile Include="Templating\Razor\RazorSiteEngine.cs" />
<Compile Include="Templating\JekyllEngineBase.cs" />
<Compile Include="Templating\SiteEngineInfoAttribute.cs" />
View
40 src/Pretzel.Logic/Templating/Razor/IncludesResolver.cs
@@ -0,0 +1,40 @@
+using System;
+using System.IO;
+using System.IO.Abstractions;
+using RazorEngine.Templating;
+
+namespace Pretzel.Logic.Templating.Razor
+{
+ internal class IncludesResolver : ITemplateResolver
+ {
+ private readonly IFileSystem fileSystem;
+ private readonly string includesPath;
+
+ public IncludesResolver(IFileSystem fileSystem, string includesPath)
+ {
+ this.fileSystem = fileSystem;
+ this.includesPath = includesPath;
+ }
+
+ public string Resolve(string name)
+ {
+ var templatePath = Path.Combine(includesPath, name);
+ var templateExists = fileSystem.File.Exists(templatePath);
+ if (!templateExists)
+ {
+ foreach (var ext in new[] { ".cshtml", ".html", ".html" })
+ {
+ var testPath = String.Concat(templatePath, ext);
+ templateExists = fileSystem.File.Exists(testPath);
+ if (templateExists)
+ {
+ templatePath = testPath;
+ break;
+ }
+ }
+ }
+
+ return templateExists ? fileSystem.File.ReadAllText(templatePath) : String.Empty;
+ }
+ }
+}
View
11 src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs
@@ -1,6 +1,9 @@
using System.ComponentModel.Composition;
+using System.IO;
using System.Text.RegularExpressions;
using Pretzel.Logic.Templating.Context;
+using RazorEngine.Configuration;
+using RazorEngine.Templating;
namespace Pretzel.Logic.Templating.Razor
{
@@ -23,9 +26,13 @@ protected override string LayoutExtension
protected override string RenderTemplate(string content, PageContext pageData)
{
- content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");
+ var includesPath = Path.Combine(pageData.Site.SourceFolder, "_includes");
+ var serviceConfig = new TemplateServiceConfiguration { Resolver = new IncludesResolver(FileSystem, includesPath) };
+ RazorEngine.Razor.SetTemplateService(new TemplateService(serviceConfig));
- return RazorEngine.Razor.Parse(content, pageData);
+ content = Regex.Replace(content, "<p>(@model .*?)</p>", "$1");
+
+ return RazorEngine.Razor.Parse(content, pageData);
}
}
}
View
14 src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs
@@ -61,5 +61,19 @@ public void File_with_content_is_processed()
string output = FileSystem.File.ReadAllText(@"C:\website\_site\index.html");
Assert.Equal(expectedfileContents, output);
}
+
+ [Fact]
+ public void File_with_include_is_processed()
+ {
+ const string templateContents = "<html><head><title>@Model.Title</title></head><body>@Raw(Model.Content)</body></html>";
+ const string pageContents = "<i>@Include(\"TestInclude\")</i>";
+ const string layoutContents = "<b>Included!</b>";
+ const string expectedfileContents = "<html><head><title>My Web Site</title></head><body><i><b>Included!</b></i></body></html>";
+
+ FileSystem.AddFile(@"C:\website\_includes\TestInclude.cshtml", new MockFileData(layoutContents));
+ ProcessContents(templateContents, pageContents, new Dictionary<string, object> { { "title", "My Web Site" } });
+ string output = FileSystem.File.ReadAllText(@"C:\website\_site\index.html");
+ Assert.Equal(expectedfileContents, output);
+ }
}
}
Something went wrong with that request. Please try again.