Looping and TemplateRepeatIndex in Dreamweaver template
Asked Answered
D

2

6

I am having some trouble with accessing variables, here in this case Setvariable. When I go inside loop, variable doesn't exists. Anyone have any insight on this. Appreciate your help

Below is my code section in template. Would you please help when you get a chance? Thanks.

<!-- TemplateBeginRepeat name="Component.Fields.section" -->
@@SetVariable("columnSectionIndex", "${TemplateRepeatIndex}")@@
Inline Value @@GetVariable("columnSectionIndex")@@       Variable value can be accessed
    <!-- TemplateBeginRepeat name ="Field.links" -->
      Inside Loop Value @@GetVariable("columnSectionIndex")@@  //Not getting declared           variable //value here. Says variable doesn’t exist in ContextVariables.
       <!-- TemplateBeginRepeat name ="Field.linkimages" -->
       <!-- TemplateEndRepeat -->
    <!-- TemplateEndRepeat -->
<!-- TemplateEndRepeat -->

Output

Variable Added Successfully
Inline Value 0 
Inside Loop Value Variable doesn't exist 

My dwt code

[TemplateCallable()]
public string SetVariable(string variableName, string value)
    {
        //Remove the old variable and set the new variable
        if (_Engine.PublishingContext.RenderContext.ContextVariables.Contains(variableName))
        {
            _Engine.PublishingContext.RenderContext.ContextVariables[variableName] = value;
            return "Variable Modified Successfully";
        }
        else
        {
            _Engine.PublishingContext.RenderContext.ContextVariables.Add(variableName, value);
            return "Variable Added Successfully";
        }
    }
    [TemplateCallable()]
    public string GetVariable(string variableName)
    {
        //Get the varialbe
        if (_Engine.PublishingContext.RenderContext.ContextVariables.Contains(variableName))
            return _Engine.PublishingContext.RenderContext.ContextVariables[variableName].ToString();
        else
            return "Variable doesn't exist";
    }
Dealate answered 18/4, 2012 at 11:40 Comment(0)
F
5

Problems with variables in loops are well known and even documented.

Basically the first loop is already evaluated by the time you set your variable, so you will always be off by one.

  • Set variable i=0
  • Loop Iteration 1, i=null
  • Loop Iteration 2, i=0
  • Loop Iteration 3, i=1
  • etc
Folberth answered 18/4, 2012 at 13:17 Comment(1)
You may want to mark it as the answer then, so it helps others with the same question.Folberth
G
4

This might help: http://www.tridiondeveloper.com/get-and-set-variables-in-dwts

Grenadier answered 18/4, 2012 at 12:30 Comment(1)
Thank you Bappi for your time. I used the same code from tridiondeveloper but didnt work.Dealate

© 2022 - 2024 — McMap. All rights reserved.