Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link target to Alloy templates #46

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions templates/Alloy.Mvc/Helpers/HtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public MenuItem(PageData page)
/// Returns a ConditionalLink object which when disposed will write a closing <![CDATA[ </a> ]]> tag
/// to the response if the shouldWriteLink argument is true.
/// </summary>
public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool shouldWriteLink, string url, string title = null, string cssClass = null)
public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool shouldWriteLink, string url,
string title = null, string cssClass = null, string linkTarget = null)
{
if (shouldWriteLink)
{
Expand All @@ -123,6 +124,11 @@ public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool
linkTag.Attributes.Add("class", cssClass);
}

if (!string.IsNullOrWhiteSpace(linkTarget))
{
linkTag.Attributes.Add("target", linkTarget);
}

helper.ViewContext.Writer.Write(linkTag.RenderStartTag());
}
return new ConditionalLink(helper.ViewContext, shouldWriteLink);
Expand All @@ -137,7 +143,8 @@ public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool
/// Overload which only executes the delegate for retrieving the URL if the link should be written.
/// This may be used to prevent null reference exceptions by adding null checkes to the shouldWriteLink condition.
/// </remarks>
public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool shouldWriteLink, Func<string> urlGetter, string title = null, string cssClass = null)
public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool shouldWriteLink,
Func<string> urlGetter, string title = null, string cssClass = null, string linkTarget = null)
{
var url = string.Empty;

Expand All @@ -146,7 +153,7 @@ public static ConditionalLink BeginConditionalLink(this IHtmlHelper helper, bool
url = urlGetter();
}

return helper.BeginConditionalLink(shouldWriteLink, url, title, cssClass);
return helper.BeginConditionalLink(shouldWriteLink, url, title, cssClass, linkTarget);
}

public class ConditionalLink : IDisposable
Expand Down
17 changes: 17 additions & 0 deletions templates/Alloy.Mvc/Helpers/UrlHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EPiServer.ServiceLocation;
using EPiServer.Shell.Navigation;
using EPiServer.Web.Routing;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -55,4 +56,20 @@ public static string PageLinkUrl(this IUrlHelper urlHelper, PageData page)
}
return string.Empty;
}

/// <summary>
/// Returns the URL anchor target for a page with shortcut settings
/// </summary>
public static string PageLinkTarget(this IUrlHelper urlHelper, PageData page)
{
return page.LinkType switch
{
PageShortcutType.Normal => "",
PageShortcutType.Inactive => "",
PageShortcutType.FetchData => page.TargetFrameName,
PageShortcutType.Shortcut => page.TargetFrameName,
PageShortcutType.External => page.TargetFrameName,
_ => throw new ArgumentOutOfRangeException($"Unknown link type: {page.LinkType}'")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@model SitePageData

<div>
@using (Html.BeginConditionalLink(Model.HasTemplate(), Url.PageLinkUrl(Model), Model.PageName))
@using (Html.BeginConditionalLink(Model.HasTemplate(), Url.PageLinkUrl(Model), Model.PageName, null, Url.PageLinkTarget(Model)))
{
<div class="img-wrapper mb-3">
@Html.DisplayFor(m => m.PageImage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img class="pb-3 pb-lg-0" src="@Url.ContentUrl(Model.PageImage)" />
</div>
<div class="col-sm-12 col-lg-6">
@using (Html.BeginConditionalLink(Model.HasTemplate(), Url.PageLinkUrl(Model), Model.PageName))
@using (Html.BeginConditionalLink(Model.HasTemplate(), Url.PageLinkUrl(Model), Model.PageName, null, Url.PageLinkTarget(Model)))
{
<h2>@Model.PageName</h2>
<p>@Model.TeaserText</p>
Expand Down