Documentation Center

Example Using ComponentLink in template code

You create a component link by using the getLinkAsString method in the ComponentLink class in Template Building Block code and including this code in a Component Template or Page Template.

When identifying a link using the URI of the Component in a template, use the following syntax: GetLinkAsString(String pageURI, String ComponentURI, String templateURI, String linkAttr, String linkText, boolean textOnFail)

<%@ Import namespace="Tridion.ContentDelivery.Web.Linking"%>
<%
	ComponentLink componentLink = null;
%>
<%
	componentLink = new ComponentLink("tcm:0-115-1");
%>
<!-- A component link -->
<%Response.Write(componentLink.GetLinkAsString("tcm:115-18962-64", 
"tcm:115-16684", "tcm:0-0-0", "onClick=\"alert('I am a link')\"",
"Link for the text",true,false));%>
<!-- end of component link -->
<%
	componentLink.Dispose();
%>

Instead of writing an explicit call to ComponentLink.GetLinkAsString, you can also use the <tridion:ComponentLink> Custom Tag or Server Control.