4

I've searched through the Razor mediation documentation and SDL Tridion 2013 documentation, and can't seem to find the answer to my question.

What I'm attempting to do: Test the value of a component link in my template building block and determine if its component type is either "component" or "binary"; and then I'd like to use that information to add a target attribute depending upon the component type (component: _self, binary: _blank).

Code context:

@foreach (dynamic c in @Component.Fields.HeaderLinks) 
{
    if (@c.Link.ExternalLink != null && @c.Link.InternalLink == null) 
    {
        <li><a href="@c.Link.ExternalLink" target="_blank">@c.LinkDisplayName</a></li>
    }
    else 
    {
        <li><tcdl:Link type="Component" destination="@c.Link.InternalLink" 
        templateURI="tcm:0-0-0" linkAttributes="" textOnFail="True" 
        addAnchor="False">@c.LinkDisplayName</tcdl:Link></li>                               
    }
}

I'd love to be able to get the component type, and conditionally assign the target to a string variable based upon type, something like:

if (@c.Link.InternalLink.ComponentLinkType == binary) 
{
    String target = "_blank";
}

or something along those lines, and then just use the variable in the linkAttributes value within <tcdl:Link> something like...

<tcdl:Link ... linkAttributes="@target">@c.LinkDisplayName</tcdl:Link>
9

You could test for specific Schema's for example

if (c.Link.InternalLink.Schema.Title == "Multimedia Schema Title") 

This would give you more control if the desired output is different based on Multimedia type (e.g. image, document, video, podcast, etc..)

You could also use

if(c.Link.InternalLink.TridionObject.ComponentType.ToString() =="Multimedia")

or

if(c.Link.InternalLink.Schema.Purpose == "Multimedia")
| improve this answer | |
  • This worked perfectly. Thanks Chris, I appreciate it! – michael t Nov 6 '14 at 20:38
  • 2
    The second method is the most direct: just getting it from the component itself. Personally, I wouldn't test with a string value, but use the Enum: ComponentType.Multimedia – Dominic Cronin Nov 7 '14 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.