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

    If no template is defined for getResource the output is a string not an array.

    $events = $modx->runSnippet(
    	'getResources',
    	array(  
    		'parents' => '2',
    		'hideContainers' => '1',
    		'depth' => '3',
    		'includeTVs' => '1',
    		'includeContent' => '1',
    		'limit' => '0'
    	)
    );
    
    echo $events;


    will output

    string(10905) "
    Array
    (
        [tpl] => 
        [tplOdd] => 
        [tplFirst] => 
        [tplLast] => 
        [sortby] => publishedon
        [sortbyAlias] => 
        [sortbyEscaped] => 0
        [sortdir] => DESC
        [limit] => 0
        [offset] => 0
        [tvFilters] => 
        [depth] => 3
        [parents] => 2
        [includeContent] => 1 .......


    I need to use the output as an array, loop through the results, make some changes and then output the result. Since the output is a string I think I need to do a reverse print_r.

    Is there a way to avoid the reverse print_r by making getResources output an array?

    Thank you
      • 22303 MODX Staff
      • 10,725 Posts
      Snippets cannot return anything except a string. What you want to do is use one Snippet to populate a placeholder containing your array, then use a second Snippet to iterate over that array retrieved with $modx->getPlaceholder(). I use this technique a lot, especially if I need to iterate over a collection of objects multiple times (e.g. to create anchor links to items listed in the main content).
        • 5340
        • 1,624 Posts
        Quote from: OpenGeek at Apr 14, 2010, 04:10 PM

        populate a placeholder containing your array

        You lost me here. smiley I’ll have a look at the Revo placeholders and see what I can find.

        If this doesn’t work I’m thinking of making my own snippet based on getResources. It might be simpler. Duplicate getResources, remove unwanted parameters and output what I need.

        Thank you
          • 27708 MODX Staff
          • 2,502 Posts
          What are you trying to achieve here? It looks to me you care calling get resources in another snippet/custom snippet. It may make sense just to write a custom snippet to do what you need with the data without getResources, manipulate the data and output the data as needed. Maybe if we had a picture of your intended outcome we could helpf further.
            Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
            • 5340
            • 1,624 Posts
            So,

            I am building a calendar with events. getResources is enough to get all the regular events but I also have weekly events.

            Weekly events are defined by a TV that has a number value.

            For example:

            Title: Event 1
            Start Date: 20 Apr 2010
            Weekly Recurrence Number: 5

            That means that from one event I have to create another 4 events with 4 different start dates. The start dates will be calculated by adding 7 days to the initial start date.

            I think in Evo this was possible, because I could use Ditto to output my initial array, than parse the array, look for a weekly number and add the weekly events in another "for" loop.

            I hope it makes sense.

            Thank you
              • 22303 MODX Staff
              • 10,725 Posts
              I won’t get into this too much, I’ll just say representing events as individual Resources is IMO, not the proper solution. Resources in MODx to me are like Views; Events are data that can be presented in many different ways or used for web services, or etc. You limit their usefulness and reusability by making them Resources.
                • 5340
                • 1,624 Posts
                I agree with you but I don’t have another option right now.

                The thing is that 25% of the events will have content, the rest will be just external links. Even more they have certain properties like Region, Type etc

                I don’t plan to reuse them for now. This is version 1 so I need to make something like a proof of concept and get it going. I’ll refine it in version 2.

                In the future I might export it to a custom table and do a module.

                I also want to say that I made my own code based on getResources and works really well.

                Thx again for your input.