来源:http://trac.seagullproject.org/wiki/Howto/Templates/Flexy/Plugins

在Flexy使用插件

快速入门

给模板做一个简单的插件:

1) 在module/classes目录中创建一个名为Output.php的文件

2) 在此文件中创建一个名为ModuleOutput(即以模块名为前缘)的PHP类和并添加一个方法,如,要根据从数据库取回的数据动态生成一个下拉列表

class ModuleOutput {
    function selectMyData($default)
    {
        $aList = getMyDataFromDatabase();  // fetch a list of data from my DB
        $sel = SGL_Output::generateSelect($aList, $default);
        return $sel;
    }
}

注意,类名是由我们的模块名Module加上Output组成的

3) 保存文件,像平常一样创建模板文件

4) 要在模板中使用插件只需加上这段代码:

<form method="post" flexy:ignore>
  <select name="mySelect">
      {this.plugin(#selectMyData#,default):h}
  </select>
</form>

注意参数是如何传递给插件的:你必须使用#字符作为插件名称和常量的定隔符。如果你要传递的是一个PHP变量的值,像例子中那样 ,不要用#包含变量名。

5) 好了,保存模板并浏览页面的。Seagull框架将会在你模块目录中寻找一个名为Output.php的文件并将其集成到Flexy引擎。

第一个注意事项

在编译完的模板中,$t是模板中保存所有模板可用变量的对象(也就是我们放在$output中的值)。所有当你使用如下代码:

$output->name = 'Foo';

在模板中写:

{name}

编译后的代码将是:

<?php echo htmlspecialchars($t->name); ?>

设置插件

注意Pear的Flexy文件,好像只有Flexy发布的插件,名为Savant,在/lib/pear/HTML/Template/Flexy/Plugin目录中。

我想创建一个名为Output的插件,它包含所有~/modules/publisher/classes/Output.php的方法而不用原本的模块方法。 你得为你的插件创建一个php文件:Output.php

在那个文件中,如:PublisherOutput

在类中定义你要使用的方法,在我们的实例中就是从原始Output.php中引用的方法:

  articleOutput()
  id2AssetIcon($documentID)
  . . . 
  test() // added this method for testing only''

测试方法代码是:

function test($data) {
    echo '<b>Test Method Output</b><br>';
    echo '<pre>'; print_r($data); echo '</pre>';
}

这样插件就可以使用了。

调用插件

在模板文件中有两种调用插件的方法: 第一种:

{this.plugin(#methodName#,#parameters#)} 

在我们的例子中是:

{this.plugin(#test#,#abc#)}

编译后变成:

<?php if ($this->options['strict'] || (isset($this) && method_exists($this,'plugin']) echo htmlspecialchars($this->plugin("test","abc"];?> 

需要注意的是:

$this->plugin("test","abc"] 

和输出的HTML:

<b>Test Method Output</b><br><pre>abc</pre>

因为插件类(如: HTML_Template_Flexy_Plugin_Output)不是继承$t类,所以$t中的变量就不能用,因此你必须把它作为一个参数传递给插件。

我们有:

$output->name = 'Foo';

{this.plugin(#test#,name)} 

生成:

$this->plugin("test",$t->name) 

HTML输出:

<b>Test Method Output</b><br><pre>Foo</pre>

或者可以访问整个类:

{this.plugin(#test#,t)}

生成:

$this->plugin("test",$t)

这样在屏幕上可以输出$t的所有内容。在我们的例子中还有一引起额外的信息输出。

当然你可以不带任何参数调用

{this.plugin(#test#)}

生成:

你可以使用如下可选参数:

:h - 不做任何修改 - eg. Raw
:u - echos urlencode($variable)

如:

{this.plugin(#test#,name):h} or {this.plugin(#test#,name):u}

另一种调用插件的方法:

{params:methodName}

这个时候参数必须是$t中的一个变量而且通常是不做任何修改输出的(如 :h 修饰)。

在我们的例子中:

{name:test}

相当于:

{this.plugin(#test#,name)}

生成:

<?php echo $this->plugin("test",$t->abc);?>

or the interesting part

{t:test}

生成:

$this->plugin("test",$t)

最后我们可以将修改~/www/themes/default/publisher/ArticleView.html中的传统方法调用:

{articleOutput()}

为调用插件:

{t:articleOutput}

稍微修改articleOutput以接收$t的参数而不在代码中使用$this:

  function articleOutput($out)
  {   
      SGL::logMessage(null, PEAR_LOG_DEBUG);
      $templ = new HTML_Template_Flexy();
      $aCurrentType = explode(' ', $out->leadArticle->type);              
      $out->articleTmpl = 'articleView';     
  }  

这样就可以了。

相关链接

 
howto/templates/flexy/plugins.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2