razorsitecorerazorengineglass-mappersitecore8.1

GlassMapper Render Link add Target for external Links


We are on sitecore 8.1 GlassMapper 4.4.1.188.

We are rendering an image in an anchor like below:

 using (BeginRenderLink(item, x => x.CarouselLink, isEditable: true))
 {
     @RenderImage(item, x => x.CarouselImage, isEditable: true)
 }

All works well.

How can we force Renderlink to insert Html Target attribute based on sitecore input from General Link?

So, if content editor picks "External link" we need link to be generated with target="_blank"


Solution

  • When you select external link, use New browser option:

    enter image description here

    This is cshtml code:

    @using (Html.Glass().BeginRenderLink(Model, x => x.Link))
    {
        <span>aaa</span>
    }
    

    and this is html output:

    <a href="http://google.com" target="_blank"><span>aaa</span></a>