[英]Get frontend PHTML template's output inside a model method in Magento
简而言之:我想在模型中调用一个前端块,以获取PHTML模板的输出。
我有一个test.phtml模板,该模板为我的模块生成特定HTML文件的内容。 仅应管理员要求生成此HTML,因此我在控制器内调用它。 该控制器调用模型:
public function generateAction()
{
Mage::getSingleton('helloworld/sample')->doSomething();
}
模型看起来像这样:
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
}
//...
}
它调用block,获取test.phtml模板文件的输出,并将其写入HTML文件。
为什么我没有在其中一种模型方法中生成HTML? 两个原因:-用户需要可以轻松访问该文件-.phtml文件对于用户/设计者而言更具可读性
这就是为什么我要创建一个块以获取其输出的原因。 但是问题是,当我尝试创建该块时,出现以下错误:
CRIT(2):无效的模板文件:frontend / base / default /template/test.phtml
Magento在“基本”主题内搜索模板。 如果我将该文件放在那里( frontend / base / default / template / test.phtml ),那么一切正常。 但我想将该模板保留在当前主题的目录中(我将其余模块的模板文件保留在该目录中):
frontend / package / theme /template/test.phtml
我怎样才能做到这一点?
抱歉,我想简化代码以使其更具可读性。 这是模板文件的实际位置:
前端\\默认\\现代\\模板\\ mymodule \\ test.phtml
单击管理面板中的按钮后,控制器将调用模型:
public function generateAction()
{
//Get model and call the method to generate HTML
Mage::getSingleton('mymodule/sample')->doSomething();
}
模型创建块以获取test.phtml模板的输出:
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
}
//...
}
到目前为止,一切正常。 但是当创建块时,Magento找不到模板文件,并给了我这个错误:
CRIT(2):无效的模板文件:frontend \\ base \\ default \\ template \\ mymodule \\ test.phtml
在app / code / core / Mage / Core / Model / Design / Package.php中添加Mage::log
之后,在system.log文件中获得了此信息:
2012-09-06T09:40:55 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ frontend \\ default \\ default \\ template \\ mymodule \\ test.phtml 2012-09- 06T09:40:55 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ frontend \\ default \\ default \\ template \\ mymodule \\ test.phtml 2012-09-06T09:40: 55 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ frontend \\ default \\ default \\ template \\ mymodule \\ test.phtml 2012-09-06T09:40:55 + 00: 00 CRIT(2):无效的模板文件: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调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ layout \\ local.xml 2012-09-06T09:40:56 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ layout \\ local.xml
如果我这样修改模型方法(注释掉对前端的引用):
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
}
//...
}
我在system.log文件中获得此信息:
2012-09-06T09:44:46 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ template \\ mymodule \\ test.phtml 2012-09- 06T09:44:46 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ template \\ mymodule \\ test.phtml 2012-09-06T09:44: 46 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ template \\ mymodule \\ test.phtml 2012-09-06T09:44:46 + 00: 00 CRIT(2):无效的模板文件: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调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ layout \\ local.xml 2012-09-06T09:44:47 + 00:00调试(7):E:\\ webserver \\ xampp \\ htdocs \\ magento \\ app \\ design \\ adminhtml \\ default \\ default \\ layout \\ local.xml
Magento似乎并不在乎当前启用了什么主题,而是在基本主题中搜索模板。 有什么办法“告诉” Magento应该使用哪个模板?
您已经有了答案,只是误认了问题。
Magento仅在base
包中查找当前主题中的文件。 所以首先Magento会检查
frontend/package/theme/template/test.phtml
然后它将检查
frontend/base/default/template/test.phtml
如果仍然找不到任何内容,则记录错误。
如果要调试Magento尝试从中加载初始文件的位置,请在设计包文件中添加一些临时的var_dump
或Mage::Log
调试代码。
#File: app/code/core/Mage/Core/Model/Design/Package.php
public function validateFile($file, array $params)
{
$fileName = $this->_renderFilename($file, $params);
$testFile = (empty($params['_relative']) ? '' : Mage::getBaseDir('design') . DS) . $fileName;
if (!file_exists($testFile)) {
var_dump($testFile);
Mage::Log($testFile);
return false;
}
return $fileName;
}
另外,在此处包括您在系统上使用的实际代码,这样人们会更好地为您提供帮助。 您说的是将模板路径设置为以下内容之间存在差异
helloworld/test.phtml
和您实际设置的是(根据Magento错误)
test.phtml
更新 :根据其他日志中的内容,看起来好像是在模板路径中添加了空格,这意味着Magento在初始位置找不到模板。
E:\webserver\xampp\htdocs\magento\app\design\ frontend\default\default \template\mymodule\test.phtml
ex.
design\ frontend
\default \template
另外,我不认为这是根本原因,但不要在模板路径中使用DS
。 PHP会照顾您的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.