Sitecore MVC and FieldRenderer.Render for Links
Asked Answered
E

5

5

I'm trying to render a general link field like this - FieldRenderer.Render(item, "link").

This works as expected but how do I set custom text within the a tag that gets rendered. I want my output to look something like this

<a href="[link from sitecore]">[custom text from another field]</a>

Basically, the text for the link should come from another field on the item.

Thanks

Electra answered 25/7, 2014 at 14:55 Comment(0)
M
8

You probably want to try the following:

@Html.Sitecore().BeginField("Link Field")
//custom code
@Html.Sitecore().EndField()
Milka answered 25/7, 2014 at 22:58 Comment(1)
Is there any reference for the MVC api?Electra
R
10

Jason has a great idea, but this functionality is out of the box.

@Html.Sitecore().BeginField("Link Field", new { haschildren= true })
@Html.Sitecore().Field("Text Field")
@Html.Sitecore().EndField()

No need to modify anything at all.

Rightness answered 29/9, 2015 at 16:13 Comment(0)
M
8

You probably want to try the following:

@Html.Sitecore().BeginField("Link Field")
//custom code
@Html.Sitecore().EndField()
Milka answered 25/7, 2014 at 22:58 Comment(1)
Is there any reference for the MVC api?Electra
S
2

Varun's answer is definitely correct, however you will encounter an issue when the content editor has put a value into the Description field of a General Link. The link renderer will output both the description and whatever is between the BeginField and EndField methods.

A solution would be to allow for an extra parameter (HideDescription) which can hide the description. Two possible solutions for this would be;

  1. Override the standard Sitecore.Xml.Xsl.LinkRenderer class with your own that would stop the description from being put in.
  2. Add a custom pipeline step after the Sitecore.Pipelines.RenderField.GetLinkFieldValue which will do some post rendered processing to remove the description.

Option 2 is less invasive but is a little more difficult to make sure the results are 100%. Once you have this you can then render fields like the following;

@Html.Sitecore().BeginField("Link Field", new { HideDescription = true })
@Html.Sitecore().Field("Text Field")
@Html.Sitecore().EndField()
Sawmill answered 20/1, 2015 at 15:12 Comment(0)
L
1

already has been answered but this worked for me

@Html.Sitecore().BeginField("Target URL", item.InnerItem, new { text = @Html.Sitecore().Field("Title", item.InnerItem) })

@Html.Sitecore().EndField()
Lowpitched answered 19/6, 2018 at 0:54 Comment(0)
W
1

You can also use begin and end-field and use the haschildren property, this will hide the text and just display any child-content between the begin and end statements:

@Html.Sitecore().BeginField("ItemName/ID", Model.DataSourceItem, new {
  haschildren = true,
  @class = "my-custom-class"
})
<span class="extra-content">
  @Html.Sitecore().Field("Text Field")
</span>
@Html.Sitecore().EndField()
Word answered 11/7, 2018 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.