Bogdan Stojanovic
Bogdan Stojanovic

Reputation: 678

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

Upvotes: 2

Views: 570

Answers (5)

Bogdan Stojanovic
Bogdan Stojanovic

Reputation: 678

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

Upvotes: 0

Nuno Linhares
Nuno Linhares

Reputation: 10234

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);
        }
    }
}

Upvotes: 2

David Forster
David Forster

Reputation: 1390

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?

Upvotes: 1

Bogdan Stojanovic
Bogdan Stojanovic

Reputation: 678

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.

Upvotes: 0

David Forster
David Forster

Reputation: 1390

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.

Upvotes: 4

Related Questions