Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Remove IHtmlHelper extension and prepare TagHelper - MetaDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Aug 7, 2020
1 parent d461b88 commit b5852a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
27 changes: 27 additions & 0 deletions Grand.Framework/TagHelpers/MetaDescriptionTagHelper.cs
@@ -0,0 +1,27 @@
using Grand.Framework.UI;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System.Threading.Tasks;

namespace Grand.Framework.TagHelpers
{
[HtmlTargetElement("meta-description", TagStructure = TagStructure.WithoutEndTag)]
public class MetaDescriptionTagHelper : TagHelper
{

private readonly IPageHeadBuilder _pageHeadBuilder;

public MetaDescriptionTagHelper(IPageHeadBuilder pageHeadBuilder)
{
_pageHeadBuilder = pageHeadBuilder;
}

public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "meta";
output.Attributes.Add("name", "description");
output.Attributes.Add("content", _pageHeadBuilder.GenerateMetaDescription());
return Task.CompletedTask;
}

}
}
23 changes: 0 additions & 23 deletions Grand.Framework/UI/LayoutExtensions.cs
Expand Up @@ -31,29 +31,6 @@ public static void AddMetaDescriptionParts(this IHtmlHelper html, string part)
var pageHeadBuilder = html.ViewContext.HttpContext.RequestServices.GetRequiredService<IPageHeadBuilder>();
pageHeadBuilder.AddMetaDescriptionParts(part);
}
/// <summary>
/// Append meta description element to the <![CDATA[<head>]]>
/// </summary>
/// <param name="html">HTML helper</param>
/// <param name="part">Meta description part</param>
public static void AppendMetaDescriptionParts(this IHtmlHelper html, string part)
{
var pageHeadBuilder = html.ViewContext.HttpContext.RequestServices.GetRequiredService<IPageHeadBuilder>();
pageHeadBuilder.AppendMetaDescriptionParts(part);
}
/// <summary>
/// Generate all description parts
/// </summary>
/// <param name="html">HTML helper</param>
/// <param name="part">Meta description part</param>
/// <returns>Generated string</returns>
public static IHtmlContent GrandMetaDescription(this IHtmlHelper html, string part = "")
{
var pageHeadBuilder = html.ViewContext.HttpContext.RequestServices.GetRequiredService<IPageHeadBuilder>();
html.AppendMetaDescriptionParts(part);
return new HtmlString(html.Encode(pageHeadBuilder.GenerateMetaDescription()));
}


/// <summary>
/// Add meta keyword element to the <![CDATA[<head>]]>
Expand Down
2 changes: 1 addition & 1 deletion Grand.Web/Views/Shared/_Root.Head.cshtml
Expand Up @@ -19,7 +19,7 @@
<head>
<title asp-default-title="true"></title>
<meta charset="utf-8">
<meta name="description" content="@(Html.GrandMetaDescription())">
<meta-description>
<meta name="keywords" content="@(Html.GrandMetaKeywords())">
<meta name="generator" content="grandnode">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
Expand Down
2 changes: 1 addition & 1 deletion Grand.Web/Views/Shared/_RootPopup.cshtml
Expand Up @@ -3,7 +3,7 @@
<head>
<title asp-default-title="true"></title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<meta name="description" content="@(Html.GrandMetaDescription())" />
<meta-description>
<meta name="keywords" content="@(Html.GrandMetaKeywords())" />
@Html.GrandHeadCustom()
@*This is used so that themes can inject content into the header*@
Expand Down

0 comments on commit b5852a4

Please sign in to comment.