We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28033
    • 925 Posts
    This is kinda a weird question. See, I coded a custom snippet that can create XHTML tags with something like...

    [[link &url=`/index.php` &rel=`blank` &css=`linkstyle`]]


    Which will make a < a > tag with all of the necessary stuff. Now, sometimes, all my site will need is the &url field, and it’s coded so everything "defaults" to something. But I’d prefer not to have to put self and a blank style on everything, which looks bad. So is there any downside in having "nothing" in those spots, i.e. broswer issues? I’ve never tried it, so I don’t know, hence this topic.
      My Snippets
      -> PopUpChunk v1.0
      • 10487 MODX Staff
      • 1,535 Posts
      Not sure on the validation side of things but can’t you adjust your coding to not output the attributes if they are not passed in the snippet call? That would seem the better option to me. You could use PHP’s isset() function to check for the existence of the parameters when constructing your output.

        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 28033
        • 925 Posts
        Validation isn’t a biggie for me. The main issue is if IE/etc. messed up because of it. As a basic example, here’s the code I have so far...

        <?php
        // CONFIGURATION SETTINGS
        // $colspan [string]
        // This setting controls how many colspans get attached to a <td> tag. Default is 1 (one).
        $colspan = isset($colspan) ? $colspan : '1';
        
        return '<td colspan="'.$colspan.'">';
        ?>


        Now, if I had another code in there, like for the class= attribute, would there be a way have it appear only if the attribute exists in the snippet call?

        (FYI, I’m a PHP newbie. I got this basic stuff working by learning from snippets I already had installed, and some points some users had told me about for a small snippet I did earlier.)
          My Snippets
          -> PopUpChunk v1.0
          • 3749
          • 24,544 Posts
          Quote from: Soshite at Oct 24, 2007, 08:41 PM

          <?php
          // CONFIGURATION SETTINGS
          // $colspan [string]
          // This setting controls how many colspans get attached to a <td> tag. Default is 1 (one).
          $colspan = isset($colspan) ? $colspan : '1';
          
          return '<td colspan="'.$colspan.'">';
          ?>


          If I understand you correctly, this should do it (note: untested):

          <?php
          $colspan = isset($colspan) ? $colspan : '1';
          
          if (isset($class)) {
              return ('<td class="'.$class.'" colspan="'.$colspan.'">';
          } else {
               return '<td colspan="'.$colspan.'">';
          }
          ?>
          


          Bob
            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
            • 28033
            • 925 Posts
            EDIT: There was one error with the (). Here’s what I got to make it work, although some of the names are changed from yours...

            if (isset($css)) {
                return '<td colspan="'.$colspan.'" width="'.$width.'" class="'.$css.'">';
            } else {
                return '<td colspan="'.$colspan.'" width="'.$width.'">';
            }
              My Snippets
              -> PopUpChunk v1.0
              • 3749
              • 24,544 Posts
              Sorry about that. It’s amazing how often I do that, even with color highlighting. I’m used to having an editor that autocompletes parentheses and tags. I’ve edited my message for the benefit of future readers.

              I’m glad you got it working. I hope it didn’t take you too long to find the error.

              Bob
                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
                • 28033
                • 925 Posts
                I haven’t done this yet, but PHP allows if/else statements inside of if/else statements, right?

                Basically, like in a program, you have a if command. If the program finds that data in the call, it’ll go to another if/else command. If not, it just goes to the else like normal.

                I was wanting to use this for an IMG tag, having a call like &link=`y`, which then checks to see if it’s a normal image with text attached, or if it’s a slimbox/lightbox image that needs the special rel attribute. If &link doesn’t exist, it’ll just make a normal image link.

                In theory, I would think that should work. wink

                (And it’s no problem. I’ve done numerous HTML/XHTML/CSS mistakes before.)

                [ Once I get this "perfected" enough, I might make it a into a ZIP for download on the Resources section --- someone might have a use for this (like disabling HTML, but allowing this to be used in it’s place), or even think of a different way to use it. Of course, I’ll credit you for it. smiley ]
                  My Snippets
                  -> PopUpChunk v1.0
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  According to the PHP documentation:
                  If statements can be nested indefinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.
                  http://il2.php.net/if
                    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
                    • 28033
                    • 925 Posts
                    Nice. Thanks for the link. grin
                      My Snippets
                      -> PopUpChunk v1.0
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Yes, I thought the user discussion at the bottom was interesting as well.
                        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