forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding shape_cache liquid tag (OrchardCMS#3194)
- Loading branch information
1 parent
a869f67
commit ce3a078
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/OrchardCore/OrchardCore.DisplayManagement.Liquid/Tags/ShapeCacheTag.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Fluid; | ||
using Fluid.Ast; | ||
using OrchardCore.Liquid.Ast; | ||
|
||
namespace OrchardCore.DisplayManagement.Liquid.Tags | ||
{ | ||
public class ShapeCacheTag : ExpressionArgumentsTag | ||
{ | ||
private static readonly char[] Separators = { ',', ' ' }; | ||
|
||
public override async Task<Completion> WriteToAsync(TextWriter writer, TextEncoder encoder, TemplateContext context, Expression expression, FilterArgument[] args) | ||
{ | ||
var objectValue = (await expression.EvaluateAsync(context)).ToObjectValue(); | ||
|
||
if (objectValue is IShape shape) | ||
{ | ||
var arguments = (FilterArguments)(await new ArgumentsExpression(args).EvaluateAsync(context)).ToObjectValue(); | ||
|
||
var metadata = shape.Metadata; | ||
|
||
|
||
if (arguments.HasNamed("cache_id")) | ||
{ | ||
metadata.Cache(arguments["cache_id"].ToStringValue()); | ||
} | ||
|
||
if (arguments.HasNamed("cache_context")) | ||
{ | ||
var contexts = arguments["cache_context"].ToStringValue().Split(Separators, StringSplitOptions.RemoveEmptyEntries); | ||
metadata.Cache().AddContext(contexts); | ||
} | ||
|
||
if (arguments.HasNamed("cache_tag")) | ||
{ | ||
var tags = arguments["cache_tag"].ToStringValue().Split(Separators, StringSplitOptions.RemoveEmptyEntries); | ||
metadata.Cache().AddTag(tags); | ||
} | ||
|
||
if (arguments.HasNamed("cache_fixed_duration")) | ||
{ | ||
if (TimeSpan.TryParse(arguments["cache_fixed_duration"].ToStringValue(), out var timespan)) | ||
{ | ||
metadata.Cache().WithExpiryAfter(timespan); | ||
} | ||
} | ||
|
||
if (arguments.HasNamed("cache_sliding_duration")) | ||
{ | ||
if (TimeSpan.TryParse(arguments["cache_sliding_duration"].ToStringValue(), out var timespan)) | ||
{ | ||
metadata.Cache().WithExpiryAfter(timespan); | ||
} | ||
} | ||
} | ||
|
||
return Completion.Normal; | ||
} | ||
} | ||
} |