-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to add Razor HTML content to the shape as a property (…
…#14867) Co-authored-by: Zoltán Lehóczky <[email protected]> Co-authored-by: Hisham Bin Ateya <[email protected]>
- Loading branch information
1 parent
af59baa
commit 79a0853
Showing
7 changed files
with
100 additions
and
4 deletions.
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
21 changes: 21 additions & 0 deletions
21
src/OrchardCore.Modules/OrchardCore.Demo/Views/EmbedContentInShape.cshtml
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,21 @@ | ||
@using Microsoft.AspNetCore.Html | ||
|
||
<div class="border"> | ||
<p>The content is inserted below this line.</p> | ||
<p>@Model.HtmlContent</p> | ||
<p>The content is inserted above this line.</p> | ||
|
||
@if (Model.SomeProperty != null) | ||
{ | ||
<p>And here is another property: @Model.SomeProperty.</p> | ||
} | ||
|
||
@if (Model.OtherContent is IHtmlContent otherContent) | ||
{ | ||
<div class="border"> | ||
<p>Another property called "OtherContent" starts here.</p> | ||
<p>@otherContent</p> | ||
<p>Another property called "OtherContent" ends here.</p> | ||
</div> | ||
} | ||
</div> |
23 changes: 23 additions & 0 deletions
23
src/OrchardCore.Modules/OrchardCore.Demo/Views/Home/AddProperty.cshtml
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,23 @@ | ||
@using Microsoft.AspNetCore.Html | ||
|
||
<shape type="EmbedContentInShape"> | ||
<add-property name="HtmlContent"> | ||
The inner HTML of the <code><add-property name="propertyName"></code> tag helper (which is a direct child | ||
of the <code><shape></code> tag helper) is converted into HTML and then passed to the shape as a model | ||
property. The property's name is the string passed into the <code>name</code> attribute. | ||
<shape type="TestContentPartA" Line="Sample Data" Creating="Now" Processing="Later"/> | ||
Even other shapes can be included! | ||
</add-property> | ||
<add-property name="OtherContent"> | ||
You can have multiple, they are just <code>@nameof(IHtmlContent)</code> type shape properties. | ||
</add-property> | ||
<add-property name="SomeProperty" value="Some value passed to the attribute." /> | ||
</shape> | ||
|
||
<shape type="EmbedContentInShape" HtmlContent="This property is ignored."> | ||
<add-property name="HtmlContent"> | ||
If a <code><add-property></code> tag helper exists, it takes precedence over any matching attribute of the | ||
<code><shape></code> tag helper. This is because child tag helpers are evaluated after the shape is | ||
created, right before it's displayed. | ||
</add-property> | ||
</shape> |
28 changes: 28 additions & 0 deletions
28
src/OrchardCore/OrchardCore.DisplayManagement/TagHelpers/AddPropertyTagHelper.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,28 @@ | ||
using Microsoft.AspNetCore.Html; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using System.Threading.Tasks; | ||
|
||
namespace OrchardCore.DisplayManagement.TagHelpers; | ||
|
||
[HtmlTargetElement("add-property", Attributes = "name", TagStructure = TagStructure.NormalOrSelfClosing)] | ||
public class AddPropertyTagHelper : TagHelper | ||
{ | ||
[HtmlAttributeName("name")] | ||
public string Name { get; set; } | ||
|
||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
if (string.IsNullOrWhiteSpace(Name)) | ||
{ | ||
return; | ||
} | ||
|
||
var content = await output.GetChildContentAsync(useCachedResult: false); | ||
var shape = (IShape)context.Items[typeof(IShape)]; | ||
shape.Properties[Name.Trim()] = output.Attributes.ContainsName("value") | ||
? output.Attributes["value"].Value | ||
: new HtmlString(content.GetContent()); | ||
|
||
output.SuppressOutput(); | ||
} | ||
} |
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
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