mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Implementing a tokens html filter
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Environment.Extensions;
|
||||
using Orchard.Services;
|
||||
|
||||
namespace Orchard.Tokens.Filters.Services {
|
||||
|
||||
[OrchardFeature("Orchard.Tokens.HtmlFilter")]
|
||||
public class TokensFilter : ContentHandler, IHtmlFilter {
|
||||
|
||||
private readonly ITokenizer _tokenizer;
|
||||
private ContentItem _displayed;
|
||||
|
||||
public TokensFilter(ITokenizer tokenizer) {
|
||||
_tokenizer = tokenizer;
|
||||
}
|
||||
|
||||
protected override void BuildDisplayShape(BuildDisplayContext context) {
|
||||
_displayed = context.ContentItem;
|
||||
}
|
||||
|
||||
public string ProcessContent(string text, string flavor) {
|
||||
return TokensReplace(text);
|
||||
}
|
||||
|
||||
private string TokensReplace(string text) {
|
||||
if (_displayed == null) {
|
||||
return text;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return string.Empty;
|
||||
|
||||
// optimize code path if nothing to do
|
||||
if (!text.Contains("#{")) {
|
||||
return text;
|
||||
}
|
||||
|
||||
Dictionary<string, object> data = new Dictionary<string, object>() { { "Content", _displayed } };
|
||||
|
||||
text = _tokenizer.Replace(text, data);
|
||||
|
||||
_displayed = null;
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,4 +14,9 @@ Features:
|
||||
Name: Feeds Tokens
|
||||
Description: Provides a content part to customize RSS fields based on tokens.
|
||||
Category: Content
|
||||
Dependencies: Orchard.Tokens, Feeds
|
||||
Dependencies: Orchard.Tokens, Feeds
|
||||
Orchard.Tokens.HtmlFilter:
|
||||
Name: Tokens Html Filter
|
||||
Description: Evaluates tokens in a body.
|
||||
Category: Content
|
||||
Dependencies: Orchard.Tokens
|
||||
@@ -77,6 +77,7 @@
|
||||
<Content Include="Styles\orchard-tokens-admin.css" />
|
||||
<Content Include="Styles\Images\tokensPopup.gif" />
|
||||
<Content Include="Web.config" />
|
||||
<Compile Include="Filters\TokensFilter.cs" />
|
||||
<Compile Include="Migrations.cs" />
|
||||
<Compile Include="Models\RssPart.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Reference in New Issue
Block a user