Dump Tridion Component as XML - tridion

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.)

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.

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.

Related

Meteor how to save templates in mongo

I want to give my users the possibility to create document templates (contracts, emails, etc.)
The best option I figured out would be to store these document templates in mongo (maybe I'm wrong...)
I've been searching for a couple of hours now but I can't figure out how to render these document template with their data context.
Example:
Template stored in Mongo: "Dear {{firstname}}"
data context: {firstname: "Tom"}
On Tom's website, He should read: "Dear Tom"
How can I do this?
EDIT
After some researches, I discovered a package called spacebars-compiler that brings the option to compile to the client:
meteor add spacebars-compiler
I then tried something like this:
Template.doctypesList.rendered = ->
content = "<div>" + this.data.content + "</div>"
template = Spacebars.compile content
rendered = UI.dynamic(template,{name:"nicolas"})
UI.insert(rendered, $(this).closest(".widget-body"))
but it doesn't work.
the template gets compiled but then, I don't know how to interpret it with its data context and to send it back to the web page.
EDIT 2
I'm getting closer thanks to Tom.
This is what I did:
Template.doctypesList.rendered = ->
content = this.data.content
console.log content
templateName = "template_#{this.data._id}"
Template.__define__(templateName, () -> content)
rendered = UI.renderWithData(eval("Template.#{templateName}"),{name:"nicolas"})
UI.insert(rendered, $("#content_" + this.data._id).get(0))
This works excepted the fact that the name is not injected into the template. UI.renderWithData renders the template but without the data context...
The thing your are missing is the call to (undocumented!) Template.__define__ which requires the template name (pick something unique and clever) as the first argument and the render function which you get from your space bars compiler. When it is done you can use {{> UI.dynamic}} as #Slava suggested.
There is also another way to do it, by using UI.Component API, but I guess it's pretty unstable at the moment, so maybe I will skip this, at least for now.
Use UI.dynamic: https://www.discovermeteor.com/blog/blaze-dynamic-template-includes/
It is fairly new and didn't make its way to docs for some reason.
There are few ways to achieve what you want, but I would do it like this:
You're probably already using underscore.js, if not Meteor has core package for it.
You could use underscore templates (http://underscorejs.org/#template) like this:
var templateString = 'Dear <%= firstname %>'
and later compile it using
_.template(templateString, {firstname: "Tom"})
to get Dear Tom.
Of course you can store templateString in MongoDB in the meantime.
You can set delimiters to whatever you want, <%= %> is just the default.
Compiled template is essentially htmljs notation Meteor uses (or so I suppose) and it uses Template.template_name.lookup to render correct data. Check in console if Template.template_name.lookup("data_helper")() returns the correct data.
I recently had to solve this exact (or similar) problem of compiling templates client side. You need to make sure the order of things is like this:
Compiled template is present on client
Template data is present (verify with Template.template_name.lookup("data_name")() )
Render the template on page now
To compile the template, as #apendua have suggested, use (this is how I use it and it works for me)
Template.__define__(name, eval(Spacebars.compile(
newHtml, {
isTemplate: true,
sourceName: 'Template "' + name + '"'
}
)));
After this you need to make sure the data you want to render in template is available before you actually render the template on page. This is what I use for rendering template on page:
UI.DomRange.insert(UI.render(Template.template_name).dom, document.body);
Although my use case for rendering templates client side is somewhat different (my task was to live update the changed template overriding meteor's hot code push), but this worked best among different methods of rendering the template.
You can check my very early stage package which does this here: https://github.com/channikhabra/meteor-live-update/blob/master/js/live-update.js
I am fairly new to real-world programming so my code might be ugly, but may be it'll give you some pointers to solve your problem. (If you find me doing something stupid in there, or see something which is better done some other way, please feel free to drop a comment. That's the only way I get feedback for improvement as I am new and essentially code alone sitting in my dark corner).

Visual Studio treat custom extension as CSharp

I've got files with a custom extension .mcsx that are really CSharp files. I want to get code completion on these files in Visual Studio 2012, but these files are treated as plain text when I open them in VS.
I've tried to create a custom MEF extension which allows me to treat the files as having a content-type based on csharp, but I don't get autocompletion or code formatting for these documents. I might need to invoke the custom CSharp classifier for my custom content type, but I'm not sure. Can anyone help?
The custom ContentType is below:
static class ContentType
{
public const string Name = "CSScript";
[Export]
[Name(Name)]
[DisplayName("CSharp Script")]
[BaseDefinition("CSharp")]
public static ContentTypeDefinition CSharpContentType = null;
[Export]
[ContentType(Name)]
[FileExtension(".mcsx")]
public static FileExtensionToContentTypeDefinition CSharpFileExtension = null;
}
Thanks to 'Ego' on the VS forums:
You can add the custom extension file via the way below:
http://blogs.msdn.com/b/zainnab/archive/2010/08/22/using-custom-file-extension-associations-vstipenv0038.aspx
For more information about Registering a Language Service please view:
http://msdn.microsoft.com/en-us/library/vstudio/bb166421(v=vs.110).aspx
So inside of VS things are actually a bit different than the editor APIs imply -- we don't actually use content types (for the most part) to determine when to activate. Noah Richards has a great blog post that shows how to create an extension that marks another extension under an existing editor.

Write Tridion dreamweaver identifier as output package

While working on compound page templating project I have discovered very interesting issue. Customer uses ${stringvalue-session-or-something-specific} in their custom developed applications that are saved and rendered in component templates using XSLT. When try to render the component presentations containing reserved Tridion dreamweaver identifier (${}) I have got Template Builder error similar to
JScriptException: Expression '""["scopedTarget.personalia"]."" != ""' had error 'Expected identifier'
at Tridion.ContentManager.Templating.Expression.JScriptEvaluator.EvaluateToObject(String statement)
at Tridion.ContentManager.Templating.Expression.JScriptEvaluator.EvaluateToString(String statement)
Removing and replacing this identifiers is not solution acceptable by customer.
I was wondering what can be the best solution to cope this issue? Either using C# code to rendering component presentations, maybe create some custom rewrites( if it is possible because I got error from component presentations).
A similar thing happens if customers use JSP EL in their applications, which use the same ${...} syntax, and want to encapsulate this in their templates.
The most common solution is to replace this syntax with something like $[...] in the Dreamweaver templates and use a .Net TBB after the Dreamweaver template which uses string replacement or regular expressions to convert it.
See my gist for an example TBB that does this.
I normally use this code to "enable JSTL in templates". Since you can't change Tridion to use a different code identifier, you need to change your markup. Run this TBB at the END of your template to convert $[expression] to ${expression}
using System;
using System.Text.RegularExpressions;
using Tridion.ContentManager.Templating;
using Tridion.ContentManager.Templating.Assembly;
namespace TridionTemplates
{
[TcmTemplateTitle("Enable JSTL")]
public class EnableJSTL : ITemplate
{
private static readonly Regex JstlRegex = new Regex(#"\$\[.*\]");
public void Transform(Engine engine, Package package)
{
Item outputItem = package.GetByName(Package.OutputName);
string outputText = outputItem.GetAsString();
Match match = JstlRegex.Match(outputText);
while (match.Success)
{
String replaceJstl = match.Value.Replace("[", "{");
replaceJstl = replaceJstl.Replace("]", "}");
outputText = outputText.Replace(match.Value, replaceJstl);
match = match.NextMatch();
}
outputItem.SetAsString(outputText);
package.Remove(outputItem);
package.PushItem(Package.OutputName, outputItem);
}
}
}
If you absolutely can't change the syntax in your Dreamweaver templates (As I stated, I'm not sure I fully understand/agree with the reasons for that) then perhaps you could switch to using the Razor Mediator for your templating instead?
As I mentioned replacing syntax is not solution. I have solved this by rendering component presentation in separate C# tbb before Dreamweaver template. Off course,David suggestion is useful if you are allowed to change the syntax.
I use SDL Tridion 2011 SP1 HR1. TemplateBuilder version 6.1 Build 6.1.0.73.
Extract components from Page executed before DWT code below.
<!-- TemplateBeginRepeat name="Components" -->
##RenderComponentPresentation()##
<!-- TemplateEndRepeat -->
Output error logged from Template builder in case if component presentations contains ${sometext}.
JScriptException: Expression '""["scopedTarget.personalia"]."" != ""' had error 'Expected identifier'
at Tridion.ContentManager.Templating.Expression.JScriptEvaluator.EvaluateToObject(String statement)
at Tridion.ContentManager.Templating.Expression.JScriptEvaluator.EvaluateToString(String statement)
at Tridion.ContentManager.Templating.Package.EvaluateExpression(String expression)
at Tridion.ContentManager.Templating.Dreamweaver.DreamweaverMediator.TransformValueReferences(Package package, StringReference templateReference, Regex startTagExpression, String endTag)
at Tridion.ContentManager.Templating.Dreamweaver.DreamweaverMediator.TransformRegions(Package package, String dreamweaverTemplate)
at Tridion.ContentManager.Templating.Dreamweaver.DreamweaverMediator.Transform(Engine engine, Template templateToTransform, Package package)
at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
at Tridion.ContentManager.Templating.Compound.CompoundTemplateMediator.Transform(Engine engine, Template templateToTransform, Package package)
at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
at Tridion.ContentManager.Templating.Engine.TransformPackage(Template template, Package package)
at Tridion.ContentManager.Templating.Debugging.DebuggingEngine.Run()
at Tridion.ContentManager.Templating.Debugging.DebugSession.Run()
---Caused by:
Expected identifier
eval code: Line 1 - Error: Expected identifier

Is it possible to intermix Modular templating and legacy VBScript CT?

In particular, the case I have in mind is this:
##RenderComponentPresentation(Component, "<vbs-legacy-ct-tcm-uri>")##
The problem I'm having is that in my case VBS code breaks when it tries to access component fields, giving "Error 13 Type mismatch ..".
(So, if I were to give the answer, I'd say: "Partially, of no practical use")
EDIT
The DWT above is from another CT, so effectively it's a rendering of component link, that's why parameterless overload as per Nuno's suggestion won't work unfortunately. BTW, the following lines inside VBS don't break and give correct values:
WriteOut Component.ID
WriteOut Component.Schema.Title
EDIT 2
Dominic was absolutely wright: it's a missing dependencies.
A bit more insight to make this info generally useful:
Suppose, the original CT looked like this ("VBScript [Legacy]" type):
[%
Call RenderComponent(Component)
%]
This CT was meant to be called from a PT, also VBS-based. That PT had a big chunk of "#include" statements in the beginning.
Now the story changes: the same CT is being called from another, DWT-based, CT. Obviously (thanks you all for your invaluable help!), dependencies are now not being included anywhere.
The solution to make original CT working again is to explicitly hand-pick and include all necessary VBS TBBs, so the original CT becomes:
[%
#include "tcm:<uri-of-vbs-tbb>"
Call RenderComponent(Component)
%]
Yes - it's perfectly possible to mix and match legacy and modular templates. Perhaps obviously, you can't mix and match template building blocks between the two techniques.
In VBScript "Error 13 Type mismatch" is sometimes used as a secret code that really means "I don't recognise the name of one of your variables, (including the names of Functions and Subs)" In the VBScript templating engine, variables from the page template could be in scope in your component template; it was very common, for example, to put the #includes in the PT so they could be used by the CT. My guess is that your component template is trying to use such a Function, and not finding it.
I know that you can render a Modular Page Template with VBScript Component Presentations, and also a VbScript page template can render a modular Component Template.
Your error is possibly due to something else? Have you tried just using the regular ##RenderComponentPresentation()## call without specifying which template?
The Page Template can render Compound Templates of different flavors - for example Razor, VBS, or XSLT.
The problem comes from the TBBs included in the Templates. Often the Razor templates will need to call functions that only exist in VBScript. So, the starting point when migrating templates is always to start with the helper functions and utility libraries. Then migrate the most generic PT / CT you have to the new format (Razor, XSLT, DWT, etc). This provides a nice basis to migrate the rest of the Templates as you have time to the new format.

Tridion 2009 - Publish another Component from a Component Template

First, the overall description:
There are two Component Templates, NewsArticle and NewsList. NewsArticle is a Dreamweaver Template, and is used to display the content of a news article. NewsList is an xml file that contains aggregated information about all of the news articles.
Currently, a content author must publish the news article, and then re-publish the newslist to regenerate the xml.
Problem:
I have been tasked with having the publish of a news article also regenerate and publish the newslist. Through C#, I am able to retrieve the content of the newslist component, generate the updated xml from the news article, and merge it into the xml from the newslist. I am running into trouble getting the newslist to publish.
I have limited access to documentation, but from what I do have, I believe using the static PublishEngine.Publish method will allow me to do what I need. I believe the first parameter (items) is just a list that contains my updated newslist, and the second parameter is a new PublishInstruction with the RenderInstruction.RenderMode set to Publish. I am a little lost on what the publicationTargets should be.
Am I on the right track? If so, any help with the Publish method call is appreciated, and if not, any suggestions?
Like Quirijn suggested, a broker query is the cleanest approach.
In a situation if a broker isn't available (i.e. static publishing model only) I usually generate the newslist XML from a TBB that adds the XML as a binary, rather than kicking off publishing of another component or page. You can do this by calling this method in your C# TBB:
engine.PublishingContext.RenderedItem.AddBinary(
Stream yourXmlContentConvertedToMemoryStream,
string filename,
StructureGroup location,
string variantId,
string mimeType)
Make the variantId unique per the newslist XML file that you create, so that different components can overwrite/update the same file.
Better yet, do this in a Page Template rather than Component Template so that the news list is generated once per page, rather than per component (if you have multiple articles per page).
You are on the right tracks here with the engine.Publish() method:
PublishEngine.Publish(
new IdentifiableObject[] { linkedComponent },
engine.PublishingContext.PublishInstruction,
new List() { engine.PublishingContext.PublicationTarget });
You can just reuse the PublishInstruction and Target from the current context of your template. This sample shows a Component, but it should work in a page too.
One thing to keep in mind is that this is not possible in SDL Tridion 2011 SP1, as the publish action is not allowed out of the box due to security restrictions. I have an article about this here http://www.tridiondeveloper.com/the-story-of-sdl-tridion-2011-custom-resolver-and-the-allowwriteoperationsintemplates-attribute

Resources