6

変数(この場合はSetvariable)へのアクセスに問題があります。ループ内に入ると、変数が存在しません。誰もがこれについての洞察を持っています。あなたの助けに感謝

以下は、テンプレートの私のコードセクションです。機会があれば手伝っていただけませんか?ありがとう。

<!-- 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 -->

出力

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

私の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";
    }
4

2 に答える 2

5

ループ内の変数の問題はよく知られており、文書化されています。

基本的に、最初のループは、変数を設定するまでにすでに評価されているため、常に 1 つずれます。

  • 変数 i=0 を設定
  • ループ反復 1、i=null
  • ループ反復 2、i=0
  • ループ反復 3、i=1
于 2012-04-18T13:17:12.053 に答える
4

これは役立つかもしれません: http://www.tridiondeveloper.com/get-and-set-variables-in-dwts

于 2012-04-18T12:30:35.470 に答える