6

have you ran into the problem when defining a template in a component query?

When using DWT my call looked like this

<!-- TemplateBeginRepeat name="allItemsInFolder" -->
         @@RenderComponentPresentation()@@
    <!-- TemplateEndRepeat -->

This worked because I set the template in the component query. when I try to do it via razor, like this :

@foreach (var c in Package.allItemsInFolder){
        @c.RenderComponentPresentation()
}

It thinks that RenderComponentPresentation is a FieldName and wont render it correctly, there error I get is

DynamicItemFields: Key 'rendercomponentpresentation' Not Found In ItemFields

Any advice would be great!

3 Answers 3

8

Your solution seems fine, except for the hardcoding of the CT's TCM ID. So it sounds like you need a way to reference the component template without hardcoding the TCM ID. Have a look at the comments in the answer here: https://stackoverflow.com/a/10802912/1284894

The solutions discussed are to:

  1. add a Parameter schema on the DWT TBB that specifies the template IDs, so that you can configure them across environments
  2. the other option is to write a C# TBB that pushes all the CTs by name onto the package with the TCM ID as the value.

Here is a link to the post with a sample TBB that you can use for this: http://www.tridiondeveloper.com/the-solution-to-hardcoding-component-template-ids-in-nested-component-presentations

2
  • 1
    I much prefer option 2 here - plugging a TCM-ID into a TBB parameter has almost all the same shortcomings as hardcoding it directly into a TBB; you're just hardcoding the ID into the CT instead of the TBB, which is just as volatile. If you can't content port a template without needing to open it up and change something, you're doing something wrong.
    – Ant P
    Feb 18, 2014 at 22:32
  • Note, with option 2, make sure to strip out the spaces and funky characters in your CT name so that you can easily get the package item by name. Feb 18, 2014 at 22:39
3

If this is Razor Mediator for Tridion (Alex Klock's extension), then you may want to use TridionObject:

@foreach (var c in Package.TridionObject.allItemsInFolder){
        @c.RenderComponentPresentation()
}
1

My current Solution is this, but I do not like it.

@foreach (var c in Package.allItemsInFolder){
    @RenderComponentPresentation(c.ID, "tcm:xx-xxxx-xxx")
}

Anything better out there?

Your Answer

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

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