xc / xoverridefilter
“X Override Filter” 是一个简单但实用的 eZ Publish 旧版扩展,允许在 override.ini 中配置 eZ Publish 商业逻辑和模板
Requires
This package is not auto-updated.
Last update: 2024-09-23 13:18:21 UTC
README
介绍
使用这个简单的扩展,您可以在 override.ini 中定义您的业务逻辑(PHP 类)和模板覆盖,因此业务逻辑可以在纯 PHP 类中轻松清晰地完成,而不是自定义操作符、复杂的模板或数据类型。
最初的想法来自 pull request:ezsystems/ezpublish-legacy#694,这是 eZ Publish 5.2 中的自动功能。
小型示例
在 override.ini 中的定义
[myform_view]
Source=node/view/full.tpl
MatchFile=myform.tpl
Subdir=templates
Match[class_identifier]=myform
#Class is new :)
Class=myFormView
实现模板 form.tpl
{if is_set( $result )}
<div>{$result}</div>
{/if}
<form action="" method="post">
<div>
Name: <input type="text" name="name" value="" />
</div>
<div>
Email: <input type="text" name="email" value="" />
</div>
<div>
<input type="submit" name ="SubmitButton" value='Submit' />
<input type="submit" name ="DiscardButton" value='Discard' />
</div>
</form>
实现类 myFormView
class myFormView implements xNodeviewRender
{
function initNodeview( $module, $node, $tpl, $viewMode )
{
$tpl->setVariable( 'cache_ttl', 0 );
$http = eZHTTPTool::instance();
if( $http->hasVariable( 'SubmitButton' ) )
{
// Show result if the form submit
$name = $http->variable( 'name' );
$email = $http->variable( 'email' );
$tpl->setVariable( 'result', ezpI18n::tr( 'example', "You inputed Name: %1, Email %2", '', array( $name, $email ) ) );
}
else if( $http->hasVariable( 'DiscardButton' ) )
{
// Redirect to homepage if the form is discarded.
$module->redirectTo( '/' );
}
}
}
增强 override.ini
增强 override.ini 支持
- 类标签用于识别逻辑的 PHP 类实现。
- Match[attribute_<attribute_identifier>]=<value> 以更好地过滤模板。
- Match[node]、Match[class_identifer]、Match[viewmode] 用于视图逻辑条件
上述三个可以与现有的模板覆盖结合使用。
要求
-
对于 eZ Publish 5.2:表现良好
-
对于 eZ Publish 4.2 - 5.1:需要内核补丁(不是未经授权的修改,而是功能回滚)。请参阅 doc/patches/event-pre_rending-*.diff
安装
-
将此扩展复制到 <ezp_root>/extension
-
如果您的 eZ Publish 小于 5.2(例如 4.7),请在 <ezp_root> 下运行以下命令
cp extension/xoverridefilter/doc/patches/event-pre_rending-4.5-4.7.diff ./ patch -p0 < event-pre_rending-4.5-4.7.diff --dry-run patch -p0 < event-pre_rending-4.5-4.7.diff注:对于社区版本,4.5-4.7 = 社区 2011.5-2012.5
-
激活扩展 xoverridefilter
-
清除 ini 缓存
-
重新生成 autoload 数组
示例(请参阅 doc/example 中的代码)
-
在 myextension 下配置条件和类
场景 1 - 自定义视图逻辑
extension/myextension/settings/override.ini.append.php。
[myform_view] Match[class_identifier]=myform Class=myFormView上述配置意味着“myform”对象将使用 myFormView 进行视图逻辑。表单模板可以在额外的模板覆盖规则中定义。
场景 2 - 自定义视图逻辑和自定义模板。您还可以在一个覆盖规则中将视图逻辑与模板覆盖结合起来。
[myform_view_2] Source=node/view/full.tpl MatchFile=form.tpl Subdir=templates Match[class_identifier]=myform #Condition section_identifier will be ignored by custom view logic. Match[section_identifier]=standard Class=myFormView上述配置意味着,在标准部分下的“myform”对象将使用类 myFormView 作为视图逻辑,并使用表单模板;而在其他部分下的“myform”对象将使用 myFormView 作为视图逻辑,并使用 full.tpl(如果没有其他覆盖规则适用)作为模板。
场景 3 - 使用属性进行覆盖匹配。最好使用此方法代替 node_id。例如
[article_breaking-news_full]
Source=node/view/full.tpl
MatchFile=break-news.tpl
Subdir=templates
Match[attribute_article_identifier]=breaking-news
Match[class_identifier]=article
[article_campaign_full]
Source=node/view/full.tpl
MatchFile=campaign.tpl
Subdir=templates
Match[attribute_article_identifier]=campaign
Match[class_identifier]=article
-
实现模板 form.tpl
extension/myextension/design/standard/override/templates/form.tpl
{if is_set( $result )} <div>{$result}</div> {/if} <form action="" method="post"> <div> {"Name:"|i8n('example')} <input type="text" name="name" value="" /> </div> <div> {"Email:"|i18n('example')} <input type="text" name="email" value="" /> </div> <div> <input type="submit" name ="SubmitButton" value={'Submit'|i18n( 'example' )} /> <input type="submit" name ="DiscardButton" value={'Discard'|i18n( 'example' )} /> </div> </form> -
实现类 myFormView
extension/myextension/classes/myformview.php
<?php class myFormView implements xNodeviewRender { /** * This method is invoked before template is fetched. * * Typical usage: * 1. Set php variable to template directly instead of use eZ custom operator in template * 2. Customize http form action * */ public function initNodeview( $module, $node, $tpl, $viewMode ) { // Disable view cache for this page, since the form will be dynamic $tpl->setVariable( 'cache_ttl', 0 ); $http = eZHTTPTool::instance(); if( $http->hasVariable( 'SubmitButton' ) ) { // Show result if the form submit $name = $http->variable( 'name' ); $email = $http->variable( 'email' ); $tpl->setVariable( 'result', ezpI18n::tr( 'example', "You inputed Name: %1, Email %2", '', array( $name, $email ) ) ); } else if( $http->hasVariable( 'DiscardButton' ) ) { // Redirect to homepage if the form is discarded. $module->redirectTo( '/' ); } } } ?> -
为扩展 bin/php/ezpgenerateautoloads.php 重新生成 autoload 数组 -e
-
在查看页面之前清除缓存(content/view/full/50)。
常见问题解答
-
业务逻辑是否在视图缓存中?
是的,对于第一次访问,eZ 将调用业务逻辑并生成视图缓存,对于第二次访问,它可能会跳过业务逻辑并只加载视图缓存,前提是视图缓存密钥没有更改。
因此,对于高度动态的页面(如表单),建议从 PHP 中禁用视图缓存。
public function initNodeview( $module, $node, $tpl, $viewMode ) { $tpl->setVariable( 'cache_ttl', 0 );
路线图
应该
- 支持覆盖规则层次结构
可以
- 通过持久变量/缓存块密钥控制缓存
反馈?
欢迎提出任何评论/建议。
在 projects.ez.no 上进行评审: http://projects.ez.no/xoverridefilter/reviews .
联系我?您可以通过 share.ez.no: http://share.ez.no/community/profile/86893