2

I have a requirement where I need to have a component presentation be published statically to a page. The catch is, the component is added to the page using a Component Template that has been marked to publish the component dynamically. Now although the component at this point will still be rendered via dynamic assembly, I actually must absolutely have this put there statically at publish time.

Some things that are out of the question is localization (this is not being published to two different publications where one has the content delivery set up and the other doesn't) or using two separate component templates (one marked dynamic, the other not, and having the authors select the static version only for the page).

2 Answers 2

4

Just want to add a comment, not related to how to render static a component template that is dinamic, but related to something that you should consider in your scenario.

As you handle the render at the page template level, in case that the component is published separately from the page, the page won't be published, as the component is using a dynamic component template.

That means that if you require the page to be updated when publishing the component you will have to trigger the publish of the page when publish the component.

If you always publish pages (never components), there won't be impact, but just verify in case you need to publish components separately

1
  • Thanks Miguel, +1! Definitely a very interesting point you've added to keep in mind for myself and any other who might try this type of approach!
    – Alex Klock
    Commented Sep 4, 2014 at 16:15
3

After playing around with some options, what I ended up with is using the TemplatingRenderer's Render method. From the Page Template's design TBB, rather than calling RenderComponentPresentation for this specific Component Template, I pieced together the necessary ResolvedItem, PublishInstruction, PublicationTarget, RenderedItem, and RenderContext instances. For the PublishInstruction and PublicationTarget, I just used the existing ones from the engine. An example of something close to what I did to get the output of a presentation is as follows:

@foreach (var cp in GetComponentPresentationsByTemplate("My Dynamic CT"))
{
    TemplatingRenderer renderer = new TemplatingRenderer();
    ResolvedItem resolvedItem = new ResolvedItem(
        cp.Component.TridionObject, cp.Template.TridionObject);
    RenderedItem renderedItem = new RenderedItem(
        resolvedItem, 
        TridionHelper.Engine.PublishingContext.PublishInstruction.RenderInstruction);
    RenderContext renderContext = new RenderContext(cp.Component.TridionObject);

    // Now that we have all of our items ready, lets render this presentation
    renderer.Render(resolvedItem, 
        TridionHelper.Engine.PublishingContext.PublishInstruction, 
        TridionHelper.Engine.PublishingContext.PublicationTarget, 
        renderedItem, 
        renderContext);

    // But uh oh... Render does not return the presentation, we'll have to grab that
    // from the underlying stream...

    // Reset the position of the buffer back to the start...
    renderedItem.Content.Seek(0, System.IO.SeekOrigin.Begin); 

    string cpOutput;
    using (StreamReader sr = new StreamReader(renderedItem.Content)) 
    {
        // ... and now read the stream to the end to a string that we can output
        cpOutput = sr.ReadToEnd();
    }
    <text>@cpOutput</text>
}

Although the above snippet is done using the Razor Mediator, you'll notice that this is just the TOM.NET API. This could easily be added to a razor helper function, or even a DWT custom function for your DW templates. You can now output that dynamic component presentation directly to the page as static content. Through the RenderContext instance, you can even set context variables if you need to pass any special information to your CT (as was my case, the CT did an extra special something if the CT was rendered in this fashion).

One important note that the Render method does not wrap the presentation in tcdl tags, so if you want to keep those intact you'll have to add them yourself.

I'd still be interested in hearing some other solution if you guys have them!

2
  • I'm sure there's a reason for this... I would have just created a different CT with the same TBBs...
    – Nuno Linhares
    Commented Sep 4, 2014 at 16:31
  • Definitely a reason for the madness, I usually would go with the double CT approach. However this was a special situation due to a hybrid publishing architecture where that approach wouldn't work.
    – Alex Klock
    Commented Sep 5, 2014 at 0:31

Your Answer

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

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