在 Symfony 1.4 中渲染任务的部分内容

发布于 2024-08-27 11:29:14 字数 279 浏览 7 评论 0原文

我正在尝试在 Symfony 任务中渲染部分内容,但没有运气。我在 1.1 中发现文档说只需调用 get_partial() ,但显然在 1.4 中不再可用。我尝试使用 sfLoader::getHelpers('Partial'); 手动加载帮助程序,但我收到“Class sfLoader not found”。任何帮助将不胜感激。

作为参考,我想做的是从我的所有布局中使用的全局标头部分生成一个名为“header.html”的 HTML 文件,以便包含在我正在集成的第三方论坛(Simple Machines/SMF)中。

I'm trying to render a partial in a Symfony task and having no luck. I found docs in 1.1 that say to just call get_partial() but apparently that's no longer readily available in 1.4. I tried loading the helper manually with sfLoader::getHelpers('Partial'); but I get "Class sfLoader not found". Any help would be greatly appreciated.

For reference what I'm trying to do is generate an HTML file called 'header.html' from my global header partial used in all of my layouts for inclusion in a third-party forum I'm integrating (Simple Machines/SMF).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

猫烠⑼条掵仅有一顆心 2024-09-03 11:29:14

第一次加载:

sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');

然后调用:

get_partial()

First load:

sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');

then just call:

get_partial()
Bonjour°[大白 2024-09-03 11:29:14

不要忘记在任务选项中添加应用程序名称。查找这一行:

new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED,'The application name', 'frontend')

现在 $this->configuration 返回您需要的 sfApplicationConfiguration

Don't forget to add an application name in your task's options. Look for this line:

new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED,'The application name', 'frontend')

Now $this->configuration returns the sfApplicationConfiguration that you need.

羁〃客ぐ 2024-09-03 11:29:14

这就能解决问题:

//load the Partial helper to be able to use get_partial()
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');

This will do the trick:

//load the Partial helper to be able to use get_partial()
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');
独闯女儿国 2024-09-03 11:29:14

sfLoader 在 symfony 1.2 中已弃用 - 我认为您需要查看 1.4 API 以及您熟悉的任何版本的升级帮助,因为这些将是您需要参考的资源到很多。

解决问题的技巧是使用 sfApplicationConfiguration 类提供的 loadHelpers() 方法加载帮助器 - 您的任务应该在其 configure( ) 方法。我自己以前没做过,请注意...

sfLoader was deprecated in symfony 1.2 - I think you need to look over the 1.4 API and the upgrade help from whichever version you're familiar with, as these are going to be resources you'll need to refer to a lot.

The trick to solving your problem is to load the helper with the loadHelpers() method provided by the sfApplicationConfiguration class - your task should hook this method in its configure() method. I've not done it before myself, mind...

还如梦归 2024-09-03 11:29:14

刚刚在任务中调用了部分用于发送免费试用 nag 电子邮件的代码:

//combines the partial with passed variables and puts the results in a string variable
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');

$partial_in_a_string = $contextInstance->getController()
          ->getAction('module_name', 'action_name')
          ->getPartial('module_name/partial_name', array('var_name'=>'var_value'));

Just called a partial in a task for use in sending free trial nag email here's the code:

//combines the partial with passed variables and puts the results in a string variable
$contextInstance = sfContext::createInstance($this->configuration);
$contextInstance->getConfiguration()->loadHelpers('Partial');

$partial_in_a_string = $contextInstance->getController()
          ->getAction('module_name', 'action_name')
          ->getPartial('module_name/partial_name', array('var_name'=>'var_value'));
想挽留 2024-09-03 11:29:14

您可以使用您还可以在taskClass::configure()方法中定义应用程序命令选项来访问当前配置

$this->configuration

(如果您已使用symfony生成:任务来生成任务类,您应该将前端作为默认应用程序选项)。
或者,您可以在调用任务时使用 --application=appName 从 cli 传递应用程序。

you can access the current configuration with

$this->configuration

You can also define application command option in taskClass::configure() method (if you have used the symfony generate:task to generate the task class you should have frontend as default application option).
Optionally you can pass an application using --application=appName from cli when calling your task.

迷鸟归林 2024-09-03 11:29:14

确保在您的配置方法中具有应用程序选项:

protected function configure()
{
    $this->addOptions(array(
        new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend')
    ))
    ....

然后,在执行方法中:

protected function execute($arguments = array(), $options = array())
{
    sfContext::createInstance($this->configuration)->getConfiguration()->loadHelpers('Partial');

这样您就可以防止这些错误:

“默认”上下文不存在。

传递给 sfContext::createInstance() 的参数 1 必须是一个实例
sfApplicationConfiguration

Be sure to have the application option in your configure method:

protected function configure()
{
    $this->addOptions(array(
        new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend')
    ))
    ....

And then, in the execute method:

protected function execute($arguments = array(), $options = array())
{
    sfContext::createInstance($this->configuration)->getConfiguration()->loadHelpers('Partial');

On this way you will prevent these errors:

The "default" context does not exist.

and

Argument 1 passed to sfContext::createInstance() must be an instance
of sfApplicationConfiguration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文