Magento has a comprehensive template engine where those who are more familiar with flat PHP files or a more traditional MVC framework may have a little trouble getting a good handle on. This Mini Tutorial will walk you through a progression of what you can expect when traditional PHP and MVC framework gets applied to Magento. I will briefly go over pseudo-code examples of the concepts that exist along side Magento, followed by an example of the Magento way of extending a pre-existing page using HTML with Magento’s form of MVC, and Magento’s form of templating.
In a flat PHP file based system, your display code and business logic are all contained in the same file. This isn’t a very optimal solution as it makes maintenance and refactoring very difficult. It’s also not flexible for working within a team since the components aren’t decoupled for easy editing and may cause merging conflicts when using source code versioning systems.
<!-- Your mixed HTML+PHP page script (pseudo-code) -->
<?phpmysql_connect(/* database details */) ?>
<?php$result=mysql_query(/* get current page from db */) ?>
In a larger application, this would be very messy and very hard to maintain.
In a MVC framework, the business logic is put into models and delegated to display components called templates, or view scripts, by the controller. Routing, business logic, and display code are decoupled into their own separate components. The benefits are that when you need to change some business logic, you do not need to look through a spaghetti mess of mixed HTML and PHP.
Example MVC controller class and model class
<?php// Your Post Model Class (pseudo-code)
// model specific code here
<?php// Your Index Controller Class (pseudo-code)
// call on Post model to get by ID
// expost post to view script
Example MVC controller action template
<!-- Your controller action template (pseudo-code) -->
We’re getting to a better pattern overall that allows us to decouple our code and encapsulate functionality but we’re not quite there yet with what Magento has in store.
Magento goes one step further in helping developers organize and maintain their code. In Magento, you have all the benefits described above. Furthermore, you can encapsulate class code to your display code in order to bind helpers that are specific to that embeddable template.
To demonstrate, I will guide you through the process of building a block that we can use to embed into our product page. You will need to create the following files and directories.
NAMESPACE: is going to the namespace you create your Twitter module in.
PACKAGE is the current theme package enabled on your Magento instance.
The big benefit to Magento’s design is that this block level code is not exposed to anything but timeline.phtml (embeded later) template file that is defined in local.xml below. The code for timeline.phtml is completely encapulsated in this class leaving all other variables, methods, and classes untouched.
Next, you can add this to your app/design/frontend/enterprise/PACKAGE/layout/local.xml: