We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9496
    • 113 Posts
    I am using ditto to generate a JSON data file that I am using with some JS.

    See the timer here: http://s51662.gridserver.com for an example. In essence its associating the time with a specific name and a quote. The reason I am trying to tie this all to MODX (versus just writing the data file) is I would like the client to be able to add quotes and data as needed.

    Now like I said I can hand code a json file whose schema looks like this:

    {"names":[
    	{"firstname":"Josie","quote":"This is a quote."},
    	{"firstname":"Karla","quote":"Hello World!"},
    	{"firstname":"Michelle","quote":"This is another quote"}
    ]}
    


    I have a page set up called quotes.json, and it is set it to plain/text. Within it is the ditto call:

    [!Ditto? &parents=`89` &format=`json`!]

    Each entry is set up as a child within that container.

    The file response returns this:

    {
     "title":"quotes",
     "link":"http://s51662.gridserver.com/quotes.json.htm",
     "description":"",
     "language":"en",
     "copyright":"Domestic Violence Center of Greater Cleveland 2006",
     "ttl":"120",
     "entries":[
    	{
    	 "title":"Janie",
    	 "link":"http://s51662.gridserver.com/quotes.json/untitled-document.htm",
    	 "date":"1223045352",
    	 "guid":"http://s51662.gridserver.com/quotes.json/untitled-document.htm",
    	 "author":"Default admin account",
    	 "description":"",
    	 "introtext":"",
    	},
    	{
    	 "title":"josie",
    	 "link":"http://s51662.gridserver.com/quotes.json/josie.htm",
    	 "date":"1223042706",
    	 "guid":"http://s51662.gridserver.com/quotes.json/josie.htm",
    	 "author":"Default admin account",
    	 "description":"",
    	 "introtext":"",
    	}
     ]
    }


    it is leaving out the "content" [+content=] (which in this case would be a quote.
    which I can get to work through modifying my mootools JS request.

    But is there a way to format it either via a template file to only output say the title and content (something similar to that original one I started with? Or shold I build it as a plain text file by eliminating the &format=`json` parameter entirely and use a template chunk instead?

    Thanks for any insight
    P

      • 9496
      • 113 Posts
      I looked into it further and it looks like I can edit those the json.format.inc.php file. Is that method kosher, or can I, somewhere in the ditto call itself require an alternate config file? Instead of overwriting the default config file.

      Something like...

      [!Ditto? &parents=`89` &format=`json` &config=`quotes.json.format.inc.php` !]

      Or am I making this wayyyy to complicated as usual....

      Thanks-
      P
        • 10449
        • 956 Posts
          • 9496
          • 113 Posts
          Thanks. So I did this:

          on my quote-data.json page I have this:

           {"names":[
          [!Ditto? &parents=`89` &config=`quotes.json`!]
          ]}
          


          I set up the config file to this (which I think isn’t the right way to go about it)

          <?php
          
          /*
           * Title: JSON
           * Purpose:
           *  	Collection of parameters, functions, and classes that expand
           *  	Ditto's output capabilities to include JSON
          */
          
          // set json placeholders
          $json_placeholders['json_lang'] = (isset($abbrLanguage))? $abbrLanguage : $_lang['abbr_lang'];
          /*
          	Param: abbrLanguage
          
          	Purpose:
          	Language for the JSON document
          
          	Options:
          	Any valid 2 character language abbreviation
          
          	Default:
          	[LANG]
          
          	Related:
          	- <language>
          */
          $json_placeholders['json_copyright'] = isset($copyright) ? $copyright: $_lang['default_copyright'];
          /*
          	Param: copyright
          
          	Purpose:
          	Copyright message to embed in the JSON document
          
          	Options:
          	Any text
          
          	Default:
          	[LANG]
          */
          $json_placeholders['json_link'] = $modx->config['site_url']."[~".$modx->documentObject['id']."~]";
          $json_placeholders['json_ttl'] = isset($ttl) ? intval($ttl):120;
          /*
          	Param: ttl
          
          	Purpose:
          	Time to live for the JSON feed
          
          	Options:
          	Any integer greater than 1
          
          	Default:
          	120
          */
          $json_placeholders['json_op'] = (!empty($_REQUEST[$dittoID.'jsonp']) ? $_REQUEST[$dittoID.'jsonp'] : '');
          
          // set tpl JSON placeholders
          $placeholders['*'] = "json_parameters";
          if(!function_exists("json_parameters")) { 
          	function json_parameters($placeholders) {
          		$jsonArr = array();
          		foreach ($placeholders as $name=>$value) {
          			$jsonArr["json_".$name] = addslashes($value);
          		}
          		$placeholders = array_merge($jsonArr,$placeholders);
          		return $placeholders;	
          	}
          }
          // ---------------------------------------------------
          // JSON Templates
          // ---------------------------------------------------
          
          $json_header = '
          [+json_op+]{"names":[
          ';
          	// not heredoc because { cannont be used as a char in heredoc
          
          $json_tpl = <<<TPL
          	{"author":"[+json_pagetitle+]","content":"[+content+]"},
          
          TPL;
          
          $json_tpl_last = <<<TPL
          	{"author":"[+json_pagetitle+]","content":"[+content+]"}
          TPL;
          
          $json_footer = <<<TPL
          
           ]
          }
          TPL;
          
          // ---------------------------------------------------
          // Pass JSON Templates To Snippet
          // ---------------------------------------------------
          
          $header = isset($header) ? $header : template::replace($json_placeholders,$json_header);
          
          $tpl = isset($tpl) ? $tpl : "@CODE:".$json_tpl;
          
          $tplLast = isset($tplLast) ? $tplLast : "@CODE:".$json_tpl_last;
          
          $footer = isset($footer) ? $footer : $json_footer;
          
          // set emptytext
          $noResults = "      ";
          
          ?>
          
          


          Which is a direct copy of the json.format.inc.php file in the formats folder with the template variable switched to where I need it.

          That returns the properly formatted json response but for some reason it doesn’t seem appropriate, I’m guessing there is an easier way to get the specific key value pairs through the ditto call itself but I am just not getting it.


            • 10449
            • 956 Posts
            I didn’t read the whole question properly... I think you could just do this with a custom template chunk.

            chunk jsonQuotes:
            {"firstname":"[+pagetitle+]","quote":"[+content+]"},
            


            chunk jsonQuotesLastRow: (without comma at the end)
            {"firstname":"[+pagetitle+]","quote":"[+content+]"}
            



            The document with the Ditto-call would be something like:
            {"names":[
            [!Ditto? &parents=`89` &tpl=`jsonQuotes` &tplLastRow=`jsonQuotesLastRow`!]
            ]}

              • 9496
              • 113 Posts
              Boom. Thanks. Thats exactly what I was looking for. A simple ditto call without having to mess about in the config or format files.

              The only thing though is that instead of &tplLastRow to modify the the last row entry, it should be &tplLast="name_of_chunk_here". I suppose if I wanted to I could use &tplHeader if I wanted to mess with the header (in this instance though its not necessary.)

              So to clarify, within the ditto call to adjust the format the params to use are &tpl=`your template chunk`, &tplLast=`your last row template chunk`, and &tplHeader=`header template chunk`".

              Out of curiosity why would I ever need to use the &format=`json` parameter?

              Thats where my confusion was coming (config format or template? from as far as formatting everything. With all that extensibility its sometimes hard to know where to start and how everything fits together (Im slowly getting it though)
              thanks again-
              P
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                It should create a json feed, using the assets/snippets/ditto/formats/json.format.inc.php file.
                  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
                  • 9496
                  • 113 Posts
                  Thanks. So using the &format=`json` simply uses that default data formatting template, and for more control over the response data though, the method described by ganesh works as well. I just wanted to make sure that I wasn’t missing something.

                  -p