5

I've been trying to solve this problem since this morning, and I know I'm missing something obvious here but I can't seem to find it.

We are using an XML file that is published to the server which contains translations of all the standard words, such as 'read more'. It is a page with a component that is localized in the appropriate publication.

In our Razor templates we use the following code below a plain news Summary item which in turn links to the full item.

<a tridion:href="@news.ID" class="more" ><%=DefaultLabels.TranslatedTerm(((HomePage)Page).Location, "read_more")%></a>

Thing is, the server tag works just fine, but gets resolved as

<tridion:ComponentLink runat="server" PageURI="tcm:15-407-64" ComponentURI="tcm:15-1475" TemplateURI="tcm:0-0-0" AddAnchor="false" LinkText="&lt;%= DefaultLabels.TranslatedTerm(((HomePage)Page).Location, &#34;read_more&#34;) %&gt;" LinkAttributes=" class=&#34;more&#34;" TextOnFail="true"/>

As you might notice, it gets written as plain text on the page (no surprise there because the LinkText parameter is declared as String in the first place according to the liveDocs).

If I take away the

tridion:href

in the first example, and write it as

href

It works fine, the code resolves into a translated string and it's even linked... to nothing more than the TCM ID of the component, not the right page with the full news item on it.

I've tried creating a function in Razor, tried replacing the linkText, tried setting the ComponentLink in the template itself, but to no avail. I feel that it should work with just a minor adjustment to this template's code, but I fail to see it and I've started looking at custom TBB's to handle the code.

Anyone have an idea how to resolve this?

EDIT:

Chris' answer was actually the one I was looking for in this particular situation, but I feel that I should point out that Priyank's function is something that should be regarded as such as well. So thank you both for the help, it made my life somewhat easier now!

2 Answers 2

8

I hope this razor function will help you lot. This is very helpful function to render the link tag from the component link or external link.

@helper RenderLink(
    dynamic link,                        // the link to render. Handles components + internal / external links
    string cssClass = null,              // optional custom CSS class
    string title = null                 // optional link text (default is the title of the component being linked to)
    ) 
{
    if(link == null) 
    {
          return;
    }

    if (title == null) 
    {   
        title = link.title;
    }

    string classAttr = string.IsNullOrEmpty(cssClass) ? "" : " class='" + cssClass + "'";
    dynamic href;
    string tridionLink = "";
    string targetAttr = "";

    if (link.Schema.Title == "External Link") 
    {
        href = link.link;
    }
    else if (link.Schema.Title == "Internal Link")
    {
        href = link.link;
        tridionLink = "tridion:";
    } 
    else 
    {
        href = link;
        tridionLink = "tridion:";
    }    

    if(link.target != null) 
    {
        targetAttr = link.target == "New window" || link.target == "Popup" ? " target='_blank'" : "";
    }    

    <a @(tridionLink)href="@href"@classAttr@targetAttr>@title</a> 
}
2

I would suggest not using the default templates for resolving your links, rather output the link yourself something like this:

<tridion:ComponentLink runat="server" PageURI="tcm:15-407-64" 
    ComponentURI="tcm:15-1475" TemplateURI="tcm:0-0-0" 
    AddAnchor="false" LinkAttributes=" class=&#34;more&#34;" 
    TextOnFail="true">
        <%=DefaultLabels.TranslatedTerm(((HomePage)Page).Location, &#34;read_more&#34;) %>
</tridionComponentLink>

Better still you might consider outputting TCDL rather than the Taglib/ServerControl

2
  • Chris, could you explain why I should not use the default templates for resolving links, and use this syntax instead? And what about outputting TCDL? Thanks.
    – MDa
    Commented Aug 28, 2012 at 8:56
  • 1
    The default "Resolve links TBB" finds all of the links containing TCM URIs, and converts them to TCDL, which later gets converted to REL, JSP, ASP.NET etc. It builds the TCDL by placing the link text in an attribute (which for you is not being executed by your ASP.NET control). To make your code execute, the easiest thing is for the link text (or in your case a bit of code) in the body of the tag (rather than an attribute). The code I provided directly makes a reference to the ComponentLink server control which will load the link text from the body of the control if it is not in the attribute. Commented Aug 28, 2012 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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