We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13986
    • 90 Posts
    So I created my first snippet.. nothing great but does what i need it to.

    Problem is I need to use the same snippet call (with different parameters) on the same page. But this isn’t working. It does the first call correctly and then just spits out the second snippet call to the page.

    Any idea why?
    Here’s my snippet code:
    <?php
    // initialize variables
    $bh_int_style = isset($OutputStyle) ? $OutputStyle : '';		// check for style parameter
    $bh_image_name = isset($ImageName) ? $ImageName : '';			// check for image parameter
    $bh_int_mapnum = isset($MapNum) ? $MapNum : '';					// check for map number parameter
    $bh_branch_name = isset($BranchName) ? $BranchName : '';		// check for branch name parameter
    $bh_branch_url = isset($BranchUrl) ? $BranchUrl : '';			// check for branch url parameter
    $bh_branch_add = isset($BranchAdd) ? $BranchAdd : '';			// check for branch address parameter
    $bh_open_times = isset($OpenDaysHours) ? $OpenDaysHours : '';	// check if proper parameters set
    $bh_array_days = '';											// holds the inital days array
    $bh_array_open_close = array();									// holds finial days, open and close times
    $bh_output = '';												// holds finial output
    $bh_lab_vic = isset($SundayReason) ? $SundayReason : '';		// for sundays hour message
    $bh_output_days = '';
    
    include($modx->config['base_path'].'assets/snippets/test/test.inc.php');
    ?>


    And the page that calls the snippet twice:
    [[hours? &OutputStyle=`1` &ImageName=`test-small.jpg` &MapNum=`1` &BranchName=`test` &BranchUrl=`test.html` &BranchAdd=`test` &OpenDaysHours=`test|test|test|test|`]]
    
    [[hours? &OutputStyle=`1` &ImageName=`test2-small.jpg` &MapNum=`2` &BranchName=`test2` &BranchUrl=`test.html2` &BranchAdd=`test2` &OpenDaysHours=`test2|test2|test2|test2|`]]
      Jeremy Foster
      • 10487 MODX Staff
      • 1,535 Posts
      Have you tried calling the snippets uncached, like [! ... !]
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 4041
        • 788 Posts
        If using it uncached does not work, you might also try using

        unset($variable_names_used_above);

        at the end of the snippet.
        Replacing $variable_names_used_above with the set variables at the top of your snippet

        I’ve had to do that in the past to be able to reuse the same snippet twice.
          xforum
          http://frsbuilders.net (under construction) forum for evolution
          • 24719
          • 194 Posts
          this can be caused if your included file defines a function. the second time the file is included, it will be trying to redefine an existing function.

          so, to solve this (if this is the problem), wrap your function in the following code:
          if (!function_exists('myfunc'))
          {
              function myfunc() { .. }
          }
          


          if you’re defining a class, use ’!class_exists’ instead of ’!function_exists’.
            • 13986
            • 90 Posts
            Thanks for the replies, nothing seemed to fix it, so...

            I started from scratch and rewrote the snippet and it’s working fine now.

            I have no idea what went wrong the first time. Caulk it up to being a first time snippet coder.

            Thanks again. smiley
              Jeremy Foster
              • 34017
              • 898 Posts
              What I usually do is include the file with the class functions, then run in the class/function in the modx snippet like so:

              //modx snippet
              
              $param1 = isset....
              $param2 = isset....
              
              include_once(my-folder/my-snippet-function.php);
              
              $output = myfunction($param1, $param2);
              return $output;
              
              


              That’s how I’ve gotten my snippets to run multiple times per page.
                Chuck the Trukk
                ProWebscape.com :: Nashville-WebDesign.com
                - - - - - - - -
                What are TV's? Here's some info below.
                http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                • 13986
                • 90 Posts
                I finally figured it out... and it’s the most stupid thing (it started happening again, so I had to solve it).

                It’s a TinyMCE issue. I would type the snippet call in [[....]] and then to duplicate it I would highlight and copy the snippet call in TinyMCE. Then paste it back into TinyMCE to change the parameters ..this screws the second snippet call.

                Lesson: Type the snippet code in, don’t copy paste. lipsrsealed
                  Jeremy Foster
                  • 46391
                  • 1 Posts
                  I got the same problem, try catch error is: TypeError: $.browser is undefined. So after some reading on JQuery it turns out $.browser is removed in JQuery 1.9 up.

                  To fix add this after your JQuery link (migrate plugin):
                  <script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>


                  Problem solved, hope this helps someone!
                    • 37008
                    • 11 Posts
                    …and be aware of the different versions: development (generates console output) and production (minified and no console warnings) – https://github.com/jquery/jquery-migrate/