简体   繁体   English

在Magento的模型方法中获取前端PHTML模板的输出

[英]Get frontend PHTML template's output inside a model method in Magento

In short: I want to call a frontend block inside a model to get the output of the PHTML template. 简而言之:我想在模型中调用一个前端块,以获取PHTML模板的输出。

I have a test.phtml template which generates content of a specific HTML file for my module. 我有一个test.phtml模板,该模板为我的模块生成特定HTML文件的内容。 This HTML has to be generated only on administrator request so I call it inside a controller. 仅应管理员要求生成此HTML,因此我在控制器内调用它。 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. 它调用block,获取test.phtml模板文件的输出,并将其写入HTML文件。

Why I don't generate that HTML inside one of the models methods? 为什么我没有在其中一种模型方法中生成HTML? Two reasons: - user needs to have easy access to that file - .phtml file is much more readable for user/designer 两个原因:-用户需要可以轻松访问该文件-.phtml文件对于用户/设计者而言更具可读性

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 CRIT(2):无效的模板文件:frontend / base / default /template/test.phtml

Magento searches for the template inside the "base" theme. Magento在“基本”主题内搜索模板。 If I put that file there ( frontend/base/default/template/test.phtml ), then all works fine. 如果我将该文件放在那里( frontend / base / default / template / test.phtml ),那么一切正常。 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 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 前端\\默认\\现代\\模板\\ 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: 模型创建块以获取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
    }
    //...
}

Until now all works fine. 到目前为止,一切正常。 But when the block is created, Magento can't find the template file, and gives me this error: 但是当创建块时,Magento找不到模板文件,并给了我这个错误:

CRIT (2): Not valid template file:frontend\\ base\\default \\template\\mymodule\\test.phtml CRIT(2):无效的模板文件: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: 在app / code / core / Mage / Core / Model / Design / Package.php中添加Mage::log之后,在system.log文件中获得了此信息:

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 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

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: 我在system.log文件中获得此信息:

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 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

It seems that Magento doesn't care what theme is currently enabled and searches for the template in base theme. Magento似乎并​​不在乎当前启用了什么主题,而是在基本主题中搜索模板。 Is there any way to "tell" Magento which template should be used? 有什么办法“告诉” Magento应该使用哪个模板?

You've already have the answer, you've just misidentified the problem. 您已经有了答案,只是误认了问题。

Magento only looks in the base package once it's looked for a file in the current theme. Magento仅在base包中查找当前主题中的文件。 So first Magento will check 所以首先Magento会检查

frontend/package/theme/template/test.phtml

THEN it will check 然后它将检查

frontend/base/default/template/test.phtml

and if it still doesn't find anything, then it logs the error. 如果仍然找不到任何内容,则记录错误。

If you want to debug where Magento is trying to load the initial file from, add some temporary var_dump or Mage::Log debugging code to the design package file 如果要调试Magento尝试从中加载初始文件的位置,请在设计包文件中添加一些临时的var_dumpMage::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;
}

Also, include the actual code you're using on your system here and people will be better able to help you. 另外,在此处包括您在系统上使用的实际代码,这样人们会更好地为您提供帮助。 There's a discrepancy between what you say you're setting your template path as 您说的是将模板路径设置为以下内容之间存在差异

helloworld/test.phtml

and what you're actually setting is as (per the Magento errors) 和您实际设置的是(根据Magento错误)

test.phtml

Update : Based on what's in the additional logging, it looks like something is adding spaces to your template path, which means Magento can't find your template in it's initial location 更新 :根据其他日志中的内容,看起来好像是在模板路径中添加了空格,这意味着Magento在初始位置找不到模板。

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

Also, I don't think it's the root cause, but don't use DS in template paths. 另外,我不认为这是根本原因,但不要在模板路径中使用DS PHP will take care of that fore you. PHP会照顾您的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM