Hi all,
I want to discuss a snippet concept I developed in course of time and get some feedback from the experienced MODx developers.
Background
From the beginning of using MODx I didn’t like two things: that everything is "scattered around" many places (documents, snippets, chunks, plugins etc.) and that I had to work mostly in form textareas instead of my beloved TextMate.
Solution 1: Outsourcing
So first of all I started to outsource templates and snippets by including them from a file:
include('assets/site/snippets/metaSnippet.php');
In this way it was possible again to edit MODx code in Textmate by double clicking a specific file in Transmit ... great!
Solution 2: Making Collections
Still I had the problem, that I hat a collection of often used snippets (most of them just a few lines of length and nothing complicated) that where separately located in the snippet area. So I had the idea to collect them all in a snippet called "metaSnippet". Example:
[[metaSnippet? &name=`getLastEditedDate`]]
In the metaSnippet I have a PHP switch construction for the different snippets:
switch($name)
{
...
case "getLastEditedDate":
$secs=$modx->documentObject['editedon'];
$dateStr=date("Y-m-d, H:i:s (T)", $secs);
echo($dateStr);
break;
...
}
In this way I have everything in one place, which I find very handy. Of course for more complex and big snippets I still create individual snippets in MODx.
I took this concept further for templates too: I am including the template and pass also some parameters down to it:
[[metaSnippet? &name=`loadTemplate` &template=`screen_default.php` &headChunk=`` &bodyTagParams=`` &contentTopChunk=`` &contentBottomChunk=``]]
In this way I don’t have to create different copies of the default template if the difference is just one or two lines (like loading an additional style or script).
I never discussed this concepts of outsourcing and snippet collecting with experienced MODx developers. It works nicely for me and actually I am quite happy with it. But now I just started wondering myself if this ideas are great or there are better and more elegant ways of achieving the same. I am curious for your feedback!
Thanks
Bogdan