We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14941
    • 1 Posts
    Sorry, I’m a newbie. How do I evaluate a template variable in php to use it in a logic statement?

    simplified example:
    // calling function from a chunk
    [[mysnippet?myvar="[*my_template_var*]"]] //my_template_var has a default value of 1


    // mysnippet code
    echo $myvar; // outputs "[*myvar*]" to the output file, then modx evalutes it to 1 and replaces [*myvar*] in the output file with 1

    $x = (int)$myvar; // x is 0 because myvar containts "[*myvar*]", not 1

    if ($x == 1) { // always evaluates false
    echo ’1’;
    } else {
    echo ’0’;
    }
    ?>
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Snippet parameters are not surrounded with quote marks, rather with backticks `value`.

      You can deal with TVs programmatically within snippets using the API function
      $myvar = $modx->getTemplateVar

      http://wiki.modxcms.com/index.php/API:getTemplateVar
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org