Get and Set Variables in DWTs

Here is a creative and useful set of Dreamweaver Custom Functions that allow instantiating your own variables in a Dreamweaver Tridion template.  Credit goes out to my fellow team members Trevor Bartlett and Riyaz Hameed.

You can do stuff like this:

<!-- TemplateBeginRepeat name="Field.columnSection" --> <div class="wpsPortlet"> <div class="wpsPortletTitle"> <br />@@Field.title@@</div> @@SetVariable(“columnSectionIndex”, "${TemplateRepeatIndex}")@@ <!-- TemplateBeginRepeat name="Field.subColumnSection" --> @@GetVariable("columnSectionIndex")@@ <!-- TemplateBeginRepeat name="Field.subTitle" --> … <!-- TemplateEndRepeat --> </div> <!-- TemplateEndRepeat -->

Here is the code:

/// <summary>
/// Sets a varialbe in the package to the name and value specifed. Also removes any other variable that was set with the same name before.
/// </summary>
/// <param name="variableName">Name of the varialbet</param>
/// <param name="value">Value of the variable</param>
[TemplateCallable()]
public string SetVariable(string variableName, object value)
{
    //Remove the old variable and set the new variable
    _engine.PublishingContext.RenderContext.ContextVariables.Remove(variableName);
    _engine.PublishingContext.RenderContext.ContextVariables.Add(variableName, value);
 
    return "";
}
 
/// <summary>
/// Gets a varialbe from the publishing context.
/// </summary>
/// <param name="variableName">Name of the variable</param>
[TemplateCallable()]
public object GetVariable(string variableName)
{
    //Get the varialbe
    return _engine.PublishingContext.RenderContext.ContextVariables[variableName];
}

5 thoughts on “Get and Set Variables in DWTs

  1. The idea is to keep it in scope of the DWT. I agree with you. My preference is to use the Package with regular TBB’s, and resort to custom DWT functions in extreme situations. This is just another option I guess.

  2. Getting an Error in TRIDION: Variable ‘GetTCMURIValueofLinkedField’ has not been declared
    Unable to get rendered content of Page (tcm:109-87820-64).
    JScriptException: Expression ‘GetTCMURIValueofLinkedField(GetTCMURIValueofLinkedField(“tcm:109-19143″, “xlink:href”, “Summary”),”xlink:href”,”MediumImage”)!=””‘ had error ‘Variable ‘GetTCMURIValueofLinkedField’ has not been declared’
    JScriptException: Expression ‘GetTCMURIValueofLinkedField(GetTCMURIValueofLinkedField(“tcm:109-19143″, “xlink:href”, “Summary”),”xlink:href”,”MediumImage”)!=””‘ had error ‘Variable ‘GetTCMURIValueofLinkedField’ has not been declared’
    JScriptException: Expression ‘RenderComponentPresentation()’ had error ‘JScriptException: Expression ‘GetTCMURIValueofLinkedField(GetTCMURIValueofLinkedField(“tcm:109-19143″, “xlink:href”, “Summary”),”xlink:href”,”MediumImage”)!=””‘ had error ‘Variable ‘GetTCMURIValueofLinkedField’ has not been declared”
    Unable to retrieve rendered data from Page.

    eval code: Line 1 – Error: Variable ‘GetTCMURIValueofLinkedField’ has not been declared

    UtilitiesPublish.RenderPage
    Page.Render
    Request.Render
    ==========================================================
    Can’t understand the reason.Below is the code:


    @@GetFieldValue(GetTCMURIValueofLinkedField(Field, ‘xlink:href’, ‘Summary’), ‘Title’)@@

    DOes anybody knows the reason?

  3. Hi Nitesh,
    Welcome to our site and thanks for posting your question. Would you please provide a some more context around what you’re trying to do? Though, to be honest, the best place to post questions about errors would be the Stack Overflow Q&A site or the SDLTridionworld forum.

    In your code there are a couple of things you may want to look at:
    1) the error message states that the function GetFieldValue does not exist. You need to define it. Please provide more
    context here.
    2) I don’t believe you can make nested function calls in DWT templates.

    It looks as though you’re trying to get values of linked components. Have a look at Nuno’s DGX extension or Will’s Add Linked Components building block. They are designed specifically for these tasks.

    Happy coding

  4. Hey Nitesh,
    Have you tried the @@GetFieldValue(${GetTCMURIValueofLinkedField(Field, ‘xlink:href’, ‘Summary’)}, ‘Title’)@@

    As in the tridion templating you have to use the delimiters in the alternating fashion i.e. @@${}@@. Try using above code this might work for you.

    Work Hard Party Hard!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>