Executing an item in the package as a Dreamweaver Template
Asked Answered
H

2

9

Does anybody know if it is possible in a compound template to use a string item in the package and execute it as if were a dreamweaver template? And whether you apply the same method to other mediators (like razor)?

Thanks Mark

Handicapped answered 13/6, 2012 at 14:0 Comment(1)
The Package class has a method package.EvaluateExpression(string expression) that might be worth investigating.Eczema
R
5

I'm afraid that won't be possible on just any item in the Package, since the Engine expects Templates to be based on Tridion items.

If your Template Item is based on a Tridion Item you can probably get pretty far by starting at the Engine.GetMediator method. If it isn't, you'll have to find some way to turn it into a valid Template object.

Template template = ...
IMediator mediator = engine.GetMediator(template);
mediator.Transform(engine, template, package);

When I have to create a Component object from a Tridion-based Item in the Package, I normally do something like this:

Component component = new Component(item.GetAsXmlDocument().DocumentElement, 
                                    engine.GetSession);

I haven't tried, but expect that you can do the same for a Template - given that you start with a valid Item from the Package representing a Template to begin with. You can probably clone the XML from an existing Item or find some other way to fake it.

If you get this to work, it will work across all registered template types. The Engine provides no special treatment for the types that come with Tridion.

Rida answered 13/6, 2012 at 14:21 Comment(1)
I found out today that engine.GetMediator is not public, so you can't reach it. I ended up simply instantiating the DreamweaverMediator in my TBB and passing in my pre-constructed TemplateBuildingBlock object. I'll update the code above if more people find it useful.Rida
O
5

I suspect this is not possible.

Package.EvaluateExpression may be useful, but as the name suggests it'll only work on expressions, not large snippets of code with embedded expressions (i.e. TEL)

Engine.GetMediator expects a Template and returns the appropriate Mediator for it. Your problem then is that the IMediator interface only defines the Transform method, which requires an Engine, a Template and a Package.

I can't think of any elegant ways around these. Maybe write your own Mediator, but that would still expect a Package, not a string, so you'd have to first store the string based Item from another TBB.

My advice: Sounds like you need to go back to the drawing board and find an alternative solution to your problem.

Ovariotomy answered 13/6, 2012 at 19:42 Comment(2)
I had assumed that Mark wanted to invoke the "string as a DWT" from within an existing ITemplate or Mediator, since he is referring to it being an item in the package. If that is the case, he could simply pass his existing Engine and Package objects to the Mediator.Transform method. The main problem I could see is in turning that string Item into a Template instance. But it's a good point, so I'll update my answer to elaborate.Rida
I think it's possible to create a new ComponentTemplate or PageTemplate object and pass in a valid XML representation of a template.That would still not get you anywhere though - I think you'd have to create a new TBB from your string item and add it to your new template's XML. I expect that TBB is going to need to be saved.Ovariotomy

© 2022 - 2024 — McMap. All rights reserved.