Skip to content

Commit 827a146

Browse files
authored
Merge pull request #525 from bradwyoung/post-tag-helpers
New and Enhanced Post Tag helpers
2 parents a38798f + 82715bf commit 827a146

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

source/DasBlog.Web.UI/TagHelpers/Post/PostEditLinkTagHelper.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class PostEditLinkTagHelper : TagHelper
99
{
1010
public PostViewModel Post { get; set; }
1111
public string BlogPostId { get; set; }
12+
public string EditLinkText { get; set; } = "Edit this post";
1213

1314
private readonly IDasBlogSettings dasBlogSettings;
1415

@@ -27,7 +28,11 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
2728
output.TagName = "a";
2829
output.TagMode = TagMode.StartTagAndEndTag;
2930
output.Attributes.SetAttribute("href", dasBlogSettings.GetPermaLinkUrl(BlogPostId + "/edit"));
30-
output.Content.SetHtmlContent("Edit this post");
31+
if (!string.IsNullOrEmpty(EditLinkText))
32+
{
33+
output.Content.SetHtmlContent("Edit this post");
34+
}
35+
3136
}
3237

3338
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Threading.Tasks;
2+
using DasBlog.Services;
3+
using DasBlog.Web.Models.BlogViewModels;
4+
using Microsoft.AspNetCore.Razor.TagHelpers;
5+
6+
namespace DasBlog.Web.TagHelpers.Post
7+
{
8+
public class PostImageTagHelper: TagHelper
9+
10+
{
11+
public PostViewModel Post { get; set; }
12+
13+
public string Css { get; set; }
14+
15+
private readonly IDasBlogSettings dasBlogSettings;
16+
17+
public PostImageTagHelper(IDasBlogSettings dasBlogSettings)
18+
{
19+
this.dasBlogSettings = dasBlogSettings;
20+
}
21+
22+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
23+
{
24+
output.TagMode = TagMode.StartTagAndEndTag;
25+
output.TagName = "img";
26+
27+
if (!string.IsNullOrEmpty(Css))
28+
{
29+
output.Attributes.SetAttribute("class", Css);
30+
}
31+
32+
output.Attributes.SetAttribute("src", dasBlogSettings.RelativeToRoot(Post.ImageUrl));
33+
output.Attributes.SetAttribute("alt", Post.Title);
34+
await Task.CompletedTask;
35+
}
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using DasBlog.Services;
6+
using DasBlog.Web.Models.BlogViewModels;
7+
using Microsoft.AspNetCore.Razor.TagHelpers;
8+
9+
namespace DasBlog.Web.TagHelpers.Post
10+
{
11+
public class PostLinkTagHelper : TagHelper
12+
13+
{
14+
public PostViewModel Post { get; set; }
15+
16+
public string Css { get; set; }
17+
18+
private readonly IDasBlogSettings dasBlogSettings;
19+
20+
public PostLinkTagHelper(IDasBlogSettings dasBlogSettings)
21+
{
22+
this.dasBlogSettings = dasBlogSettings;
23+
}
24+
25+
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
26+
{
27+
output.TagMode = TagMode.StartTagAndEndTag;
28+
output.TagName = "a";
29+
30+
if (!string.IsNullOrEmpty(Css))
31+
{
32+
output.Attributes.SetAttribute("class", Css);
33+
}
34+
35+
output.Attributes.SetAttribute("href", dasBlogSettings.RelativeToRoot(Post.PermaLink));
36+
await Task.CompletedTask;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)