Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<PackageVersion Include="OrchardCoreContrib.OpenApi.Abstractions" Version="1.4.0" />
<PackageVersion Include="OrchardCoreContrib.Testing" Version="1.0.0-beta3" />
<PackageVersion Include="OrchardCoreContrib.Templating.Abstractions" Version="1.0.0" />
<PackageVersion Include="RazorLight" Version="2.3.1" />
<PackageVersion Include="Scalar.AspNetCore" Version="2.14.14" />
<PackageVersion Include="SendGrid" Version="9.29.3" />
<PackageVersion Include="SkiaSharp" Version="3.119.2" />
Expand Down
2 changes: 2 additions & 0 deletions OrchardCoreContrib.Modules.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<Project Path="src/OrchardCoreContrib.Sms.Azure/OrchardCoreContrib.Sms.Azure.csproj" />
<Project Path="src/OrchardCoreContrib.System/OrchardCoreContrib.System.csproj" />
<Project Path="src/OrchardCoreContrib.Templating.Liquid/OrchardCoreContrib.Templating.Liquid.csproj" />
<Project Path="src/OrchardCoreContrib.Templating.Razor/OrchardCoreContrib.Templating.Razor.csproj" />
<Project Path="src/OrchardCoreContrib.Tenants/OrchardCoreContrib.Tenants.csproj" />
<Project Path="src/OrchardCoreContrib.Themes.Admin/OrchardCoreContrib.Themes.Admin.csproj" />
<Project Path="src/OrchardCoreContrib.UserGroups/OrchardCoreContrib.UserGroups.csproj" />
Expand All @@ -47,6 +48,7 @@
<Project Path="test/OrchardCoreContrib.HealthChecks.Tests/OrchardCoreContrib.HealthChecks.Tests.csproj" />
<Project Path="test/OrchardCoreContrib.Modules.Tests/OrchardCoreContrib.Modules.Tests.csproj" />
<Project Path="test/OrchardCoreContrib.Templating.Liquid.Tests/OrchardCoreContrib.Templating.Liquid.Tests.csproj" />
<Project Path="test/OrchardCoreContrib.Templating.Razor.Tests/OrchardCoreContrib.Templating.Razor.Tests.csproj" />
<Project Path="test/OrchardCoreContrib.UserGroups.Tests/OrchardCoreContrib.UserGroups.Tests.csproj" />
</Folder>
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<ProjectReference Include="..\OrchardCoreContrib.Sms.Azure\OrchardCoreContrib.Sms.Azure.csproj" />
<ProjectReference Include="..\OrchardCoreContrib.System\OrchardCoreContrib.System.csproj" />
<ProjectReference Include="..\OrchardCoreContrib.Templating.Liquid\OrchardCoreContrib.Templating.Liquid.csproj" />
<ProjectReference Include="..\OrchardCoreContrib.Templating.Razor\OrchardCoreContrib.Templating.Razor.csproj" />
<ProjectReference Include="..\OrchardCoreContrib.Tenants\OrchardCoreContrib.Tenants.csproj" />
<ProjectReference Include="..\OrchardCoreContrib.Themes.Admin\OrchardCoreContrib.Themes.Admin.csproj" />
<ProjectReference Include="..\OrchardCoreContrib.UserGroups\OrchardCoreContrib.UserGroups.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using OrchardCoreContrib.Templating;
using OrchardCoreContrib.Templating.Razor.Services;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Provides extension methods for registering the Razor template engine services in an <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers the Razor template engine services in the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddRazorTemplating(this IServiceCollection services)
{
services.AddSingleton<ITemplateEngine, RazorTemplateEngine>();

return services;
}
}
11 changes: 11 additions & 0 deletions src/OrchardCoreContrib.Templating.Razor/Manifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using OrchardCore.Modules.Manifest;
using ManifestConstants = OrchardCoreContrib.Modules.Manifest.ManifestConstants;

