Deutsche Version medianotions.de subscribe RSS feed

10.11.2008

MODx-Feature: MODx API Cheat Sheet

Tags: MODx, Snippets | last edited: 23.03.2011


Admittedly, there are better documentations than the current MODx one. Fortunately, you don’t have to refer to it too much once you have figured out how MODx works generally. The exception to the rule is the MODx-API, and for that reason I have - step by step - created a MODx-API cheat sheet in which the most important functions are listed:

MODx Evolution API cheat sheet

<? 
   // Get and set placeholders
   $var=$modx->getPlaceholder('placeholder_name');
   $modx->setPlaceholder('placeholder_name', $var);

   // Print placeholders in HTML
   ?>
      [+placeholder_name+]
   <?

   // Run snippet
   $params['test']=1;
   $params['dummy']=2;
   $snippet=$modx->runSnippet('snippet_name', $params);

   // Run snippet in HTML
   ?>
      [[snippet_name? &test=`1` &dummy=`2`]]
   <?

   // Get chunk
   $chunk=$modx->getChunk('chunk_name');

   // Print chunk in HTML
   ?>
      {{chunk_name}}
   <?

   // Parse chunks
   $params['test']=1;
   $params['dummy']=2;
   $chunk=$modx->parseChunk('chunk_name', $params, '[+', '+]');

   // Get MODx documents
   $id=$modx->documentIdentifier;
   $document=$modx->getParent($id);
   $document=$modx->getDocument($id);
   $document=$modx->getDocumentChildren($id);
   $document=$modx->getActiveChildren($id, 'menuindex', 'ASC');

   // MODx document array
   $id=$document['id'];
   $pagetitle=$document['pagetitle'];
   $longtitle=$document['longtitle'];
   $description=$document['description'];
   $alias=$document['alias'];
   …

   // Get the value of template variables
   $document_tvs=$modx->getTemplateVarOutput(true, $id);
   $templatevar_output=$document_tvs['template_variable_name'];

   // Print template variables in HTML
   ?>
      [*template_variable_name*]
   <?

   // Make URL
   $url=$modx->makeUrl(intval($id));

   // Make URL in HTML
   ?>
      [~id~]
   <?

   // Send Redirect to the Browser
   $count_attempts=1;
   $modx->sendRedirect('http://www.newdomain.com', $count_attempts, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');

   // Forward to a new MODx document without reloading the page
   $modx->sendForward($id);

   // get MODx configuration
   $site_name=$modx->config['site_name'];
   $site_url=$modx->config['site_url'];
   $site_start=$modx->config['site_start'];
   $error_page=$modx->config['error_page'];
   $base_path=$modx->config['base_path'];
   $base_url=$modx->config['base_url'];
   $rb_base_dir=$modx->config['rb_base_dir'];
   $rb_base_url=$modx->config['rb_base_url'];   
   $modx_charset=$modx->config['modx_charset'];
   …

   // Get MODx configuration in HTML
   ?>
      [(site_name)]
      [(site_url)]
      …
   <?

   // Handle MODx events
   $event=$modx->Event; 
   switch($event->name)
   { 
      case "OnBeforeDocFormSave":
         …
      break;
   }

   // TV @SELECT
   @SELECT pagetitle FROM modx_site_content WHERE parent = 329 ORDER BY pagetitle 
?>

According to my experience, these API functions are the most commonly used code snippets when developing your own snippets or modules.

Background: MODx-API

With the help of MODx-API, MODx can be extended quickly and comfortably by your own functions. More on this can be found in the article “MODx-Feature: Snippets – all rounders”.

I am looking forward to suggestions on how to extend the MODx-API cheat sheet!

Downloads


Bookmark or recomend this page

Write a comment


14 Comments

Sydney Church visit website

08.12.2011 19:51:24

Thank you for sharing the compilation.
This is what I need =)

Bogdan visit website

04.08.2010 09:45:07

Hi Dan,

I am glad you find my cheatsheet useful!

Regarding your question: I am afraid I can’t help you at all with this. I switched to jQuery some time ago and I never was very deep into MooTools or the MODx Manager.

I would suggest to ask the official MODx developer team what it would take to change the Manager interface. My guess is that its a LOT of work, but it would be nice to have different interfaces to choose from though.

Good luck!

dan

03.08.2010 17:32:37

Nice modx cheatsheet Bogden! very handy.

Q: Bogden, you seem to have a knowledge of mootools + modx — would you know how swap out the revolution 2.0 manager for the mootools mocha ui interface? I’d love to be able to have mocha as the modx backend, instead of the standard revo manager module.

Any ideas?

http://mochaui.org
http://mochaui.org/demo/

Jarkko visit website

18.02.2010 12:05:35

Thanks. Really very useful info!

Milos visit website

30.09.2009 12:31:06

Dear Bogdan,

How do you embed php code into modx pages and syntax highligt like that?

If you visit my site, you’ll see how I did it, but I like your way better.

Any instructions? Thanks

Bogdan visit website

21.08.2009 13:37:00

Hello Patrick,

I am glad the API Cheat sheet is useful to you. Spread the word if you like MODx!

Patrick

21.08.2009 13:18:37

Thank you for this. I’m a n00b to Modx but I am a php professional. Just getting the API objects so handy like this is great. Thanks again.

Bogdan visit website

23.04.2009 19:39:32

Thanks for the suggestion. This cheat sheet is for developers using the MODx API indeed. However there are quite a few tutorials for MODx beginners, though a tutorial is not the same as a cheat sheet of course.

Best,
Bogdan

learn to play craps visit website

23.04.2009 14:14:41

I think ideally there should be 2 cheat sheets (as if you haven’t got enough to do already Grin): one for developers with API stuff etc, and one for the actual users of the website (our clients) with all the basics; how to make a new page, add images, stuff like that.

Bogdan visit website

01.02.2009 17:31:28

Just added the examples the associative arrays MODx document arrays.

Bogdan visit website

27.01.2009 09:08:27

Thanks for your feedback Shane! You are right, I forgot the document arrays as I take it as granted to know how to use them. I will update the cheat sheet in the next few days.

Shane Sponagle visit website

26.01.2009 01:02:22

Bogdan: This is a great simple cheat sheet. Bookmarked for reference. Cool site as well. I look forward to more stuff on MODx.

I think that a nice thing to include in the cheat sheet would be examples of how to work with the associative arrays that the api returns. I see many people stumped with how to work with the results.

Bogdan visit website

14.12.2008 16:48:32

Thanks Zaigham. Never thought about offering a printable Version since its not like a Photoshop shortcuts cheat sheet you can use just by looking at it. The MODx-API cheat sheet you certainly want to copy/paste the lines you need, isn’t it?

Zaigham visit website

12.12.2008 21:20:31

Very nice work Bogdan.

Maybe a downloadable pdf or png version of the same would be helpful for those who like to print the sheets and use them physically.