In short: I want to call a frontend block inside a model to get the output of the PHTML template.
I have a test.phtml template which generates content of a specific HTML file for my module. This HTML has to be generated only on administrator request so I call it inside a controller. And that controller calls model:
public function generateAction()
{
Mage::getSingleton('helloworld/sample')->doSomething();
}
Model looks like this:
class My_Helloworld_Model_Sample Mage_Core_Model_Abstract
{
public function doSomething()
{
$templatePath = 'helloworld/test.phtml';
$output = Mage::app()->getLayout()
->createBlock("core/template")
->setData('area','frontend')
->setTemplate($templatePath)
->toHtml();
//write $output in HTML file
}
//...
}
It calls block, gets the output of the test.phtml template file and writes it in HTML file.
Why I don't generate that HTML inside one of the models methods? Two reasons: - user needs to have easy access to that file - .phtml file is much more readable for user/designer
That's why I want to create a block to get its output. But the problem is that when I try to create that block, I get this error:
CRIT (2): Not valid template file:frontend/base/default/template/test.phtml
Magento searches for the template inside the "base" theme. If I put that file there (frontend/base/default/template/test.phtml), then all works fine. But I'd like to keep that template inside the current theme's directory (where I keep the rest of the module's template files):
frontend/package/theme/template/test.phtml
How could I achieve this?
EDIT:
I'm sorry, I wanted to simplify the code to make it more readable. Here's where the template file is actually located:
frontend\default\modern\template\mymodule\test.phtml
After the button in the admin panel is clicked, controller calls model:
public function generateAction()
{
//Get model and call the method to generate HTML
Mage::getSingleton('mymodule/sample')->doSomething();
}
Model creates block to get output of the test.phtml template:
class My_Mymodule_Model_Sample Mage_Core_Model_Abstract
{
public function doSomething()
{
$templatePath = 'mymodule' . DS . 'test.phtml';
$output = Mage::app()->getLayout()
->createBlock("core/template")
->setData('area','frontend')
->setTemplate($templatePath)
->toHtml();
//write $output in HTML file
}
//...
}
Until now all works fine. But when the block is created, Magento can't find the template file, and gives me this error:
CRIT (2): Not valid template file:frontend\ base\default \template\mymodule\test.phtml
After I added Mage::log
in app/code/core/Mage/Core/Model/Design/Package.php, I got this info in the system.log file:
2012-09-06T09:40:55+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\ frontend\default\default \template\mymodule\test.phtml 2012-09-06T09:40:55+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\frontend\default\default\template\mymodule\test.phtml 2012-09-06T09:40:55+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\frontend\default\default\template\mymodule\test.phtml 2012-09-06T09:40:55+00:00 CRIT (2): Not valid template file:frontend\base\default\template\mymodule\test.phtml 2012-09-06T09:40:56+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml 2012-09-06T09:40:56+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml 2012-09-06T09:40:56+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml
If I modify the model method like this (comment out reference to frontend):
class My_Mymodule_Model_Sample Mage_Core_Model_Abstract
{
public function doSomething()
{
$templatePath = 'mymodule' . DS . 'test.phtml';
$output = Mage::app()->getLayout()
->createBlock("core/template")
//->setData('area','frontend') // <--removed
->setTemplate($templatePath)
->toHtml();
//write $output in HTML file
}
//...
}
I get this info in the system.log file:
2012-09-06T09:44:46+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\ adminhtml\default\default \template\mymodule\test.phtml 2012-09-06T09:44:46+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\template\mymodule\test.phtml 2012-09-06T09:44:46+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\template\mymodule\test.phtml 2012-09-06T09:44:46+00:00 CRIT (2): Not valid template file:adminhtml\base\default\template\mymodule\test.phtml 2012-09-06T09:44:47+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml 2012-09-06T09:44:47+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml 2012-09-06T09:44:47+00:00 DEBUG (7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml
It seems that Magento doesn't care what theme is currently enabled and searches for the template in base theme. Is there any way to "tell" Magento which template should be used?