[assembly: Module(
Name = "Razor Templating",
Author = ManifestConstants.Author,
Website = ManifestConstants.Website,
Version = "1.0.0",
Description = "Provides Razor template engine integration.",
Category = "Templating"
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Module.Targets" />
<PackageReference Include="RazorLight" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(OrchardCoreContribPath)OrchardCoreContrib.Abstractions\OrchardCoreContrib.Abstractions.csproj" />
<ProjectReference Include="$(OrchardCoreContribPath)OrchardCoreContrib.Infrastructure.Abstractions\OrchardCoreContrib.Infrastructure.Abstractions.csproj" />
<ProjectReference Include="$(OrchardCoreContribPath)OrchardCoreContrib.Templating.Abstractions\OrchardCoreContrib.Templating.Abstractions.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Localization;
using OrchardCoreContrib.Infrastructure;
using RazorLight;

namespace OrchardCoreContrib.Templating.Razor.Services;

/// <summary>
/// Represents a template engine that uses the Razor templating language.
/// </summary>
public class RazorTemplateEngine(IStringLocalizer<RazorTemplateEngine> S) : ITemplateEngine
{
private readonly RazorLightEngine _engine = new RazorLightEngineBuilder()
.UseMemoryCachingProvider()
.Build();

/// <inheritdoc />
public async Task<Result<string>> RenderAsync(string template, TemplateContext context)
{
Guard.ArgumentNotNullOrEmpty(template, nameof(template));
Guard.ArgumentNotNull(context, nameof(context));

try
{
var result = await _engine.CompileRenderStringAsync(Guid.NewGuid().ToString(), template, context.Model);

return Result.Success(result);
}
catch (Exception ex)
{
return Result.Failed<string>(S["Rendering razor template failed: {0}", ex.Message]);
}
}
}
9 changes: 9 additions & 0 deletions src/OrchardCoreContrib.Templating.Razor/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Modules;

namespace OrchardCoreContrib.Templating.Liquid;

public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services) => services.AddRazorTemplating();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using OrchardCoreContrib.Templating.Razor.Services;

namespace OrchardCoreContrib.Templating.Razor.Tests.Extensions;

public class ServiceCollectionExtensionsTests
{
[Fact]
public void AddRazorTemplating_RegistersRazorTemplateEngineAsSingleton()
{
// Arrange
var services = new ServiceCollection()
.AddLogging()
.AddLocalization();

// Act
services.AddRazorTemplating();

// Assert
using var provider = services.BuildServiceProvider();

var first = provider.GetRequiredService<ITemplateEngine>();
var second = provider.GetRequiredService<ITemplateEngine>();

Assert.IsType<RazorTemplateEngine>(first);
Assert.Same(first, second);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="Testcontainers.Redis" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\OrchardCoreContrib.Templating.Razor\OrchardCoreContrib.Templating.Razor.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Extensions.Localization;
using Moq;

namespace OrchardCoreContrib.Templating.Razor.Services.Tests;

public class RazorTemplateEngineTests
{
private readonly RazorTemplateEngine _templateEngine;

public RazorTemplateEngineTests()
{
var stringLocalizerMock = new Mock<IStringLocalizer<RazorTemplateEngine>>();

stringLocalizerMock.Setup(localizer => localizer[It.IsAny<string>()])
.Returns((string k) => new LocalizedString(k, k));

stringLocalizerMock.Setup(localizer => localizer[It.IsAny<string>(), It.IsAny<object[]>()])
.Returns((string k, object[] a) => new LocalizedString(k, string.Format(k, a)));

_templateEngine = new RazorTemplateEngine(stringLocalizerMock.Object);
}

[Fact]
public async Task RenderAsync_ShouldRenderTemplate_WhenTemplateIsValid()
{
// Arrange
var template = "Hello @Model.Name";
var context = new TemplateContext(new { Name = "World" });

// Act
var result = await _templateEngine.RenderAsync(template, context);

// Assert
Assert.Equal("Hello World", result.Value);
}

[Fact]
public async Task RenderAsync_ShouldThrowTemplateRenderException_WhenTemplateParsingFails()
{
// Arrange
var template = "@{ var x = ; }";
var context = new TemplateContext();

// Act
var result = await _templateEngine.RenderAsync(template, context);

// Assert
Assert.NotEmpty(result.Errors);
Assert.StartsWith("Rendering razor template failed:", result.Errors.Single().Message);
}
}
Loading