We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37031
    • 93 Posts
    I'm having a strange issue. I have my environment setup as described in Developing an Extra in MODX Revolution (http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution) so I'm creating my snippets using Coda in a remote directory on my local server. I perform all my logic and try

    return $output;

    and nothing is returned with no error. It doesn't even seem to be working. But if I try

    echo $output;
    return;

    all is good. Can anyone please explain?
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      what's your code and how are you calling it?
        • 37031
        • 93 Posts
        Thanks Everett. I have a snippet called scShowAssignments:
        <?php
        $base_path = $modx->getOption('core_path').'MODXComponents/studentcentre/trunk/core/components/studentcentre/';
        
        $f = $base_path.'snippet.scShowAssignments.php';
        if (file_exists($f)) {
           include $f;
        } else {
           $modx->setLogTarget('ECHO');
           $modx->log(modX::LOG_LEVEL_ERROR,'scShowAssignments not found at: '.$f);
        }
        
        return;

        Then at MODXComponents/studentcentre/trunk/core/components/studentcentre/ I have a PHP file called snippet.scShowAssignments.php:
        <?php
        /*
        *
        * @package studentcentre
        *
        */
        
        $path = MODX_CORE_PATH . 'components/studentcentre/';
        $result = $modx->addPackage('studentcentre', $path . 'model/', 'sc_');
        if (!$result) {
          return 'Failed to add StudentCentre package';
        }
        
        // get the logged in student id
        if ($modx->user->get('username') != '(anonymous)') {
            $studentId = 36; // $modx->user->get('id');
        } else {
        	$url = $modx->makeUrl(55,'','','full'); 
        	$modx->sendRedirect($url);
        	return;
        }
        
        $output = '';
        
        // get the collection of student assignments for that student
        $c = $modx->newQuery('scStudentAssignment');
        //$c->prepare();
        //echo $c->toSQL();
        $c->where(array(
        	'student_id:=' => $studentId
        ));
        $c->sortby('status','DESC');
        $assignments = $modx->getCollection('scStudentAssignment', $c);
        
        if (count($assignments) > 0) { // if the array is not empty...
        
        	// for each assignment, output it's properties along with a link to the assignment
        	foreach ($assignments as $assignment) {
        		$fields = $assignment->toArray();
        		$assignmentUrl = $modx->makeUrl(2658,'',array(
        			'aid' => $assignment->get('id')
        		));
        		$fields['url'] = $assignmentUrl;
        		$output .= $modx->getChunk('csAssignmentRowTpl', $fields);
        	}
        	
        } else { // if there are no assignments
        	$msg = $modx->getChunk('csMessageTpl', array(
        		'class' => 'msg',
        		'text' => 'Sorry but there are no assignments available to you yet.'
        	));
        	return $modx->setPlaceholder('csMessage', $msg);
        }
        echo $output;
        return;

        Thoughts?
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Is your Snippet call cached or uncached?

          A couple random thoughts (don't know if these would make a difference):

          * $modx->setLogTarget('ECHO'); --> sometimes that can prove wonky, especially with Ajax requests. I've almost always found it better to log errors to a file. Maybe something there is causing *all* Snippet output to be routed to standard out instead of being returned like normal, so your printing statement works when your return statement fails (?)

          * "include" might not be the proper function here... include_once, require, or require_once are usually what's required. Can that file be safely included multiple times? And can your script when the file fails to load?

          * am I reading that wrong, or are you dealing with 2 different paths?
          core/MODXComponents/studentcentre/trunk/core/components/studentcentre/
          vs.
          core/components/studentcentre/

          The tried and true method here would to go through line by line (or bit by bit) and try returning a message, e.g. "return __LINE__;" and see if and when your Snippet goes south.

          Hope that helps.
            • 37031
            • 93 Posts
            Thanks for the tips Everett I really appreciate it. I was following the tutorial at
            http://rtfm.modx.com/display/revolution20/Using+Custom+Database+Tables+in+your+3rd+Party+Components
            and essentially tried following it to the letter so that I could use Coda to develop (instead of developing within MODX). Maybe I'll just move the code inside MODX to simplify things. Thanks again Everett!
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Your snippet has to have something to return. Just an empty return statement won't return anything. Your included file needs to return its output to the snippet, then the snippet can return that output.
                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
                • 3749
                • 24,544 Posts
                Hmmm . . . I left a message here, but it seems to have disappeared.


                When you include your code in the snippet, you have to use something like this:


                return include $f;


                Also, setPlaceholder() doesn't return anything useful, so it would be clearer, and a little safer, to use this:

                $modx->setPlaceholder('csMessage', $msg);
                return '';

                  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