如何在Symfony 1.4中实现一个助手? [英] How to implement a helper in Symfony 1.4?

查看:78
本文介绍了如何在Symfony 1.4中实现一个助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的帮助程序,但无法在Google for Symfony 1.4/Doctrine上找到任何帮助.

I'd like to create my own helper but can't find any help on Google for Symfony 1.4/Doctrine.

我想这与在lib/helpers/中创建myClassHelper.class.php有关,但我不知道要实现什么,或者是否必须重写特定方法.

I guess it has something to do with creating a myClassHelper.class.php in lib/helpers/ or something, but I don't know what to implement, or if specific methods have to be overridden.

任何帮助将不胜感激!

推荐答案

我认为1.4中的助手没有任何变化.从文档(尽管是1.2) :

I don't think that anything changed regarding the helpers in 1.4. From the documentation(although 1.2):

Helper函数(返回HTML代码的常规PHP函数)应保存在名为FooBarHelper.php的文件中,其中FooBar是helper组的名称.将文件存储在apps/myapp/lib/helper/目录中(或在项目的lib/文件夹之一下创建的任何helper/目录中),以便use_helper('FooBar')助手可以自动将其包含在内.

Helper functions (regular PHP functions returning HTML code) should be saved in a file called FooBarHelper.php, where FooBar is the name of the helper group. Store the file in the apps/myapp/lib/helper/ directory (or in any helper/ directory created under one of the lib/ folders of your project) so it can be found automatically by the use_helper('FooBar') helper for inclusion.

因此,您只需将普通函数放在普通文件中即可(与类或方法无关).这些函数可以采用任意参数(您可以决定创建HTML所需的参数),还可以返回HTML.

So you just put normal functions in a normal file (has nothing to do with classes or methods). The functions can take arbitrary parameters (you decide what they need to create the HTML) and the have to return HTML.

例如

MyHelper.php

function hello_word() {
   return '<strong>Hello world!</strong>';
}

并在模板中:

<?php use_helper('My') ?>

<!-- somewhere in the template -->
<?php echo hello_world() ?>

这篇关于如何在Symfony 1.4中实现一个助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