We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11858
    • 130 Posts
    I need to pass a URL as a parameter of a snippet. There's slashes, ampersands, equals signs, etc and my snippet call chokes on them:
    [[!updateEntrantDB? &eventCSVLink=`http://www.something.com/activereports/smartLink.aspx?eventid=LFQWZI9uO/h0s=&crid=13246`]]
    



    Does anyone know how I can escape the illegal characters? [ed. note: Hat-rack last edited this post 15 years, 2 months ago.]
      • 4041
      • 788 Posts
      You'll have to do some character replacements, the sample snippet below is a basic example based on the link structure you posted above.:


      /*

      [ [! updateEntrantDB?
      &eventCSVLink=`www.something.com/activereports/smartLink.aspx+eventid-LFQWZI9uO/h0s-|crid-13246`
      &eventCSVLinkTitle=`My Link Title`
      ] ]
      * the above snippet call produces this on the page:
      <a href="http://www.something.com/activereports/smartLink.aspx?eventid=LFQWZI9uO/h0s=&crid=13246" target="_blank" rel="no_follow">My Link Title</a>

      */
      $output ='';

      $eventCSVLink = isset($eventCSVLink) ? $eventCSVLink : '';
      $eventCSVLinkTitle = isset($eventCSVLinkTitle) ? $eventCSVLinkTitle : '';

      if($eventCSVLink !=''){

      $replace = array(
      '+' => '?',
      '-' => '=',
      '|' => '&'
      );

      $link = str_replace(array_keys($replace), array_values($replace), $eventCSVLink);

      $output .='<a href="http://'.$link.'" target="_blank" rel="no_follow">'.($eventCSVLinkTitle !='' ? $eventCSVLinkTitle : $link).'</a>';

      return $output;
      }

      Edit: the code tags seem to kabosh the code smiley [ed. note: breezer last edited this post 15 years, 2 months ago.]
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        You can also use a wrapper snippet that calls your snippet with $modx->runSnippet('snippetName', array('param1'=>'value1','param2'=>'value2'); and the params and values can have whatever you like.
          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
          • 6705
          • 79 Posts
          Quote from: sottwell at Aug 26, 2011, 02:06 AM
          You can also use a wrapper snippet that calls your snippet with $modx->runSnippet('snippetName', array('param1'=>'value1','param2'=>'value2'); and the params and values can have whatever you like.

          I hope it's not too late to post to this thread..

          I'm not sure if I'm understanding this properly, but it looks as though the above allows arbitrary parameters to be passed into another snippet (which is useful to know), but only if the user is able to create this 'wrapper' snippet by themself? I presume this would still have the same problem with special characters (equals, ampersand, question mark, etc) if you tried to tried to pass them as normal snippet parameters in a call to this snippet from a normal MODx page?

          My particular problem is that I want to allow page authors to call a particular snippet in a page and pass as a parameter an HTML string (which will in this case include these special characters). Unfortunately, I can't expect my users to have sufficient technical knowledge to be able to correctly break down the HTML (which is widget 'embed code' generated by a third-party) into multiple parameters to be passed into the snippet individually, and nor do I want to give their user role the privileges to create new snippets as I can't expect them to have the knowledge to do this properly.

          Is there any other way that I could have the users pass in an HTML string (other than the special character substitution previously suggested, which unfortunately also requires too much technical knowledge from my users regarding which characters they need to substitute)?

          (I'm using MODx Evolution 1.0.6, by the way.)
            Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
            • 3749
            • 24,544 Posts
            You could have the users paste the HTML string into a template variable or a chunk. Then it would be available in the snippet with either


            $html = $modx->resource->getTVValue('TvName');


            or

            $html = $modx->getChunk('chunkName');





            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html
              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
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              In Revolution you wont have to do any escaping like this on special characters (the backtick being the sole exception AFAIK), but in Evo you probably do.
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.