Skip to content

Commit 441ec54

Browse files
committed
Added template to PostList widget
1 parent 3cd0fa5 commit 441ec54

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

plugins/Common/Views/Widgets/PostList/Edit.cshtml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@{
55
var keyAuth = $"{Model.Theme}-{Model.Widget.Title}-auth";
66
var keyCat = $"{Model.Theme}-{Model.Widget.Title}-cat";
7+
var keyTmpl = $"{Model.Theme}-{Model.Widget.Title}-tmpl";
78
var keyMax = $"{Model.Theme}-{Model.Widget.Title}-max";
89

910
var authors = _db.Authors.All().Select(a => a.DisplayName).ToArray();
@@ -18,10 +19,12 @@
1819
var selectedAuth = _db.CustomFields.GetCustomValue(keyAuth);
1920
var selectedCat = _db.CustomFields.GetCustomValue(keyCat);
2021
var maxRecords = _db.CustomFields.GetCustomValue(keyMax);
22+
var template = _db.CustomFields.GetCustomValue(keyTmpl);
2123

2224
if (string.IsNullOrEmpty(selectedAuth)) { selectedAuth = "All"; }
2325
if (string.IsNullOrEmpty(selectedCat)) { selectedCat = "All"; }
2426
if (string.IsNullOrEmpty(maxRecords)) { maxRecords = "10"; }
27+
if (string.IsNullOrEmpty(template)) { template = "<a href=\"/posts/{0}\" class=\"list-group-item list-group-item-action\">{1}</a>"; }
2528
}
2629
<form method="post" action="~/widgets/api/postlist/edit" asp-antiforgery="true">
2730
<div class="form-group">
@@ -56,6 +59,10 @@
5659
}
5760
</select>
5861
</div>
62+
<div class="form-group">
63+
<label class="form-group-label">Template ({0} = slug {1} = title)</label>
64+
<textarea rows="2" id="txtTemplate" name="txtTemplate" class="form-control">@template</textarea>
65+
</div>
5966
<div class="form-group">
6067
<label class="form-group-label">Max records</label>
6168
<input type="text" id="txtMaxRecords" name="txtMaxRecords" class="form-control" value="@maxRecords" />
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
@model IEnumerable<BlogPost>
2-
<div class="list-group">
3-
@foreach (var post in Model)
4-
{
5-
<a href="~/posts/@post.Slug" class="list-group-item list-group-item-action" title="">@post.Title</a>
6-
}
7-
</div>
1+
@foreach (var item in Model.Posts)
2+
{
3+
@Html.Raw(string.Format(Model.Template, item.Slug, item.Title))
4+
}

plugins/Common/Widgets/PostList.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Core.Services;
1+
using Core.Data;
2+
using Core.Services;
23
using Microsoft.AspNetCore.Mvc;
34
using System;
5+
using System.Collections.Generic;
46
using System.Linq;
57

68
namespace Common.Widgets
@@ -20,14 +22,17 @@ public IViewComponentResult Invoke(string theme, string widget)
2022
var keyAuth = $"{theme}-{widget}-auth";
2123
var keyCat = $"{theme}-{widget}-cat";
2224
var keyMax = $"{theme}-{widget}-max";
25+
var keyTmpl = $"{theme}-{widget}-tmpl";
2326

2427
var selectedAuth = _db.CustomFields.GetCustomValue(keyAuth);
2528
var selectedCat = _db.CustomFields.GetCustomValue(keyCat);
2629
var maxRecords = _db.CustomFields.GetCustomValue(keyMax);
30+
var template = _db.CustomFields.GetCustomValue(keyTmpl);
2731

2832
if (selectedAuth == "All") { selectedAuth = ""; }
2933
if (selectedCat == "All") { selectedCat = ""; }
3034
if (string.IsNullOrEmpty(maxRecords)) { maxRecords = "10"; }
35+
if (string.IsNullOrEmpty(template)) { template = "<a href=\"/posts/{0}\" class=\"list-group-item list-group-item-action\">{1}</a>"; }
3136

3237
var posts = _db.BlogPosts.All()
3338
.Where(p => p.Published > DateTime.MinValue)
@@ -55,7 +60,7 @@ public IViewComponentResult Invoke(string theme, string widget)
5560
if (!int.TryParse(maxRecords, out maxRec))
5661
maxRec = 10;
5762

58-
var model = posts.Take(maxRec).ToList();
63+
var model = new PostListWidgetModel { Title = widget, Posts = posts.Take(maxRec).ToList(), Template = template };
5964

6065
return View("~/Views/Widgets/PostList/Index.cshtml", model);
6166
}
@@ -73,17 +78,26 @@ public PostListController(IDataService db)
7378

7479
[HttpPost]
7580
[Route("edit")]
76-
public IActionResult Edit(string selAuthors, string selCats, string txtMaxRecords, string hdnWidget, string hdnTheme)
81+
public IActionResult Edit(string selAuthors, string selCats, string txtMaxRecords, string txtTemplate, string hdnWidget, string hdnTheme)
7782
{
7883
var keyAuth = $"{hdnTheme}-{hdnWidget}-auth";
7984
var keyCat = $"{hdnTheme}-{hdnWidget}-cat";
8085
var keyMax = $"{hdnTheme}-{hdnWidget}-max";
86+
var keyTmpl = $"{hdnTheme}-{hdnWidget}-tmpl";
8187

8288
_db.CustomFields.SaveCustomValue(keyAuth, selAuthors);
8389
_db.CustomFields.SaveCustomValue(keyCat, selCats);
8490
_db.CustomFields.SaveCustomValue(keyMax, txtMaxRecords);
91+
_db.CustomFields.SaveCustomValue(keyTmpl, txtTemplate);
8592

8693
return Redirect("~/admin/settings/themes");
8794
}
8895
}
96+
97+
public class PostListWidgetModel
98+
{
99+
public string Title { get; set; }
100+
public string Template { get; set; }
101+
public List<BlogPost> Posts { get; set; }
102+
}
89103
}

src/App/app.db

0 Bytes
Binary file not shown.

src/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
<Version>2.1.1.0</Version>
5+
<Version>2.1.1.1</Version>
66
</PropertyGroup>
77

88
<ItemGroup>

0 commit comments

Comments
 (0)