We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27705
    • 38 Posts
    Can you use placeholders in javascript chunks?

    This keeps giving me errors right after wherever I put the placeholder.

    <script type="text/javascript">
    
    var link = [[*shoplink]]
    
    if(link != null) {
    document.write("<a href="[[*shoplink]]">Buy Now</a>");
    }else{
    document.write("Call for pricing");
    }
    
    </script>

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I would imagine that the javascript requires that value to be in quotes like any other string; ’[[*shoplink]]’
        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
        • 27705
        • 38 Posts
        That stopped the error but the placeholder variable wasn’t passing through.

        Is there an easier way to see if a placeholder is null in a chunk?
          • 27705
          • 38 Posts
          I decided to try it in PHP instead and it seems like I’m getting more progress like that using
          [[shop-linkage]]


          snippet in my chunk with the snippet containing
          $shoplink = $modx->getPlaceholder('shoplink');
          
          if ($shoplink != null){
          echo "<a href='" . $shoplink . "'>Buy Now</a>";
          }else{
          echo "Call for pricing";
          }


          But the $shoplink variable is always coming back as null.

            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Well, [[*...]] is not a placeholder. [[+...]] is the tag for placeholders.
              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
              • 27705
              • 38 Posts
              I think I may be using the wrong terminology. I was trying to access a template variable. I ended up having to pass the variable through the snippet such as [[!Snippet?phpvariable=[[*templatevariable]]]]

              Think I finally found this solution in an old thread of yours.

              Thanks for the help!