We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Say I’ve got a custom table of categories, and I want to provide a link to a page that passes the category ID so that the links are something like this:

    <li><a href="/path/to/mypage?category_id=123">My Category</a></li>
    <li><a href="/path/to/mypage?category_id=411">My Other Category</a></li>


    I can do this pretty easily using the ~ syntax, e.g. my template chunk might look like this:
    <li><a href="[[~2]]?category_id=[[+category_id]]">[[+name]]</a></li>


    BUT... this solution isn’t fail-safe. If Friendly URLs are disabled, it will fail... you’d get something like index.php?q=/path/to/my/page?category_id=123 -- 2 "?"’s would be a trainwreck, no?

    So is there a cool MODx way to safely append arguments to a URL that will handle the case that FURLs are disabled? Something like this maybe:

    <li><a href="[[~2:category_id=`123`]]">My Category</a></li>


    Know what I mean?
      • 4041
      • 788 Posts
      Having that as an output filter would be cool, but heres how I usually solve that issue.

      Create a snippet with the following code and add it to the header of the template:
      <?php
      // revolution  [[+link_sep]]
      $link_sep = $modx->getOption('friendly_urls') =='1' ? '?' : '&';
      
      // evolution   [+link_sep+]
      //$link_sep = $modx->config['friendly_urls'] =='1' ? '?' : '&';
      
      $modx->setPlaceholder('link_sep', $link_sep);
      ?>


      Then use [[+link_sep]] (revo) or [+link_sep+] (evo) as needed.
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Yeah... a Snippet would do the trick. It looks like SOMETHING is happening when you add some output filters, e.g.:
        [[~1:ucase]]


        Outputs:
        /?FILTER_COMMANDS=ARRAY&FILTER_MODIFIERS=ARRAY 


        But this isn’t doing much for the backend... I set up a "Testing" Snippet with the following code:
        print_r( get_defined_vars() ); 
        exit;


        Compare [[~1:Testing]]
        Array
        (
            [scriptProperties] => Array
                (
                    [input] => /?filter_commands=Array&filter_modifiers=Array
                    [options] => 
                    [token] => 
                    [name] => 1
                    [tag] => [[~1:Testing]]
                )
        )



        To a "legitimate" Output Filter [[*alias:Testing]]:
        Array
        (
            [scriptProperties] => Array
                (
                    [input] => test
                    [options] => 
                    [token] => *
                    [name] => alias
                    [tag] => [[*alias:Testing]]
                )
        )



        How do you access filter_commands in the Snippet? I can read the $input, $options, $name, etc... so I can write a custom Output filter that will handle this... but the behavior here is weird.
          • 28215
          • 4,149 Posts
          [[~2? &category_id=`123` &anotherParam=`value`]]
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Thanks Shaun! I should have known there was a simpler way.