2012-08-27 48 views
5

自從今天上午以來,我一直在試圖解決這個問題,並且我知道我錯過了某些明顯的東西,但我似乎無法找到它。在組件鏈接中使用翻譯字符串

我們正在使用發佈到服務器的XML文件,該文件包含所有標準單詞的翻譯,例如'閱讀更多'。它是一個組件在適當的出版物中本地化的頁面。

在我們的Razor模板中,我們在普通新聞摘要項下面使用以下代碼,然後鏈接到完整的項目。

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

事情是,服務器標籤工作得很好,但被解析爲

<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"/> 

正如你可能已經注意到,它就會被寫在頁面上純文本(這並不足爲奇,因爲連結文字參數根據liveDocs首先聲明爲String)。

如果我帶走

tridion:href 
在第一個例子

,並把它寫成

href 

它工作正常,代碼解析爲翻譯後的字符串和它甚至鏈接...無非是組件的TCM ID,而不是包含全部新聞項目的正確頁面。

我試過在Razor中創建一個函數,試圖替換linkText,試圖在模板本身中設置ComponentLink,但無濟於事。我覺得應該對這個模板的代碼做一些小的調整,但是我沒有看到它,並且我開始考慮定製的TBB來處理代碼。

任何人都有一個想法如何解決這個問題?

編輯:

克里斯的回答竟是一個我一直在尋找在這個特殊的情況,但我覺得我應該指出的是,Priyank的功能是什麼,也應該被看作是這樣。所以謝謝你們的幫助,現在讓我的生活更輕鬆一些!

回答

2

我不使用默認的模板解決您的鏈接,而輸出鏈接自己是這樣的建議:

<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> 

更妙的是你可以考慮輸出戰術通用數據鏈,而不是標籤庫/ ServerControl

+0

Chris,你能解釋爲什麼我不應該使用默認模板來解析鏈接,而是使用這個語法嗎?那麼輸出TCDL呢?謝謝。 – MDa

+1

默認的「解析鏈接TBB」查找所有包含TCM URI的鏈接,並將它們轉換爲TCDL,稍後將其轉換爲REL,JSP,ASP.NET等。它通過將鏈接文本放入屬性中來構建TCDL這對你而言並不是由你的ASP.NET控件執行的)。爲了讓代碼執行,最簡單的方法是在標籤主體(而不是屬性)中使用鏈接文本(或者在您的情況下使用一些代碼)。我提供的代碼直接提供了對ComponentLink服務器控件的引用,如果該控件不在屬性中,它將從控件正文加載鏈接文本。 –

8

我希望這個剃刀功能可以幫助你很多。這對於從組件鏈接或外部鏈接呈現鏈接標籤非常有幫助。

@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"@[email protected]>@title</a> 
}