Question

What is the simplest way of dumping Tridion component content (based on a given schema, i.e.: non-arbitrary) into XML using modular template?

Specifically, need to pass component's content to/as ASP.Net custom control, that is, something like:

<custom:MyControl runat="server"
    TextField="value1"..>
  <EmbeddedField>
    ..
  </EmbeddedField>
</custom:MyControl>

I see one possible way would be to tweak Default Dreamweaver Component Design DWT TBB, but that seems to be too laborious.

EDIT

Another murky subject was the second part of the question above: embedding XML into ASP.Net control markup. The problem is that XML needs to be escaped. The following brainteaser does it in a DWT (pressuming, that Nuno's TBB wrote into Output):

<custom:MyControl runat="server">
    <xml>
        @@(Output).replace(RegExp.call(null, /&/.toString()[1], "g"), '&').replace(RegExp.call(null, /</.toString()[1], "g"), '<').replace(RegExp.call(null, />/.toString()[1], "g"), '>').replace(RegExp.call(null, String.fromCharCode(34), "g"), '"')@@
    </xml>
</custom:MyControl>

(All that ado is only necessary to overcome DWT's arcane JS escaping rules.)

Was it helpful?

Solution

Create a c# Template Building Block with the following code in it:

TemplatingLogger log = TemplatingLogger.GetLogger(GetType());
if (package.GetByName(Package.ComponentName) == null)
{
    log.Debug("Not running in a Component Template, exiting.");
    return;
}
Component component = (Component)engine.GetObject(package.GetByName(Package.ComponentName));
package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Xml, component.Content.OuterXml));

And use it in your template without any other building blocks.

This will output the exact content of Component.Content, which is rarely what you really want (for instance, how do you deal with binaries or linked components?), so you'll want to evolve to something else, with more details, and possibly removing the namespace from the XML nodes too...

i typically use a large switch(field.GetType().Name) and then deal with each field type as appropriate, something along these lines:

foreach (ItemField field in content)
{
    string fieldType = field.GetType().Name;
    switch (fieldType)
    {
        case "XhtmlField":
            // do something, like write it to an XmlWriter
            break;

And so on and so forth.

OTHER TIPS

I would use the XSLT mediator and then make an XSLT TBB using <xsl:copy-of select="."/>

If you don't have the XSLT Mediator installed, it will be simpler to write a C# TBB with outputs the .Content property of the Component to the package with the name Output. See Nuno's code sample for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top