Yes. You are seeing things
"In contrast to a hallucination, a mirage is a real optical phenomenon which can be captured on camera. ... What the image appears to represent, however, is determined by the interpretive faculties of the human mind." - Wikipedia
Simplx Mirage is a simple Class library that helps you see and handle your MODX Templates and Resources as PHP Classes and Objects. Mirage is my 'optical illusion' to help you build web applications using only the very basic features of MODX.
Using the awesomeness of MODX
Since day one with our dear CMS I have seen Templates as modx version of data types, or Classes.
This is not only a working Metaphore but pretty much a practical reality as Templates in MODX do represent a logical entity with named fields. When a Resource chooses a Template it inherits these fields, extending its own set of fields. The Resource ends up representing the Template in much the same manner as an Object represents an instance of a Class.
The MODX API twist
The MODX API is plenty great for dealing with all aspects of the framework. This goes for the modResource class as well. The problem for me however, with my line of thinking, has been that the Template Variables that extend the Templates (and thus also the Resources) are very generic and volatile in their nature. By this l refer to the fact that one TV can be assigned to multiple Templates, and can also be removed from a Template at any time.
The whole thing is made even more problematic by the fact that a Resource can change template at anytime and loose its set of attributes, and thus also its signature. Summing up, we have all the stuff in MODX to turn it in to the Rapid Application Studio for the web, but with a slightly incompatible model.
Now lets put on our 3D goggles and see how Mirage lets us fix this!
Conjuring up the illusion
Ok. First of all I have made a Template which is to represent a product on my site. This Template has a bunch of Template Variables such as category, price and product image.
I have also made a container Resource called Product in the tree. This in turn is placed in a container called Objects. Like this,
Objects
- Product
Remember, when using Mirage, the Product Template with its Template Variables is considered to be a type declaration, or Class.
To create an Object of type Product, we now add a new Resource in our Product container and make sure to set the Template to Product. Lets say the new Resource is called 'Camera' gets the id 30.
Objects
- Product
-- Camera
Create a Snippet (for testing purposes) .
Be sure to include the simplx.mirage.php file.
Declare a php class like this:
class Product extends Simplx_Mirage_Object {}
We can now get the Product Object like this,
$camera = new Product(30);
Easy right? You can now thanks to the inner workings of Mirage use this object in plain old php fashion:
$camera->price = 45;
$camera->category = 'Cameras';
$camera->save();
And you can use the Simplx_Mirage class to do this:
$ProductClass = Simplx_Mirage::getClass('Product');
$cheapCameras = $ProductClass->getObjects(array( 'price:<'=>50, 'category:='=>'Cameras' ));
Thats all for now kids! Ill continue this article in a day or two :)
Have fun,
Lars
"In contrast to a hallucination, a mirage is a real optical phenomenon which can be captured on camera. ... What the image appears to represent, however, is determined by the interpretive faculties of the human mind." - Wikipedia
Simplx Mirage is a simple Class library that helps you see and handle your MODX Templates and Resources as PHP Classes and Objects. Mirage is my 'optical illusion' to help you build web applications using only the very basic features of MODX.
Using the awesomeness of MODX
Since day one with our dear CMS I have seen Templates as modx version of data types, or Classes.
This is not only a working Metaphore but pretty much a practical reality as Templates in MODX do represent a logical entity with named fields. When a Resource chooses a Template it inherits these fields, extending its own set of fields. The Resource ends up representing the Template in much the same manner as an Object represents an instance of a Class.
The MODX API twist
The MODX API is plenty great for dealing with all aspects of the framework. This goes for the modResource class as well. The problem for me however, with my line of thinking, has been that the Template Variables that extend the Templates (and thus also the Resources) are very generic and volatile in their nature. By this l refer to the fact that one TV can be assigned to multiple Templates, and can also be removed from a Template at any time.
The whole thing is made even more problematic by the fact that a Resource can change template at anytime and loose its set of attributes, and thus also its signature. Summing up, we have all the stuff in MODX to turn it in to the Rapid Application Studio for the web, but with a slightly incompatible model.
Now lets put on our 3D goggles and see how Mirage lets us fix this!
Conjuring up the illusion
Ok. First of all I have made a Template which is to represent a product on my site. This Template has a bunch of Template Variables such as category, price and product image.
I have also made a container Resource called Product in the tree. This in turn is placed in a container called Objects. Like this,
Objects
- Product
Remember, when using Mirage, the Product Template with its Template Variables is considered to be a type declaration, or Class.
To create an Object of type Product, we now add a new Resource in our Product container and make sure to set the Template to Product. Lets say the new Resource is called 'Camera' gets the id 30.
Objects
- Product
-- Camera
Create a Snippet (for testing purposes) .
Be sure to include the simplx.mirage.php file.
Declare a php class like this:
class Product extends Simplx_Mirage_Object {}
We can now get the Product Object like this,
$camera = new Product(30);
Easy right? You can now thanks to the inner workings of Mirage use this object in plain old php fashion:
$camera->price = 45;
$camera->category = 'Cameras';
$camera->save();
And you can use the Simplx_Mirage class to do this:
$ProductClass = Simplx_Mirage::getClass('Product');
$cheapCameras = $ProductClass->getObjects(array( 'price:<'=>50, 'category:='=>'Cameras' ));
Thats all for now kids! Ill continue this article in a day or two :)
Have fun,
Lars
This is looking very interesting, Lars.
SvaraRaderaThanks for all your entusiasm, i like to watch you work (via your tweets). It's like watching a very busy ant colony... :)
A question:
in order to use this, you'd have to create resources like in above structure Objects->Product->Camera?
So, if i had products in different places in my website structure, i could not just create them anywhere, but only inside the Object container?
Hi :)
SvaraRaderaThanks for taking the time! And thanks for asking such a good question ;)
In short, no you do not need any special folder structure. Your Cameras, in this case, could be anywhere in the MODx folder structure.
An optional exception to this is Mirages default assumption that aggregate and composite objects are placed in folders which are named exactly as the Class / Template they use. More on this later :)
Ciao
Lars