Skip to content

Commit

Permalink
Adding shape_cache liquid tag (OrchardCMS#3194)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored and deanmarcussen committed Feb 22, 2019
1 parent a869f67 commit ce3a078
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Liquid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ Input

### `shape`

Renders a specific named tag with its properties
Creates and renders a new shape, with optional caching arguments.

Input

Expand All @@ -534,6 +534,18 @@ When using the shape tag a specific wrapper and / or alternate can be specified.
{% shape "menu", alias: "alias:main-menu", alternate: "Menu_Footer" %}
```

### `shape_cache`

Sets the caching parameters of a shape.

Input

```liquid
{% shape_cache my_shape cache_id: "my-shape", cache_expires_after: "00:05:00" %}
```

For more information about the available caching paramters please refer to [this section](../OrchardCore.DynamicCache/#shape-tag-helper-attributes)

### `zone`

Renders some HTML content in the specified zone.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static LiquidViewTemplate()
Factory.RegisterTag<ShapeTypeTag>("shape_type");
Factory.RegisterTag<ShapeDisplayTypeTag>("shape_display_type");
Factory.RegisterTag<ShapePositionTag>("shape_position");
Factory.RegisterTag<ShapeCacheTag>("shape_cache");
Factory.RegisterTag<ShapeTabTag>("shape_tab");
Factory.RegisterTag<ShapeRemoveItemTag>("shape_remove_item");
Factory.RegisterTag<ShapePagerTag>("shape_pager");
Expand Down
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;
}
}
}

0 comments on commit ce3a078

Please sign in to comment.