We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27716
    • 126 Posts
    Hi Guys,

    Im trying to create a snippet where I pass the snippet variables into a JSON array.

    Need some basic guidance.

    My Snippet Call will be

    [[!callSnippet? &fileLocation=`http://file.com`]]


    Below is my attempt at the snippet. When executed it calls another class and sends an API request.

    <?php
    require_once("/class.php");
    
    $fileLocation = isset($fileLocation)? $fileLocation : '';
    
    
    $new_job = new ProcessJob('
      {
        "api_key": "xxxxxxxxxxxxxxxxx",
        "input": "[color=red]$fileLocation[/color]",
        "outputs": [
          {
            "label": "web"
          }
        ]
      }
    ');


    The red part is currently failing and the variable isn’t going from the snippet to the Json.

    Assistance appreciated....

    Stuart
      • 27716
      • 126 Posts
      Hi Guys,

      Managed to solve this by replacing

      $fileLocation


      with....

      '.$fileLocation.'
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        You might want to use "empty()" instead of "isset()".
          • 27716
          • 126 Posts
          Ok smiley

          Will take your advice....

          What is the difference, Only have a vague conception of either.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            A variable can be defined with an empty value and in that case isset() will return true, since the variable is in fact set, while empty() will also return true since it is empty. So if you use !empty() it will make sure that there is actually a value there to work with. Sometimes it’s useful to use both: if(isset(...) && !empty(...)) ...
            http://itslennysfault.com/php-isset-vs-empty
            Using empty() can be dangerous if 0 is a valid value, such as the "parent" of top-level resources, or the number of children a resource has if it has none. Some odd bugs have occurred using PHP functions that presume a value of 0 equates to false or empty, since MODx makes use of a value of 0 in a number of places.
              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
              • 27716
              • 126 Posts
              Hi Susan,

              Thanks so much for the info.

              I think I get the gist, even though I’m new to writing this kind of code... smiley

              But making progress.

              Thanks again.

              Stuart
                • 3749
                • 24,544 Posts
                You can also make it a regular array and use:
                <?php
                
                $jsonString = $modx->toJSON($arrayName);
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting