Using PHP code on a CMS page (custom Block/module)
Posted on 02. Sep, 2008 by Fido in CMS, Magento, Module
If you have ever wanted to do some sort of PHP processing within a CMS page that you created, here is how you do it.
A limitation of this tutorials is form processing. That involves creating and/or extending and/or overriding a controller class (which I’ll save for another day).
This code will allow your code to accept arguments, making it handy for some sort of dynamic use!
These are the basic instructions to create any custom module as well!
Off we go:
1) 1. declare your module: app/etc/modules/MyCompany_All.xml
<?xml version="1.0"?> <config> <modules> <MyCompany_Custom> <active>true</active> <codePool>local</codePool> </MyCompany_Custom> </modules> </config>
2. create module config: app/code/local/MyCompany/Custom/etc/config.xml
<?xml version="1.0"?> <config> <global> <blocks> <mycompany_custom> <class>MyCompany_Custom_Block</class> </mycompany_custom> </blocks> </global> </config>
3. Create your custom PHP code: app/code/local/MyCompany/Custom/Block/Test.php
<?php
class MyCompany_Custom_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
// put here your custom PHP code with output in $html;
// use arguments like $this->getMyParam1() , $this->getAnotherParam()
return $html;
}
}
4. Use your custom PHP logic in CMS page/block:
{{block type="mycompany_custom/test" my_param1="value 1" another_param="value 2"}}
Some notes on use:
Anything returned in the _toHtml() method will be outputted to the browser (no need to use any $this->getChildHtml() or anything).
Also, there’s no template (.phtml file) associated with this method, although you can certainly use one! (And create your own methods to use within that template).
Additionally, I found that if you override the __construct() method, you cannot retrieve your custom parameters from within it (presumable because the __construct method is always the first run and the class has yet to retrieve the parameters passed).




24 Comments
Johannes
28. Oct, 2008
There’s an error in the config.xml
line 6. mycompany_Custom_Block
The classname is key sensitive.
i changed it to
MyCompany_Custom_Blocklike the classname in Test.php
-> class MyCompany_Custom_Block…
then it works for me
wilson
25. Nov, 2008
Hi thanks for posting this, just waht we needed. How would we do 2 or more on one cms page?
Thanks.
Fido
25. Nov, 2008
Johannes – Thanks for catching that! I believe I copied and pasted from a working module, but I can’t say at the moment
Wilson – You can keep creating modules all day long and add them all in a CMS page via snippets such as this:
{{block type=”mycompany_custom/test” my_param1=”value 1″ another_param=”value 2″}}
(Just keep using those within on CMS page).
You can also gain a little more control using the Layout Update XML within the CMS page.
macfred
26. Nov, 2008
Hey,
Nice piece of code. I have trouble to have the customer name, how can i do it without using $this or toString()? Here is my code :
getCustomer()->toString(’{{firstname}} {{lastname}}{{email}}’);
return $html;
}
}
?>
macfred
26. Nov, 2008
ok, I figured it out
Thanks for that module!
Stephan
11. Feb, 2009
Hi
first thanks for this, great work!
But if I use this one in the layout configuration it doesn’t work!
i tried this in the layout area
instead of this in the html-area
{{block type=”company_example/view” message=”hello” template=”example/view.phtml”}}
both show up, but at the first call the message parameter is empty!
Whats the Problem?
Sailcomp
20. May, 2009
How can I use Sessions or Cookies?
Д
22. May, 2009
Хм… Как раз на эту тему думал, а тут такой пост шикарный, спасибо!
mike.bowen
25. Jun, 2009
Fido…
You going to extend this to form processing etc. anytime in the near future?
I’d love to hear about this.
David
17. Jul, 2009
Hello.
I have a small PHP and JS code that checks and sets some coockies as soon as a new visitor comes to our website. These coockies determine which website to direct the visitor to
http://www.domain.com
or
http://www.domain.com/international
and than it will remember to always direct him to the same ‘domain’
So based on this tutorial I thought I need to do this as a module and place the block code on the app/design/………/layout/page.xml
but, ofcourse since I’m not sure of what I’m doing I get the following error:
Fatal error: Call to a member function toHtml() on a non-object in C:\AppServ\www\app\code\core\Mage\Core\Block\Abstract.php on line 503
Got ideas?
Here is the code: http://www.magentocommerce.com/boards/viewreply/155785/
Tom
10. Aug, 2009
If you don’t want to create a module, the most simplest way is create an phtml file, and in your CMS page, call block {{block type=”core/template” param1=”value1″ param2=”value2″ template=”your_folder/your_file.phtml”}}
In your_file.phtml, you can do whatever you want
Ahsan
04. Sep, 2009
nice article!
@Tom Wao! will b great if it works …
trying to implement Tom’s way …
rizwan
11. Sep, 2009
thanks but there is another problem that how can i show my products images in a customize way in category page ……
Kyle
24. Nov, 2009
Tom,
Thank you. Thank you Thank you. I love the simple solutions. Worked perfectly. Been beating my head on this for a couple of hours now.
Oleksii
11. Dec, 2009
{{block type=”mycompany_custom/test” …}} my_param1=”value 1″ another_param=”value 2″}}
How i understand, type=”mycompany_custom/test” initiate call to class MyCompany_Custom_Block_Test, but i don’t understand HOW called exactly _toHtml() function??
Oleksii
11. Dec, 2009
{{block type=”mycompany_custom/test” …}} my_param1=”value 1″ another_param=”value 2″}}
How i understand, type=”mycompany_custom/test” initiate call to class MyCompany_Custom_Block_Test, but i don’t understand HOW called exactly _toHtml() function??
Sorry about my bad english)
Éric
11. Dec, 2009
Where do we put the phtml file in Tom’s solution ? It’s interesting!
Name
11. Dec, 2009
Forget that, I found it. Was having a few problems due to some cut’n paste with wrong quotation marks.
If someone else like me, it’s simply in :
/app/design/frontend/your_interface/default/template/your_folder/your_file.phtml
Björn
27. Feb, 2010
Thanks a lot for this great tutorial. I had quite some trouble getting the custom params. After a while I came up with the following solution:
public function getAttribute()
{
return $this->_getData(’my_param1′);
}
protected function _toHtml()
{
…
$var = $this->getAttribute();
…
}
One very important thing is to add the block into the cms content area and not to the layout updates!
david
21. Jul, 2010
Thanks, just what i was looking for!
bhavya
31. Aug, 2010
Hi to everybody. I want to display only categories in this page
http://www.uniquefloorsandstone.com/index.php/floor-navigator/find-hardwood-flooring.html
Plz help me
mage
11. Nov, 2010
The Block class should extend mage_core_block_template, not mage_core_block_abstract
vxcriss
29. Nov, 2010
Hey, thanks for the tutorial, this magento was driving me nuts.
kalpa
17. Dec, 2010
i cant get that params.this solution is not working for me.
What is that params.value
Leave a reply