0
votes

I am trying to create a custom link from sitecore into my view

@Html.Sitecore().Field("CTA display", Model.Item, new { text = "<span>" +  + "</span>"})

I am not 100% sure what the correct way to do this is, but I want to wrap the text from the link into a for styling. I've tried to put the Model.Rendering.Item.Fields["CTA display"] into there with .Text and it doesn't work.

Any help would be appreciated.

2

2 Answers

2
votes

First, I'd start by creating a SitecoreHelper extension method that allows you to modify the inner html of the element you're rendering:

public static HtmlString Field(this SitecoreHelper helper, string fieldName, Item item, object parameters, string innerHtml)
{
    if (helper == null)
    {
        throw new ArgumentNullException("helper");
    }
    if (innerHtml.IsNullOrEmpty())
    {
        return helper.Field(fieldName, item, parameters);
    }
    return new HtmlString(helper.BeginField(fieldName, item, parameters).ToString() + innerHtml + helper.EndField().ToString());
}

This will allow you to pass an optional innerHtml string that will be inserted between opening and closing tags of your element (in this case, an <a> tag).

From here, pass your html string containing your CTA label to the above method, or modify the method to output the field's Text value wrapped in a <span>.

0
votes

I used the solution posted above by computerjules which worked a treat. You can then called the extended method like follows

@Html.Sitecore().Field("Link",  Html.Sitecore().CurrentItem, new {Class = "some-class"}, "<span class='some-other-class'></span>")

and the span is rendered within the anchor tabs