我不是一个喜欢使用插件或任何东西的人,所以我现在尽量避免使用 Zend 之类的东西,但我想看看用 php 函数创建"template"是否是一种好的做法? 我现在忘记了很多 PHP,一旦我得到他的答案就会重新开始,但我在想这样的事情:
PHP
function input($type, $label, $id, $class) {
echo "<div>";
echo "<input type=" .$type. " class=" .$class. " id=" .$id. />";
echo "<label for=" .$id. ">".$label."</label>";
echo "</div>";
}
HTML
<div class="wrapper">
<?PHP input("text", "Username", "username", "input"); ?>
</div>
当然,这段代码需要根据我的需要进行调整,但我想知道这是否会被视为一种好的做法?或者我应该远离做这样的事情吗?
最佳答案
我看到了评论,我希望你不要批评我说大多数人会同意可重用性:返回值而不是回应它
function input($type, $label, $id, $class) {
// create the div here ...
return $divString;
}
然后根据需要处理结果...
<div class="wrapper">
<?PHP echo input("text", "Username", "username", "input"); ?>
</div>
或者像这样使用它
$formElements = input("text", "Username", "username", "input") . input("密码", "密码", "密码", "输入");
关于用于创建表单元素的 PHP 函数。好的做法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16154483/