10

In the HTML template for my Component I need to write out the TCM URI of the Component Template:

<!-- Start Component Presentation: {"ComponentID" : "@@Component.ID@@",
                        "ComponentTemplateID" : "@@ComponentTemplate.ID@@" } -->

But unfortunately ComponentTemplate.ID doesn't exist.

How can I access the Component Template ID from within my Component DWT?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

3 Answers3

11

I'm afraid you'll have to write a C# TBB (fragment or assembly):

TcmUri templateId = engine.PublishingContext.ResolvedItem.Template.Id;
if (templateId.ItemType == ItemType.ComponentTemplate) {
    Item item = package.CreateStringItem(ContentType.Text, templateId.ToString());
    package.PushItem("ComponentTemplateID", item);
}

You can only access the ComponentTemplate.ID from within a Page DWT layout...

Bart Koopman
  • 4,835
  • 17
  • 30
Mihai Cădariu
  • 2,407
  • 14
  • 17
  • Not exactly 'access' but if the DWT is used only with a certain template, could you hard-code it? What am I saying, this is @Frank we're talking about. – Alvin Reyes Mar 30 '12 at 11:55
  • This may be Frank, but he's in this case working on a somewhat realistic web site for once. So unfortunately hard-coding it is not an option. Writing tons of code to solve the problem, is an option though; so I am so far taking Mihai's suggestion, but then put it in a function source that I can call from my DWT @@GetComponentTemplate()@@ – Frank van Puffelen Mar 30 '12 at 12:54
  • 2
    Maybe tons of code isn't needed. Apparently @@ID@@ returns the Component Template ID in Preview and Publishing, but not Template Builder. For example, `@@Component.ID@@ @@ID@@` gives me `tcm:6-1541 tcm:6-1541` in Template Builder, but `tcm:6-1541 tcm:7-81-32` in Preview. – Alvin Reyes Jan 10 '13 at 21:22
1

@@ID@@ will give you the Component Template TCM URI when previewing (in the CME, not in the Template Builder) or Publishing.

Bart Koopman
  • 4,835
  • 17
  • 30
User 4.5.5
  • 1,311
  • 3
  • 12
  • 20
1

Add a "ComponentTemplateID" parameter to the Template Invocation in the compound template. You can then access this in the same way as package variables.

Dominic Cronin
  • 6,062
  • 2
  • 23
  • 56
  • 1
    Although an interesting alternative, I would still have to type that parameter value in manually. Thus action like copy/pasting, porting or localizing the CT will render the parameter value incorrect. – Frank van Puffelen Mar 30 '12 at 20:00
  • Indeed Frank. I often prefer to do things with configuration rather than code, but maybe I've gone a bit far. I don't mind someone who copy/pastes having to fix it up, and I don't mind having a technical design that outlaws localizing the CT, but the porting problem is just too nasty. +1 to Mihai's solution then. – Dominic Cronin Apr 01 '12 at 17:14