5

Is there a way to use Page meta data from a component template (Component Presentation)

Example:

Page has meta data schema with a field called "pageName" and value "awesome page" Component has a field called "firstName" with value "Mike"

(in Component template the code reads) Welcome @@firstName@@ we're glad you're visiting @@Page.Metadata.pageName@@, thank you!

Expected Output: Welcome Mike we're glad you're visiting awesome page, thank you! Actual Output: Welcome Mike we're glad you're visiting , thank you!

its obvious that the page.metadata is not coming through, is there a way to get it?

  • fixed an error in my example, should have read @@Page.Metadata.pageName@@ – Daryl H Sep 17 '13 at 19:02
7

In the Templating Base project on SDL Tridion World there is a method GetPage() this is something which usually is called on a Page Template only, but can also be used to get to the Page object on a Component Template (provided it exists, which is only the case for embedded Component Presentations when they are published, or the Page they are on is previewed).

Below is an extract of the code for that method, the else statement shows how you can get to the Page from the RenderContext:

// this only works on a Page Template
Item pageItem = m_Package.GetByType(ContentType.Page);
if (pageItem != null)
    return m_Engine.GetObject(pageItem.GetAsSource().GetValue("ID")) as Page;
else
{
    // this works on a Component Template when it is an embedded one, 
    // and the Page it is on is published or previewed
    Page page = m_Engine.PublishingContext.RenderContext.ContextItem as Page;
    if (page != null)
        return page;
}
| improve this answer | |
4

It is possible to get to the page object from a component template (if one exists, remember that you could be executing in preview or in a dynamic component template) using a .Net based TBB and add it to the package or do something else with it (i.e. access your metadata). See Accessing page object in C# TBB used in the component template and Access container page of a component in .net based CT

| improve this answer | |
4

It sounds like in the Component Layout (DWT added to the Component Template) you should output the first name, such as "Welcome @@firstName@@" which will output "Welcome Mike". Next, you should add that component to the page with the appropriate CT.

In your page layout (DWT added to the Page Template), you can render the Component Presentation (outputting "Hello Mike"), followed with whatever you would like to output with the page metadata which is now easily accessible.

| improve this answer | |
  • what I'm trying to do is use some logic from the page such as "pageName" to decide the formating that the component template uses when rendering the component. – Daryl H Sep 17 '13 at 19:05
  • 1
    As David mentioned below, you may have to use a C# TBB to retrieve the Pages metadata field in the component template and add it to the package. Then inside of your component layout you could use the variable on the package in your logic. See the links David provided for more details on the TBB. – Josh Hebb Sep 17 '13 at 19:09
  • You can also output a tag as a part of component presentation and run a different regex or logic on every pt which uses it.. – Rohan Gadiya Sep 18 '13 at 16:00

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.