We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7557
    • 61 Posts
    This is an auto-generated topic for ifEval 0.9-beta by dj13.

    Brief Description:

    Snippet for comparison operations on TV, placeholders and other values.
    Use the correct PHP code for input parameter, as the expression in the 'IF' function in PHP.
    IMPORTANT: This snippet uses PHP EVAL function. It should be used with careful security precautions.
      • 7557
      • 61 Posts
      ifEval

      Snippet for comparison operations on TV, placeholders and other values.
      Use the correct PHP code for input parameter, as the expression in the ’IF’ function in PHP.
      IMPORTANT: This snippet uses PHP EVAL function. It should be used with careful security precautions.

      @ author Djordje Dimitrijev (dj13 on Modx forum)
      @ version 0.9 beta

      CURRENT VERSION NOTES:

      Added possibility for unlimited number of snippet parameters.
      New way for naming parameters.
      Added support for placholders.
      Added processElementTags suggested by Bert (bertoost on Modx forum).
      And much more smiley

      VERSION 0.1 pl NOTES:

      First initial version.

      IMPORTANT:

      Support for default naming parameters from ifEval v0.1 (input, contentFalse, contentTrue)
      will be removed in next final version.

      USAGE:

      First select a name as a base for the parameter names. Say, let it be MyVar
      All possible parameters with MyVar are (7):

      ccMyVar - Statement expresion (require).

      rrMyVarTrue - (Opt) Will be returned if the statement is correct.
      rrMyVarFalse - (Opt) Will be returned if the statement is not correct.

      evalMyVarTrue - (Opt) Evaluate and return if the statement is correct.
      evalMyVarFalse - (Opt) Evaluate and return if the statement is not correct.

      chunkMyVarTrue (Opt) and
      chunkMyVarFalse (Opt) must be in JSON format in the following form:
      {"chunk_name" : {"placeholder_name": "content_for_placeholder_name" ,
      "other_placholder": "content_for_other_placholder"},
      {"other_chunk_name" : {"placeholder_123": "content_for_placeholder_123"}}
      Special: evalute content for placholder. Put the ($evalReturnPrefix default "eval") on the placholder:
      {"third_chunk_name" : {"evalThisPlaceholder": "[[mySnippet]] * 1.18"}

      $jsonQuote - Because of restrictions JSON format, and needs for quotation marks, you can use $jsonQuote default "%%"
      {"fourth_chunk_name" : {"evalPlaceholder123": "[[+price]] + %%[[delivery]]%%"}

      Output from the snippet will be in the order of parameters in the snippet call.

      EXAMPLES:

      In template with contentTrue and contentFalse parameter:
      [[!ifEval? &ccName123=`stripos("[[*pagetitle]]", "a")===0` &rrName123True=`<h3>Article with first letter A</h3>` &rrName123False=`<h3>Article with first letter different from A</h3>`]]


      In template with with several different parameters and evaluate return:
      [[!ifEval?
          &ccABC=`[[*id]]==5 && "[[*group]]"=="phone"`
          &rrABCTrue=`<img src="phone_icon.jpg" />`
          &evalABCTrue=`"<h4>Phone price: ".([[*price]]+[[!delivery? &product=`phone`]])."</h4>"`
          
          &ccQWE=`[[*id]]==5 && "[[*group]]"=="notebook"`  
          &rrQWETrue =`<img src="notebook_icon.jpg" />`
          &evalQWETrue=`"<h4>Notebook price: ".([[*price]]+[[!delivery? &product=`notebook`]])."</h4>"`
        
          &ccXYZ=`[[*id]]!=5 and [[*id]]!=23`
          &rrXYZTrue=`[[$comment]]`  
          &rrXYZFalse=`[[$links]]`
        ]]


      With chunk parameter in JSON format:
      [[!ifEval?
          &ccCondition=`"[[+user]]"=="registered"`
          &chunkConditionTrue=
              `{
                 "myChunk" :
                 {
                    "name": "[[+name]]" ,
                    "phone": "[[+phone]]",
                    "email": "[[+email]]"
                 }
               }`
          &chunkConditionFalse=
              `{
                 "myChunk2" :
                 {
                    "name": "[[+name]]",
                    "evaldate": "date(%%d-m-y%%,time())"
                 }
               }`
          &rrConditionTrue=`<a href="login.php">Login</a>`
        ]]


      Sorry for my english.

        • 36686
        • 165 Posts
        This snippet sounds very useful, but I don’t quite get how to use it.

        I have a tv called relatedLinks. If it’s YES I want to show the chunk called relatedLinks. If it’s NO I want to show the chunk noRelatedLinks. What should the call look like, and do I need anything else in the template?
          • 7557
          • 61 Posts
          Quote from: lottaar at Oct 25, 2010, 09:37 AM

          This snippet sounds very useful, but I don’t quite get how to use it.

          I have a tv called relatedLinks. If it’s YES I want to show the chunk called relatedLinks. If it’s NO I want to show the chunk noRelatedLinks. What should the call look like, and do I need anything else in the template?
          Hi
          Try something like this:
          [[!ifEval? 
          &ccLink1=`"[[*relatedLinks]]"=="YES"` &rrLink1True=`[[$relatedLinks]]` 
          &ccLink2=`"[[*relatedLinks]]"=="NO"` &rrLink2True=`[[$norelatedLinks]]` 
          ]]

          Or
          [[!ifEval? 
          &ccLink=`"[[*relatedLinks]]"=="YES"` &rrLinkTrue=`[[$relatedLinks]]`  &rrLinkFalse=`[[$norelatedLinks]]` 
          ]]


            • 7557
            • 61 Posts
            There may be a bug in the script. If there is only new parameters script does not work. I think. Until the new version just add to the beginning of the snippet code:
            $output_old = array();
              • 28173
              • 409 Posts
              I try to use ifEval like this, but that’s doesn’t work :
              [[!ifEval?
                      &ccPageFormation=`[[UltimateParent? &topLevel=`2`]]==4 and [[*id]]!=10`
                      &rrPageFormationTrue=`true`
                      &rrPageFormationFalse=`false`
                   ]]

              Is there a problem with the UltimateParent call ?
                • 28173
                • 409 Posts
                I have found the problem : I have 2 "if"/"ifEval" calls and I forgot to altern cached/uncached theses calls.
                  • 36629
                  • 3 Posts
                  Quote from: dj13 at Oct 25, 2010, 10:20 AM

                  There may be a bug in the script. If there is only new parameters script does not work. I think. Until the new version just add to the beginning of the snippet code:
                  $output_old = array();


                  Oh, thank you so much for that! I’d gotten it to work fine with the old parameters and was driving myself crazy trying to figure out what I’d done wrong with the new ones.