Adding element token provider and HTML token filter.

This commit is contained in:
Sipke Schoorstra
2014-11-22 00:04:42 -08:00
parent 1a9e5f4681
commit 088e04b49d
4 changed files with 91 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using Orchard.Environment.Extensions;
using Orchard.Services;
using Orchard.Tokens;
namespace Orchard.Layouts.Filters {
// TODO: Fix the version that lives in Orchard.Tokens.Filters.
[OrchardFeature("Orchard.Layouts.Tokens")]
public class TokensFilter : IHtmlFilter {
private readonly ITokenizer _tokenizer;
public TokensFilter(ITokenizer tokenizer) {
_tokenizer = tokenizer;
}
public string ProcessContent(string text, string flavor) {
return TokensReplace(text);
}
private string TokensReplace(string text) {
if (String.IsNullOrEmpty(text))
return "";
if (!text.Contains("#{")) {
return text;
}
var data = new Dictionary<string, object>();
text = _tokenizer.Replace(text, data, new ReplaceOptions {Encoding = ReplaceOptions.NoEncode});
return text;
}
}
}

View File

@@ -27,3 +27,8 @@ Features:
Description: Adds a Projection element to the system.
Category: Layout
Dependencies: Orchard.Layouts, Orchard.Projections
Orchard.Layouts.Tokens:
Name: Element Tokens
Description: Provides an element token provider, enabling elements to be rendered using a token.
Category: Layout
Dependencies: Orchard.Layouts, Orchard.Tokens

View File

@@ -214,6 +214,7 @@
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\BlueprintAdminController.cs" />
<Compile Include="Filters\TokensFilter.cs" />
<Compile Include="Framework\Drivers\ImportContentContextWrapper.cs" />
<Compile Include="Framework\Drivers\IContentImportSession.cs" />
<Compile Include="Framework\Drivers\ExportElementContext.cs" />
@@ -243,6 +244,7 @@
<Compile Include="Services\ICurrentControllerAccessor.cs" />
<Compile Include="Services\IElementEventHandler.cs" />
<Compile Include="Signals.cs" />
<Compile Include="Tokens\ElementTokens.cs" />
<Compile Include="ViewModels\ElementBlueprintPropertiesViewModel.cs" />
<Compile Include="ViewModels\CreateElementBlueprintViewModel.cs" />
<Compile Include="Models\ElementBlueprint.cs" />
@@ -385,6 +387,7 @@
<ItemGroup>
<Content Include="Views\Parts.Layout.Summary.cshtml" />
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -0,0 +1,46 @@
using System;
using Orchard.DisplayManagement;
using Orchard.Environment.Extensions;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Services;
using Orchard.Tokens;
namespace Orchard.Layouts.Tokens {
[OrchardFeature("Orchard.Layouts.Tokens")]
public class ElementTokens : Component, ITokenProvider {
private readonly IElementManager _elementManager;
private readonly IElementDisplay _elementDisplay;
private readonly IShapeDisplay _shapeDisplay;
public ElementTokens(IElementManager elementManager, IElementDisplay elementDisplay, IShapeDisplay shapeDisplay) {
_elementManager = elementManager;
_elementDisplay = elementDisplay;
_shapeDisplay = shapeDisplay;
}
public void Describe(DescribeContext context) {
context.For("Element", T("Element"), T("Element tokens."))
.Token("Display:*", T("Display:<element type>"), T("Displays the specified element type."))
;
}
public void Evaluate(EvaluateContext context) {
context.For("Element", "")
.Token(token => token.StartsWith("Display:", StringComparison.OrdinalIgnoreCase) ? token.Substring("Display:".Length) : null, TokenValue);
}
private string TokenValue(string elementTypeName, string value) {
var describeContext = DescribeElementsContext.Empty;
var elementDescriptor = _elementManager.GetElementDescriptorByTypeName(describeContext, elementTypeName);
if (elementDescriptor == null)
return "";
var element = _elementManager.ActivateElement(elementDescriptor);
var elementShape = _elementDisplay.DisplayElement(element, null);
var elementHtml = _shapeDisplay.Display(elementShape);
return elementHtml;
}
}
}