We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40735
    • 119 Posts
    Hi!
    I'm writing a snippet that will pull data from Flickr and I want to be able to pass the set name to the snippet from my template. My snippet call looks like this:
    [[!flickrousel &setname=`home`]]

    My code looks like this:
    //find set ID from user-supplied name
    foreach((array)$sets["photoset"] as $set){
    	if($set["title"] == $setname){
    		$setid = $set["id"];
    		break;
    	}
    }
    

    I get an error on the page that says "Undefined variable: setname in /var/www/html/core/cache/includes/elements/modsnippet/38.include.cache.php on line 28" but I can't see what I'm doing wrong. I've read http://rtfm.modx.com/display/revolution20/Snippets and it seems like I'm doing everything it suggests. What am I missing?
    Thanks!

    This question has been answered by dwillis210. See the first response.

      • 4172
      • 5,888 Posts
      This is an eNotice - error
      $setname isn't defined in your snippet.

      you can modify your error-reporting in your php.ini
      or you can set your modx-system-setting 'debug' to 0

      or, if you want to do it correct, define the variable in your snippet with something like that:

      $setname = $modx->getOption('setname',$scriptProperties,'');
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 40735
        • 119 Posts
        Well, simply suppressing the error doesn't help me because I need the value in the variable. According to the documentation, any variable in the snippet call should be available in the snippet with the same name as stated here:
        Passing Values Into a Snippet

        Values are passed to your Snippet using a modifed CGI web-form type notation that follows the Snippet's name. If your Snippet were named "mySnippet", you might call it using something like this:

        1
        [[!mySnippet? &input=`Hello World`]]
        And the code for your Snippet might look something like this:

        1
        2
        3
        <?php
        return 'My input was: ' . $input;
        ?>
        Notice that the variable names in the calling bit need to match the variable names in the Snippet EXACTLY (case matters... i.e. 'input' not 'INPUT' or 'Input'). Secondly, don't forget the '&' in front of the would-be variable names. And last but most certainly not least, take note that those are backticks, not single quotes!

        I tried what you suggested with the getOption method and it stopped the error from being displayed but didn't do anything else.
        • discuss.answer
          • 40735
          • 119 Posts
          Alright, I left the ? out of my snippet call. Adding it in fixed the problem. This is the second time I've posted about a problem and that was the cause. I should check my code more carefully. lol
            • 38121
            • 36 Posts
            The part of the documentation you quote is partial or outdated. It works but [it isn't recommended and] you can pass a single argument.
            As Bruno57 mentioned it, you should use $modx->getOption() to retrieve a snippet's argument.

            There a few common errors why a snippet's argument can't be retrieved:

            In the snippet call:
            - misspelling of the name of the snippet, that one is often overlooked,
            - misspelling of the parameter name, this must be the same as the first argument in the getOption() call
            - forgetting the question mark after the snippet name,
            - forgetting the ampersand before each parameter,
            - using single quotes around parameters' values instead of backticks.

            In the snippet itself
            - not setting up the snippet parameter as a snippet property.
            - putting a space after the commas in the getOption() call => ('setname', $scripProperties, '') => the proper way is no spaces, yep, this one is tricky.
            - misspelling $scripProperties

            In your specific case, this should work:
            [[flickrousel? &setname=`home`]]

            $setname = $modx->getOption('setname',$scriptProperties,'');
            return $setname;

            You can also take a look at the section Set Default Values of How to Write a Good Snippet on rtfm.

            [Update] you posted as I was writing. I'll leave this post on as it might help others with other mistakes in the call.