简体   繁体   English

Dreamweaver模板中的循环和TemplateRepeatIndex

[英]Looping and TemplateRepeatIndex in Dreamweaver template

I am having some trouble with accessing variables, here in this case Setvariable. 我在访问变量方面遇到了一些麻烦,在本例中为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 我的dwt代码

[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";
    }

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 设置变量i = 0
  • Loop Iteration 1, i=null 循环迭代1,i = null
  • Loop Iteration 2, i=0 循环迭代2,i = 0
  • Loop Iteration 3, i=1 循环迭代3,i = 1
  • etc 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM