Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 获取前端PHTML模板';s在Magento中的模型方法内的输出_Templates_Magento_Model_Module_Block - Fatal编程技术网

Templates 获取前端PHTML模板';s在Magento中的模型方法内的输出

Templates 获取前端PHTML模板';s在Magento中的模型方法内的输出,templates,magento,model,module,block,Templates,Magento,Model,Module,Block,简而言之:我想调用模型内的前端块,以获得PHTML模板的输出 我有一个test.phtml模板,它为我的模块生成特定HTML文件的内容。这个HTML只能在管理员请求时生成,所以我在控制器内部调用它。控制器调用模型: public function generateAction() { Mage::getSingleton('helloworld/sample')->doSomething(); } public function generateAction() { //

简而言之:我想调用模型内的前端块,以获得PHTML模板的输出

我有一个test.phtml模板,它为我的模块生成特定HTML文件的内容。这个HTML只能在管理员请求时生成,所以我在控制器内部调用它。控制器调用模型:

public function generateAction()
{
    Mage::getSingleton('helloworld/sample')->doSomething();
}
public function generateAction()
{
    //Get model and call the method to generate HTML
    Mage::getSingleton('mymodule/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文件

为什么我不在其中一个models方法中生成HTML?原因有二: -用户需要轻松访问该文件 -.phtml文件对于用户/设计者来说可读性更高

这就是为什么我想创建一个块来获得它的输出。但问题是,当我尝试创建该块时,会出现以下错误:

临界值(2):无效的模板 文件:frontend/base/default/template/test.phtml

Magento在“基本”主题内搜索模板。如果我把那个文件放在那里(frontend/base/default/template/test.phtml),那么一切都正常。但我希望将该模板保存在当前主题的目录中(我将模块的其余模板文件保存在该目录中):

前端/包/主题/template/test.phtml

我怎样才能做到这一点

编辑: 很抱歉,我想简化代码以使其更具可读性。以下是模板文件的实际位置:

前端\default\modern\template\mymodule\test.phtml

单击管理面板中的按钮后,控制器调用模型:

public function generateAction()
{
    Mage::getSingleton('helloworld/sample')->doSomething();
}
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找不到模板文件,并给出以下错误:

临界值(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临界值(2):无效模板 文件:frontend\base\default\template\mymodule\test.phtml 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 2012-09-06T09:40:56+00:00调试(7): E:\webserver\xampp\htdocs\magento\app\design\adminhtml\default\default\layout\local.xml

如果我这样修改模型方法(注释掉对前端的引用):

class My\u Mymodule\u Model\u Sample Mage\u Core\u Model\u Abstract
{
公共函数doSomething()
{
$templatePath='mymodule.DS.'test.phtml';
$output=Mage::app()->getLayout()
->createBlock(“核心/模板”)
//->setData('area','frontend')//setTemplate($templatePath)
->toHtml();
//在HTML文件中写入$output
}
//...
}
我在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临界值(2):无效模板 文件:adminhtml\base\default\template\mymodule\test.phtml 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 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
然后它会检查

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错误)

更新:根据附加日志中的内容,似乎有什么东西正在向模板路径添加空格,这意味着Magento无法在模板的初始位置找到模板

E:\webserver\xampp\htdocs\magento\app\design\ frontend\default\default \template\mymodule\test.phtml
ex.
design\ frontend
\default \template

另外,我不认为这是根本原因,但不要在模板路径中使用
DS
。PHP会在您之前解决这个问题。

您已经找到了答案,只是错误地识别了问题

Magento仅在查找文件后在
base
包中查找