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

    is there any way to change the output format of getResources?


    As far as I know it was possible to make it for Ditto in evo when setting a call with &format =`json`

    Or Should I install Ditto for Revo and work with the same parameters as in Evo?



    Thanks
      modx and ecommerce pro
    • You can use the tpl’s to output whatever format you want to, HTML, XML, JSON, etc. Just be careful with JSON since some of the output can mimic MODX tags if spaces are not used carefully.
      • Hi

        I am afraid this is a bit confusing way but probably will help, thanks.

        HOwever there is another one: http://modx.com/extras/package/simplxwidgeteer

        but I have no idea whow to use it... Looking for readme.. Got readme for revo anyone?
          modx and ecommerce pro
        • Having no luck with http://modx.com/extras/package/simplxwidgeteer

          have created tpls for GR as the OpenGeek kindly recommended ;-)
          {
          "title":"[[*pagetitle]]",
          "link":"[[~[[*id]]]]",
          "description":"[[*description]]",
          "language":"en",
          "copyright":"",
          "ttl":"",
          "entries":[
          [[!getResources:default=``?
          &parents=`[[!switch?&get=`[[*context_key]]`&c1=`web`&do1=`22`&c2=`ned`&do2=`21`&c3=`rus`&do3=`20`&c4=`estonian`&do4=`19`&default=`22`]]`
          &tpl=`tpl.JSON.Data`
          &tplLast=`tpl.JSON.Data.last`
          &depth=`2`
          &hideContainers=`1`
          &context=`[[!switch?&get=`[[*context_key]]`&c1=`web`&do1=`web`&c2=`ned`&do2=`ned`&c3=`rus`&do3=`rus`&c4=`estonian`&do4=`estonian`&default=`web`]]`
          ]]
          ]
          }

          where tpl.JSON.Data

          is the chunk CODE:
          {
          "title":"[[+pagetitle]]",
          "link":"[[~[[+id]]]]",
          "date":"[[+pub_date]]",
          "cout":"[[+unpub_date]]",
          "guid":"[[+id]]",
          "author":"",
          "description":"[[+description]]",
          "introtext":"[[+introtext]]",
          },

          ======================================================================
          tpl.JSON.Data.last is the last tpl chunk (which does not have a comma on the tail) CODE:
          {
          "title":"[[+pagetitle]]",
          "link":"[[~[[+id]]]]",
          "date":"[[+pub_date]]",
          "cout":"[[+unpub_date]]",
          "guid":"[[+id]]",
          "author":"",
          "description":"[[+description]]",
          "introtext":"[[+introtext]]",
          }
          =========================================================================

          So this returns resources in JSON format (I believe) ;-) and my JSON class can happily read it!

          Hope it helps someone, cheers
            modx and ecommerce pro
            • 13490
            • 5 Posts
            Thanks for posting this, set me on a good path.

            Instead of using tplLast with a comma’less chunk, you can use;
            &outputSeparator=`,`


            You probably should also escape json/javascript characters from the placeholder values.

            Here’s my full solution to the same situation (Mutliple Resources -> Ditto -> Template -> "JSON" resource -> JSON client (sencha-touch in my case.).

            Multiple Resources
            Just a collection of resources ("Artists") all using the same Template with various TVs, nothing special.

            Ditto
            [[getResources? 
               &parents=`3`
               &includeContent=`1`
               &includeTVs=`1`
               &processTVs=`1`
               &tvPrefix=``
               &tpl=`ArtistStoreItem`
               &outputSeparator=`,`
            ]]
            


            Template - {{ArtistStoreItem}} chunk
                   {
                   	   ... some stuff removed for confidentiality ...
            	   "id": "[[+id]]",
            	   "artistName": "[[+pagetitle]]",
                       "artistDescription": [[+content:tojson]]
                   }
            

            Notice the "tojson" modifier, escaping invalid characters, like comma, tabs, newlines, etc.

            :tojson custom modifier
            I searched through docs but couldn’t find mention of a "toJSON" modifier in the core.
            So I made this real basic snippet called "tojson";
            //$output = $modx->toJSON($input);
            $output = json_encode($input);
            return $output;
            

            I thought that toJSON($mixed) was a member of $modx, but I guess it’s only on xPDO?
            No big deal though since json_encode($mixed) works fine (I think this requires PHP 5.2+, and I think newline chars have only been escaped in more recent versions?).

            "JSON" resource - ~/stores/artists.json
            Just a basic resource to serve out the JSON, but with a nice content type;
            {
                "artists": [
            
            [[getResources? 
               &parents=`3`
               &includeContent=`1`
               &includeTVs=`1`
               &processTVs=`1`
               &tvPrefix=``
               &tpl=`ArtistStoreItem`
               &outputSeparator=`,`
            ]]
            
                ]
            }
            


            Page Settings look like;

            • Searchable: NO
            • Cacheable: YES
            • Content Type: JSON
            • Content Disposition: Inline
            • Class Key: modDocument

            JSON Content Type
            Created in System -> Content Types;

            • Name: JSON
            • MIME Type: application/json
            • Extension: .json

            This is our first use of Revolution and it’s great, thanks to the team for getting it so polished!

            Cheers
            James
            • The modX class extends xPDO, so you have all the methods of xPDO available on $modx, including toJSON() and fromJSON().
                • 40452
                • 10 Posts
                Thanks James! That really helped me as it was exactly what I wanted to do.

                Thanks
                [ed. note: rohan.r last edited this post 11 years, 8 months ago.]
                  • 48693
                  • 7 Posts
                  Having troubles getting Template Variables to output even with the following settings:

                  &includeContent=`1`
                  &includeTVs=`1`
                  &processTVs=`1`
                  &showHidden=`1`
                  &tvPrefix=``

                  My TPL chunk is following the tv.TVname convention:

                  {
                  "id":"[[+id]]",
                  "product":"[[+pagetitle]]",
                  "description":[[+tv.ProductDescription:tojson]],
                  "sku":"[[+tv.SKU]]",
                  "uri":"[[+uri]]",
                  "url":"[[++site_url]][[+uri]]",
                  "oneDay":"[[+tv.Price1]]",
                  "sevenDays":"[[+tv.Price7]]",
                  "fourteenDays":"[[+tv.Price14]]",
                  "twentyOneDays":"[[+tv.Price21]]",
                  "thirtyDays":"[[+tv.Price30]]",
                  "img":"[[+tv.ProductImage]]",
                  "quantity":"[[+tv.Quantity]]",
                  "category":"[[+tv.Category]]"
                  }
                    • 3749
                    • 24,544 Posts
                    Have you tried leaving out the &prefix property? You appear to be using the default .tv prefix.
                      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