Skip to content

Commit

Permalink
Make ResourcesTagHelper extensible (#16329)
Browse files Browse the repository at this point in the history
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
  • Loading branch information
sarahelsaig and hishamco authored Jul 24, 2024
1 parent 79a0853 commit 84ae5cb
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,31 @@ public class ResourcesTag
public static async ValueTask<Completion> WriteToAsync(IReadOnlyList<FilterArgument> argumentsList, TextWriter writer, TextEncoder _, TemplateContext context)
{
var services = ((LiquidTemplateContext)context).Services;
var resourceManager = services.GetRequiredService<IResourceManager>();
var processors = services.GetRequiredService<IEnumerable<IResourcesTagHelperProcessor>>();

var type = ResourceType.Footer;
var processorContext = new ResourcesTagHelperProcessorContext(ResourceTagType.Footer, writer);

foreach (var argument in argumentsList)
{
switch (argument.Name)
{
#pragma warning disable CA1806 // Do not ignore method results
case "type": Enum.TryParse((await argument.Expression.EvaluateAsync(context)).ToStringValue(), out type); break;
#pragma warning restore CA1806 // Do not ignore method results
case "type":
var typeString = (await argument.Expression.EvaluateAsync(context)).ToStringValue();
if (Enum.TryParse<ResourceTagType>(typeString, out var type))
{
processorContext = processorContext with { Type = type };
}

break;
}
}

switch (type)
foreach (var processor in processors)
{
case ResourceType.Meta:
resourceManager.RenderMeta(writer);
break;

case ResourceType.HeadLink:
resourceManager.RenderHeadLink(writer);
break;

case ResourceType.Stylesheet:
resourceManager.RenderStylesheet(writer);
break;

case ResourceType.HeadScript:
resourceManager.RenderHeadScript(writer);
break;

case ResourceType.FootScript:
resourceManager.RenderFootScript(writer);
break;

case ResourceType.Header:
resourceManager.RenderMeta(writer);
resourceManager.RenderHeadLink(writer);
resourceManager.RenderStylesheet(writer);
resourceManager.RenderHeadScript(writer);
break;

case ResourceType.Footer:
resourceManager.RenderFootScript(writer);
break;

default:
break;
await processor.ProcessAsync(processorContext);
}

return Completion.Normal;
}

public enum ResourceType
{
Meta,
HeadLink,
Stylesheet,
HeadScript,
FootScript,
Header,
Footer
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OrchardCore.ResourceManagement;

namespace OrchardCore.Resources.Services;

public class ResourcesTagHelperProcessor : IResourcesTagHelperProcessor
{
private readonly IResourceManager _resourceManager;
private readonly ILogger<ResourcesTagHelperProcessor> _logger;

public ResourcesTagHelperProcessor(IResourceManager resourceManager, ILogger<ResourcesTagHelperProcessor> logger)
{
_resourceManager = resourceManager;
_logger = logger;
}

public Task ProcessAsync(ResourcesTagHelperProcessorContext context)
{
switch (context.Type)
{
case ResourceTagType.Meta:
_resourceManager.RenderMeta(context.Writer);
break;
case ResourceTagType.HeadLink:
_resourceManager.RenderHeadLink(context.Writer);
break;
case ResourceTagType.Stylesheet:
_resourceManager.RenderStylesheet(context.Writer);
break;
case ResourceTagType.HeadScript:
_resourceManager.RenderHeadScript(context.Writer);
break;
case ResourceTagType.FootScript:
_resourceManager.RenderFootScript(context.Writer);
break;
case ResourceTagType.Header:
_resourceManager.RenderMeta(context.Writer);
_resourceManager.RenderHeadLink(context.Writer);
_resourceManager.RenderStylesheet(context.Writer);
_resourceManager.RenderHeadScript(context.Writer);
break;
case ResourceTagType.Footer:
_resourceManager.RenderFootScript(context.Writer);
break;
default:
_logger.LogWarning("Unknown {TypeName} value \"{Value}\".", nameof(ResourceTagType), context.Type);
break;
}

return Task.CompletedTask;
}
}
3 changes: 3 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Resources/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OrchardCore.Modules;
using OrchardCore.ResourceManagement;
using OrchardCore.Resources.Liquid;
using OrchardCore.Resources.Services;

namespace OrchardCore.Resources
{
Expand Down Expand Up @@ -34,6 +35,8 @@ public override void ConfigureServices(IServiceCollection serviceCollection)

var resourceConfiguration = _shellConfiguration.GetSection("OrchardCore_Resources");
serviceCollection.Configure<ResourceOptions>(resourceConfiguration);

serviceCollection.AddScoped<IResourcesTagHelperProcessor, ResourcesTagHelperProcessor>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;

namespace OrchardCore.ResourceManagement;

/// <summary>
/// Processes resources in the <c>&lt;resources /&gt;</c> tag helper.
/// </summary>
public interface IResourcesTagHelperProcessor
{
/// <summary>
/// Invoked when rendering registered resources.
/// </summary>
Task ProcessAsync(ResourcesTagHelperProcessorContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace OrchardCore.ResourceManagement;

/// <summary>
/// The possible <c>type</c> values of the <c>&lt;resources type="..." /&gt;</c> Razor tag helper and the
/// <c>{% resources type: "..." %}</c> liquid tag, indicating the kinds of resources to be rendered. The value should be
/// chosen based on the tag's location in the document.
/// </summary>
public enum ResourceTagType
{
/// <summary>
/// Resources that should be rendered along with <see cref="IResourceManager.RenderMeta"/>.
/// </summary>
Meta,

/// <summary>
/// Resources that should be rendered along with <see cref="IResourceManager.RenderHeadLink"/>.
/// </summary>
HeadLink,

/// <summary>
/// Resources that should be rendered along with <see cref="IResourceManager.RenderStylesheet"/>.
/// </summary>
Stylesheet,

/// <summary>
/// Resources that should be rendered along with <see cref="IResourceManager.RenderHeadScript"/>.
/// </summary>
HeadScript,

/// <summary>
/// Resources that should be rendered along with <see cref="IResourceManager.RenderFootScript"/>.
/// </summary>
FootScript,

/// <summary>
/// Resources that should be rendered inside the <c>/html/head</c> element.
/// </summary>
Header,

/// <summary>
/// Resources that should be rendered inside the <c>/html/head</c> element, near the end of the document.
/// </summary>
Footer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace OrchardCore.ResourceManagement;

/// <summary>
/// The context passed to <see cref="IResourcesTagHelperProcessor.ProcessAsync"/>, to render the
/// <c>&lt;resources /&gt;</c> Razor tag helper and the <c>{% resources %}</c> liquid tag.
/// </summary>
/// <param name="Type">The value indicating which types of resources to render.</param>
/// <param name="Writer">The object that writes the rendered content into the HTML output.</param>
public record ResourcesTagHelperProcessorContext(
ResourceTagType Type,
TextWriter Writer);
Original file line number Diff line number Diff line change
@@ -1,81 +1,45 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Text;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Logging;

namespace OrchardCore.ResourceManagement.TagHelpers
{
public enum ResourceType
{
Meta,
HeadLink,
Stylesheet,
HeadScript,
FootScript,
Header,
Footer
}

[HtmlTargetElement("resources", Attributes = nameof(Type))]
public class ResourcesTagHelper : TagHelper
{
public ResourceType Type { get; set; }

private readonly IResourceManager _resourceManager;
private readonly ILogger _logger;
private readonly IEnumerable<IResourcesTagHelperProcessor> _processors;

public ResourcesTagHelper(
IResourceManager resourceManager,
ILogger<ResourcesTagHelper> logger)
ILogger<ResourcesTagHelper> logger,
IEnumerable<IResourcesTagHelperProcessor> processors)
{
_resourceManager = resourceManager;
_logger = logger;
_processors = processors;
}

public override void Process(TagHelperContext tagHelperContext, TagHelperOutput output)
public ResourceTagType Type { get; set; }

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
try
{
using var sw = new ZStringWriter();

switch (Type)
{
case ResourceType.Meta:
_resourceManager.RenderMeta(sw);
break;
await using var writer = new ZStringWriter();

case ResourceType.HeadLink:
_resourceManager.RenderHeadLink(sw);
break;
var processorContext = new ResourcesTagHelperProcessorContext(Type, writer);

case ResourceType.Stylesheet:
_resourceManager.RenderStylesheet(sw);
break;

case ResourceType.HeadScript:
_resourceManager.RenderHeadScript(sw);
break;

case ResourceType.FootScript:
_resourceManager.RenderFootScript(sw);
break;

case ResourceType.Header:
_resourceManager.RenderMeta(sw);
_resourceManager.RenderHeadLink(sw);
_resourceManager.RenderStylesheet(sw);
_resourceManager.RenderHeadScript(sw);
break;

case ResourceType.Footer:
_resourceManager.RenderFootScript(sw);
break;

default:
break;
foreach (var processor in _processors)
{
await processor.ProcessAsync(processorContext);
}

output.Content.AppendHtml(sw.ToString());
output.Content.AppendHtml(writer.ToString());
}
catch (Exception ex)
{
Expand Down
4 changes: 4 additions & 0 deletions src/docs/releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ You may have to adjust your GraphQL queries in that case.

Additionally, the `GetAliases` method in the `IIndexAliasProvider` interface is now asynchronous and has been renamed to `GetAliasesAsync`. Implementations of this interface should be modified by updating the method signature and ensure they handle asynchronous operations correctly.

### Resource Management

Previously the `<resources type="..." />` Razor tag helper and the `{% resources type: "..." %}` Liquid tag were only capable of handling a hard-coded set of resource definition types (`script`, `stylesheet`, etc). Now both can be extended with `IResourcesTagHelperProcessor` to run custom rendering logic. To make this possible, the `OrchardCore.ResourceManagement.TagHelpers.ResourceType` and `OrchardCore.Resources.Liquid.ResourcesTag.ResourceType` enums have been replaced with a common `OrchardCore.ResourceManagement.ResourceTagType`. It was renamed to avoid confusion with `ResourceDefinition.Type`. This change is breaking in code, but it does not affect the uses of the Razor tag helper or the Liquid tag in templates.

## Change Logs

### Azure AI Search Module
Expand Down

0 comments on commit 84ae5cb

Please sign in to comment.