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

    When creating snippets or plugins it’s sometimes difficult to have to copy and paste code to from your PHP into the Manager.

    I’ve found an easier to developing snippets. It’s very simple. All you have to do is to create a folder (best to use same name as snippet) inside the assets/snippets folder and then create a .php file to store your snippet code. For example:


    assets/snippets/mysnippet/snippet.mysnippet.php

    The above file would look something like:

    <?php

    /**
    * MySnippet
    */

    return "Hello world";

    ?>


    From within the manager create a snippet called MySnippet and insert the following into the body of the snippet


    return include ’assets/snippets/mysnippet/snippet.mysnippet.php’;

    When you have successfully completed the development of your snippet you can copy and paste the code between the <?php ?> tags into the manager.


    That’s it!
      xWisdom
      www.xwisdomhtml.com
      The fear of the Lord is the beginning of wisdom:
      MODx Co-Founder - Create and do more with less.
      • 4018
      • 1,131 Posts
      Damn good tip! Thanks for sharing!
        Jeff Whitfield

        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
        • 1764
        • 680 Posts
        That’s exactly what I need. I was just thinking the other day that someone should make a way to have a snippet as a file so that you could use an editor. Great tip! Thanks.
          • 32963
          • 1,732 Posts

          Many thanks. I glad you like it smiley

            xWisdom
            www.xwisdomhtml.com
            The fear of the Lord is the beginning of wisdom:
            MODx Co-Founder - Create and do more with less.
            • 4018
            • 1,131 Posts
            While I’m thinking about it...

            I haven’t done this yet but will likely be doing this very very soon with a snippet I’m working on. How does MODx handle includes in Snippets? Do they pretty much work the same as if it were in a separate PHP file? Reason I ask is that I have an existing site I’m moving into MODx with some custom Dreamweaver PHP code. I’m pretty sure I can make a snippet to handle the code and just use a return variable for the output via heredoc code. Guess I’m wondering what to expect. Getting a handle on this would be good since I’ll likely be developing some serious ass snippets (ie. link directory module!!).
              Jeff Whitfield

              "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
              • 32963
              • 1,732 Posts

              What I would do is use the ob_start() functions to trap text generated by my include files.

              ob_start()
                 include "whatere.php";
                 startApp(); // here my app uses echo to output text
                 return ob_get_content();
              ob_end_clean();


                xWisdom
                www.xwisdomhtml.com
                The fear of the Lord is the beginning of wisdom:
                MODx Co-Founder - Create and do more with less.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                I did this for some restricted database access, and other than not having the API functions available, it worked fine. You can write it as any .php file, with more includes and whatever you would usually do. I just returned everything at the end, as you would do in a snippet; I didn’t use a function in the snippet, just an include statement. I suspect that the final return statements are redundant, but did it in both places anyway just in case. Doesn’t seem to hurt anything.

                include "./assets/snippets/termbase/termbase.inc.php";
                return $output;


                And in the included .php file, I included manager/includes/config.inc.php and wrote the code:

                ...
                
                // get the languages available in the database
                function getLanguages() {
                    include "./manager/includes/config.inc.php";
                    $db = mysql_connect($database_server,$database_user,$database_password) or die("cannot connect to database");
                    $selected = @mysql_select_db($dbase, $db) or die ("cannot select database");
                    $query = "SHOW COLUMNS FROM tb_termbase";
                    $result = mysql_query($query) or die ("Cannot query the database"); 
                    $languages = array();   
                    if (mysql_num_rows($result) > 0) {
                        while ($row = mysql_fetch_assoc($result)) {
                            $languages[] = $row['Field'];
                        }
                    }
                    $languages = array_slice($languages, 2);
                    return $languages;
                }
                
                ...
                
                // start drawing the result table
                $output .= "<table class='wide' id='glossary' border='0' cellpadding='0' cellspacing='0'>";
                $output .= "<tr>";
                foreach($display_languages as $language) {
                    $output .= "<th>$language</th>";
                }
                $output .= "</tr>";
                for($j=0;$j<$rows;$j++) {
                    $output .= "<tr>";
                    $list = mysql_fetch_row($result);
                    for($i=0;$i<count($list);$i++) {
                        $style = $j%2 == 0 ? 'data1' : 'data2';
                        $output .= "<td class='data $style'>$list[$i]</td>";
                    }
                    $output .= "</tr>";
                }
                $output .= "</table>";
                
                etc. etc.
                
                return $output;
                ?>
                


                This is why I was interested in a separate database abstraction class file.
                  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
                  • 32963
                  • 1,732 Posts
                  Hmmmm,

                  I can’t see why you would include the config.inc.php file there

                  This should work just fine:

                  function getLanguages() {
                     gloabl $modx;
                  
                     // your code here
                  
                  }


                  from within the getLanguages() function you will be able to access the $modx->db object

                  This can be done for any file included inside a snippet whether is was directly or indirectly included

                  Give it a try and let me know what happend
                    xWisdom
                    www.xwisdomhtml.com
                    The fear of the Lord is the beginning of wisdom:
                    MODx Co-Founder - Create and do more with less.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    You are probably quite correct. This was my first attempt at a serious snippet, and modx was still just a user login mod for Etomite.
                      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
                      • 4673
                      • 577 Posts
                      can something like this be done for templates?

                      I’m getting pretty sick and tired of copying, pasting, and saving ... ugh!
                        Tangent-Warrior smiley