Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

Content-Rich Scripts in Magento

Welcome to the first post of this blog series, aptly named Tim's Tutorials, by me Tim.

In a recent project, we were tasked with building an array of websites that were to be built primarily out of CMS static blocks and pages. The method that we decided to use was very helpful for creating and filling the CMS content with html that was already supplied. So in this post I would like to share this useful method with you.

 


Let’s begin with our folder structure:

1.     Create the folder structure for your module and register it using the Magento XML system. 

In your namespace (app->code->local->[yournamespace]) create a folder called “General”.

NOTE:Throughout this post I will be using the namespace “Invent”. Also the module I create will be called “General”, but you can choose any name you like to call your module. Just replace any occurrence of “Invent” with “yournamespace” and any occurrence of “General” with “yourmodulename” – keeping in line with the casing.

Inside this folder create 3 more folders. Name them “data”, “etc”, “Model”.

Inside “data”, create 2 more folders. One named “invent_general_setup” and the other “Content”. The “invent_general_setup” folder is where we are going to         create our actual upgrade scripts and the “Content” folder will be where our .txt files with the html content will be placed.

Inside the “etc” folder we are going to create our xml file, naming it, “config.xml”

"config.xml" should look something like this:

           

Online 4– 6 we register our module and give it a version number. This version number is very important in the upgrade process and I will be coming back to it       later. There is however, one more step that needs to take place before we can start using our module for upgrade scripts.

Navigate to app->etc->modules in your file structure and create another XML file called Invent_General.xml. This file should contain the following: 

    

         

Now  our file structure is almost complete!

All we have left to do is create another folder inside the “Model” folder, named “Content”. This “Content” folder is where we will place our PHP files which specify              whether we are creating a block or page and also where we assign the content a store code.

We end up with a file structure that looks like the following:

           

2.   Create an upgrade script file in the “invent_general_setup” folder and create an Abstract.php file for all methods.

Something important to note here is that the file(s) you create must follow a strict naming convention.

Our first file MUST be called “data-install-0.1.1.php” (The number in the name MUST correspond with the number in between the <version></version> tags in your config.xml).

 “data-install-0.1.1.php” would look something like this:

              

 The reason we use the Mage::log function is so that if our upgrade scripts produce any errors, we can navigate to “var/log” in our folder structure and see exactly what the errors in the log files are. This will come in handy when doing multiple blocks and pages in one script. We will now create the “Abstract.php” file in “Invent->General->Model->Content”.

This file should contain the following:      

             

The Abstract.php file acts as a base for all our functions and this file will be extended from later on in this process.

Now login to your admin panel and refresh the page, or refresh the frontend of your website and the new module should be installed. In order to make 100% sure, at this point I always take a look in the database at the “core_resource” table. If invent_general_setup is visible with the version number of 0.1.1, we know it has taken effect. We can now start creating dynamic cms content!

 3.   Create a .txt file in “Invent->General->data->Content” which will contain all your html code.

We will now create a .txt file in “Invent->General->data->Content”. I usually like to give this text file the same name as I give to the actual static block being created to make the process easier. For the purpose of this tutorial, I am going to call our .txt file “our-first-static-block.txt”.

You can input any html code in this text file. Let’s put something simple in this one:

          

Now that we have “our-first-static-block.txt” file, we can move on to the more interesting side of this process.

 

 4.     Create a PHP file mirroring the name of your text file (without the hyphens).

We will now create a PHP file in “Invent->General->Model->Content”. This file should mirror the name of the .txt file we have just created for ease of use, by just removing the hyphens. So we are going to create a file named “ourfirststaticblock.php” which will look like this:

                        

NOTE:The comments describe what each lines function is.

Notice how our PHP class extends the Abstract.php we created earlier. The getContent() function is what we will be calling in our actual upgrade script. For $type we can also use “cms/page” if we wanted to create a page.

 

5.     Create an upgrade script in the “invent_general_setup” (Invent->General->data->invent_general_setup) folder which increments the version number.

We will now create our upgrade script which increments the version number of our initial install script. We will call our upgrade script “data-upgrade-0.1.1-0.1.2.php”.

The file will look like this:

                            

 Notice how we use the Mage::log command to log any errors and to see that the installer has worked as expected. We call our model with the Mage::getModel() method and get the content with the getContent() method we created in our PHP file.

The last step is to increment our version number within the <version></version> tags in the config.xml. Our version number should now be set as “0.1.2”.

Save your config.xml and refresh the page on either the frontend or backend and your static block should have been created! Have a look at the “core_resource” table in your database to see whether the version number has increased. If not, browse the log files (in var/log).

Keep in mind that any html can be input into the txt files. We can also reference other static blocks and/or pages, as well as images and other content that you might want to put in your block/page.

And there you have it! Content rich upgrade scripts in Magento. For any questions or queries please leave comments below and I will get back to you.

In addition, merchants, for more information on how our development team can help your eCommerce site contact us now on +44 (0) 20 3011 5000.

Stay tuned for my next tutorial in early 2014!