9

We are working on Tridion 2011 SP1 and we always used DWT (Dreamweaver templates). We have a huge collection of usefull custom functions that we are using accross various dreaweaver templates.

Due to issues with getting component links inside dreamweaver templates, we are now started by using Razor templates. Everything goes well, but is there possibility to reuse all our custom functions that we have in DWT?

Since we are using Media Manager, they provided us the following function to get the url of an image in Media Manager via a Custom DWT Function. But in our Razor templates, we cannot use them anymore.

Custom function example in DWT:

@@GetExternalContentLibraryDirectLink("tcm:6-12345")@@
@@RootUri()@@

A lot of these functions can be replaced with @function {} in Razor. But we have really complex functions that are using the Engine and Package. And we have the GetExternalContentLibraryDirectLink where we don't have the source of.

This function is an example, the class inherits of IFunctionSource and the dll is added to the GAC.

public void Initialize(Engine engine, Package package) {
   m_Engine = engine;
   m_Package = package;
}

[TemplateCallable]
public string RootUri() {
   return GetPublication().RootStructureGroup.PublishLocationUrl;
}
12

You can use your Custom Dreamweaver Functions from a Razor Mediator Template with some configuration changes. Your DW functions are in a DLL that is stored in the GAC, and you can reference and use that dll from Razor Mediator. To be able to call those functions you'll have to do the following.

First, you'll have to edit the Tridion.ContentManager.config file, and add a reference to your assembly in the <assemblies /> section. Assuming your dll is "YourCompany.Dreamweaver.dll", your config change would look something along the lines of:

<assemblies>
    <add assembly="YourCompany.Dreamweaver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=89g4afbf414eb835" />
</assemblies>

Make sure you restart the COM+ and usual services after making the config change.

Now from your Razor Mediator template you can do something like the following:

@{
    var dw = new YourCompany.Dreamweaver.DreamweaverFunctions();
    dw.Initialize(TridionHelper.Engine, TridionHelper.Package);
}

... where DreamweaverFunctions is your custom class implementing IFunctionSource. You can then call your methods of your custom functions class using your instance variable.

<div>@dw.DoSomethingYouWouldNormallDoFromDWT("blah")</div>

If in a situation where you are making heavy use of your DW functions, but it is not worthwhile or possible to replace them with Razor Helper Functions, I find the following property useful to use as a global import for use in all of your razor templates.

@functions {
    private YourCompany.Dreamweaver.DreamweaverFunctions _dw;

    public YourCompany.Dreamweaver.DreamweaverFunctions DW
    {
        get
        {
            if (_dw == null)
            {
                _dw = new YourCompany.Dreamweaver.DreamweaverFunctions();
                _dw.Initialize(TridionHelper.Engine, TridionHelper.Package);
            }
            return _dw;
        }
    }
}

You can then call @DW.YourFunction() from your templates. Hope that helps!

5

There are pros and cons to every Template language you will choose, while on of the biggest pros for Razor Templating will be that you have the power of .NET available inside your Razor Templates, this also means you leave functionality like you have in DWT Templates behind.

So your DWT Custom functions are useless and will have to be replaced with Razor helpers and functions. For those you built yourself, that will be relatively easy, but for the out of the box ones, you have a bit of a challenge indeed.

If its only the GetExternalContentLibraryDirectLink() you are missing, then that is not such a huge problem. Indeed its source code is not available at current, but I have placed an example TBB for ECL (External Content Libraries) on Google code. It's free for you to use and shows how a TBB can make use of the ECL APIs, including on line 215, a call to GetDirectLinkToPublished(), which seems to be the result you are looking for.

Basically what you need to do in that helper function is create an ECL item, build Template Parameters and then call GetDirectLinkToPublished(), in short:

// using statement on ecl session, so it gets correctly disposed
using (IEclSession eclSession = SessionFactory.CreateEclSession(engine.GetSession())
{
  // try to get an ecl item from your ecl- or tcmuri
  IEclUri eclUri = myEclOrTcmUri.StartsWith("tcm:") ? eclSession.TryGetEclUriFromTcmUri(myEclOrTcmUri) : eclSession.HostServices.CreateEclUri(myEclOrTcmUri);
  if (eclUri != null)
  {
    // we have a valid ecl item
    IContentLibraryMultimediaItem eclItem = (IContentLibraryMultimediaItem)eclSession.GetContentLibrary(eclUri).GetItem(eclUri);

    // build template attributes (attributes in our image or link element)
    // see https://code.google.com/p/sdl-tridion-world/source/browse/ECL%20TBB/trunk/ECL%20Template%20Building%20Blocks/ResolveEclItems.cs#372 for examples
    // lets use an empty list in this sample code for now
    IList<ITemplateAttribute> attributes = new List<ITemplateAttribute>();

    // get direct link to published ecl item
    string publishedPath = eclItem.GetDirectLinkToPublished(attributes);
  }        
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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