Templates Symfony1模板默认值和回退或模块之间的模板共享

Templates Symfony1模板默认值和回退或模块之间的模板共享,templates,view,symfony1,symfony-1.4,admin-generator,Templates,View,Symfony1,Symfony 1.4,Admin Generator,我在处理一个Symfony1应用程序 对于应用程序的一部分,我们使用symfony管理生成器为特定模块构建默认操作和模板 可以在子操作类中重写自动生成的操作方法,也可以通过在模块的模板文件夹中使用相同的名称来重写模板。 使用本地模板而不是自动生成的模板,这些模板存储在缓存文件夹中(我假设这是正常的symfony行为) 我目前正在开发一个单独的应用程序,它不使用管理生成器 但我有3个模块,它们做着非常相似的事情,我想和大家分享一下 我有3个操作扩展了相同的自定义操作类,这样它们都实现了相同的方法并

我在处理一个Symfony1应用程序

对于应用程序的一部分,我们使用symfony管理生成器为特定模块构建默认操作和模板

可以在子操作类中重写自动生成的操作方法,也可以通过在模块的模板文件夹中使用相同的名称来重写模板。 使用本地模板而不是自动生成的模板,这些模板存储在缓存文件夹中(我假设这是正常的symfony行为)

我目前正在开发一个单独的应用程序,它不使用管理生成器

但我有3个模块,它们做着非常相似的事情,我想和大家分享一下

我有3个操作扩展了相同的自定义操作类,这样它们都实现了相同的方法并共享了相同的方法

我遇到的问题是共享模板。 主模板和大部分部分可以按原样重用

apps/
    other_app/
        lib/
            printingActions.class.php
        modules/
            ticket/
                actions/
                    actions.class.php
                lib/
                templates/
                    printSuccess.php //exactly the same for all 3
                    _part_1.php
                    _part_2.php      //exactly the same for all 3
                    _part_3.php
            receipt/
                actions/
                    actions.class.php
                lib/
                templates/
                    printSuccess.php //exactly the same for all 3
                    _part_1.php
                    _part_2.php      //exactly the same for all 3
                    _part_3.php
            voucher/
                actions/
                    actions.class.php
                lib/
                templates/
                    printSuccess.php //exactly the same for all 3
                    _part_1.php
                    _part_2.php      //exactly the same for all 3
                    _part_3.php
我想做的是拉出常见的模块,这样每个模块和任何具有类似接口的未来模块都只需要具有模块特定信息的部分

这将是我的理想设置:

apps/
    other_app/
        lib/
            printingActions.class.php
            printingCommonTemplates/
                printSuccess.php //exactly the same for all 3
                _part_2.php      //exactly the same for all 3
        modules/
            ticket/
                actions/
                    actions.class.php
                lib/
                templates/
                    _part_1.php
                    _part_3.php
            receipt/
                actions/
                    actions.class.php
                lib/
                templates/
                    _part_1.php
                    _part_3.php
            voucher/
                actions/
                    actions.class.php
                lib/
                templates/
                    _part_1.php
                    _part_3.php
我知道这类事情是可以做到的,因为管理生成器可以做到,但经过几个小时的挖掘,我无法找到它到底在哪里做到的

有人能给我指一下这个的正确方向吗?
理想情况下,如果有一个回退模板设置,我可以为一个特定的模块或一个过滤器类设置,我可以扩展它来做我需要的事情?

我找到了一个部分解决方案,至少可以共享模板

在中,它提到有3个地方可以从中调用partials:

  • 从当前模块
  • 来自同一应用程序中的不同模块
  • 从当前应用程序的全局模板文件夹
所以,我能做的就是把公共部分放在一个地方,并从需要使用它们的每个模块中引用它们

我还可以传入一个具有当前模块名称的变量,以便它可以从通用模板中动态调用特定于模块的模板

我考虑过将公共部分直接放在全局模板目录中,但是如果有多种类型的模块这样做,就会变得混乱和混乱

棘手的工作是在模板目录中创建一个目录,我可以将这些文件放入其中

apps/
  other_app/
    lib/
      printingActions.class.php
    modules/
      ticket/
        actions/
          actions.class.php
        lib/
        templates/
          printSuccess.php //common parts extracted to partial
          _part_1.php
          _part_3.php
      receipt/
        actions/
          actions.class.php
        lib/
        templates/
          printSuccess.php //common parts extracted to partial
          _part_1.php
          _part_3.php
      voucher/
        actions/
          actions.class.php
        lib/
        templates/
          printSuccess.php //common parts extracted to partial
          _part_1.php
          _part_3.php
    templates/
      _printing_common/
        print_success_common.php //common parts of PrintSuccess extracted as partial
        part_2.php      //exactly the same for all 3
它产生了一个不太理想的问题,即必须在新目录下加下划线,并从其中的部分中删除下划线

但是,使用这种方法,我能够共享公共位,并且只有模块特定的代码需要在模块模板目录中指定

以下是其中一些文件的内容示例:

apps/other_app/modules/ticket/templates/printsucture.php

<?php
  include_partial(
    'global/printing_common/print_success_common',
    array(
      'moduleNameText' => $moduleNameText
    )
  );

...
...

这不是理想的解决方案,因此我会让这个问题有更好的解决方案,但这是我在此之前的工作。

如果要使用具有默认布局的通用模块操作类,可以使用以下方法:

class printingActions extends sfActions {

  public function preExecute() {
    $this->setLayout('print_success_common');
  }

  public function executeIndex() {

  }
}
然后,在模块操作中,您可能有:

class ticketActions extends printingActions
{
   public function executePrint(sfWebRequest $request)
   {
      $this->txt = 1234;
      return $this->renderPartial('part_1', array('txt' => $this->txt));
   }
}
您可以通过以下方式使用与操作不同的(通用)布局:

class ticketActions extends printingActions
{
   public function executePrint(sfWebRequest $request)
   {
      $template = $this->getContext()->getConfiguration()->getTemplateDir('printing', 'print_success_common_alt.php');
      $this->setLayout($template . '/print_success_common_alt');
      ...

      $this->txt = 1234;
      return $this->renderPartial('part_1', array('txt' => $this->txt));
   }
}

谢谢这让我更接近我想做的事情。这与我之前的回答相结合,应该能让我在大部分方面达到目的。我会将此标记为正确答案,但如果有人提出更好的解决方案,我可能会更改它。
class ticketActions extends printingActions
{
   public function executePrint(sfWebRequest $request)
   {
      $this->txt = 1234;
      return $this->renderPartial('part_1', array('txt' => $this->txt));
   }
}
class ticketActions extends printingActions
{
   public function executePrint(sfWebRequest $request)
   {
      $template = $this->getContext()->getConfiguration()->getTemplateDir('printing', 'print_success_common_alt.php');
      $this->setLayout($template . '/print_success_common_alt');
      ...

      $this->txt = 1234;
      return $this->renderPartial('part_1', array('txt' => $this->txt));
   }
}