我在一个Symfony1应用程序上工作。
对于应用程序的一部分,我们使用symfony管理生成器,它为特定模块构建默认操作和模板。
可以在子操作类中重写自动生成的操作方法,也可以通过在模块的模板文件夹中创建同名的模板来重写模板。使用本地模板而不是自动生成的模板,这些模板存储在缓存文件夹中(我假设这是正常的symfony行为)。
apps/
my_app
modules/
post/
actions/
actions.class.php
lib/
templates/
...
cache/
my_app/
environment/
modules/
autoPostcd /
actions/
actions.class.php
lib/
templates/
indexSuccess.php
_part_1.php
_part_2.php我目前正在处理一个单独的应用程序,它不使用管理生成器。
但我有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我知道这种事情是可以做的,因为管理员生成器可以做到这一点,但是经过几个小时的挖掘,我找不到它到底在哪里做的。
有人能为我指出正确的方向吗?理想情况下,如果有一个备用模板设置,我可以为一个特定的模块或过滤器类,我可以扩展,以完成我的需要?
发布于 2015-07-14 13:08:45
如果要使用具有默认布局的公共模块操作类,可以使用以下方法:
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));
}
}发布于 2015-07-13 13:02:09
我找到了一个部分解决方案,至少能够共享模板。
在symfony1文档的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/printSuccess.php
<?php
include_partial(
'global/printing_common/print_success_common',
array(
'moduleNameText' => $moduleNameText
)
);apps/other_app/templates/_printing_common/print_success_common.php
...
<?php include_partial($moduleNameText.'/part_1',array('moduleNameText' => $moduleNameText)); ?>
...
<?php include_partial('global/printing_common/part_2',array('moduleNameText' => $moduleNameText)); ?>
...
<?php include_partial($moduleNameText.'/part_3',array('moduleNameText' => $moduleNameText)); ?>这不是理想的解决方案,所以我会把这个问题留给一个更好的解决方案,但他们是我的工作,直到那时。
https://stackoverflow.com/questions/31295140
复制相似问题