Skip to content

Commit

Permalink
Merge pull request #525 from bradwyoung/post-tag-helpers
Browse files Browse the repository at this point in the history
New and Enhanced Post Tag helpers
  • Loading branch information
poppastring authored Jan 12, 2021
2 parents a38798f + 82715bf commit 827a146
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PostEditLinkTagHelper : TagHelper
{
public PostViewModel Post { get; set; }
public string BlogPostId { get; set; }
public string EditLinkText { get; set; } = "Edit this post";

private readonly IDasBlogSettings dasBlogSettings;

Expand All @@ -27,7 +28,11 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.TagName = "a";
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("href", dasBlogSettings.GetPermaLinkUrl(BlogPostId + "/edit"));
output.Content.SetHtmlContent("Edit this post");
if (!string.IsNullOrEmpty(EditLinkText))
{
output.Content.SetHtmlContent("Edit this post");
}

}

public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
Expand Down
37 changes: 37 additions & 0 deletions source/DasBlog.Web.UI/TagHelpers/Post/PostImageTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading.Tasks;
using DasBlog.Services;
using DasBlog.Web.Models.BlogViewModels;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace DasBlog.Web.TagHelpers.Post
{
public class PostImageTagHelper: TagHelper

{
public PostViewModel Post { get; set; }

public string Css { get; set; }

private readonly IDasBlogSettings dasBlogSettings;

public PostImageTagHelper(IDasBlogSettings dasBlogSettings)
{
this.dasBlogSettings = dasBlogSettings;
}

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagMode = TagMode.StartTagAndEndTag;
output.TagName = "img";

if (!string.IsNullOrEmpty(Css))
{
output.Attributes.SetAttribute("class", Css);
}

output.Attributes.SetAttribute("src", dasBlogSettings.RelativeToRoot(Post.ImageUrl));
output.Attributes.SetAttribute("alt", Post.Title);
await Task.CompletedTask;
}
}
}
39 changes: 39 additions & 0 deletions source/DasBlog.Web.UI/TagHelpers/Post/PostLinkTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DasBlog.Services;
using DasBlog.Web.Models.BlogViewModels;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace DasBlog.Web.TagHelpers.Post
{
public class PostLinkTagHelper : TagHelper

{
public PostViewModel Post { get; set; }

public string Css { get; set; }

private readonly IDasBlogSettings dasBlogSettings;

public PostLinkTagHelper(IDasBlogSettings dasBlogSettings)
{
this.dasBlogSettings = dasBlogSettings;
}

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagMode = TagMode.StartTagAndEndTag;
output.TagName = "a";

if (!string.IsNullOrEmpty(Css))
{
output.Attributes.SetAttribute("class", Css);
}

output.Attributes.SetAttribute("href", dasBlogSettings.RelativeToRoot(Post.PermaLink));
await Task.CompletedTask;
}
}
}

0 comments on commit 827a146

Please sign in to comment.