html 创建,如何正确创建HTML类?

我正在使用PHP中的HTML类,这样我们就可以保持所有HTML输出的一致性。但是,我在绕过逻辑时遇到了一些麻烦。我在PHP工作,但任何语言的答案都可以。

我希望该类正确嵌套标记,所以我希望能够像这样调用:

$html = new HTML;

$html->tag("html");

$html->tag("head");

$html->close();

$html->tag("body");

$html->close();

$html->close();

类代码在幕后使用数组,并推送数据,弹出数据。我很确定我需要创建一个子阵列,使

在下面,但我无法弄清楚逻辑。这是HTML类的实际代码:

class HTML {

/**

* internal tag counter

* @var int

*/

private $t_counter = 0;

/**

* create the tag

* @author Glen Solsberry

*/

public function tag($tag = "") {

$this->t_counter = count($this->tags); // this points to the actual array slice

$this->tags[$this->t_counter] = $tag; // add the tag to the list

$this->attrs[$this->t_counter] = array(); // make sure to set up the attributes

return $this;

}

/**

* set attributes on a tag

* @author Glen Solsberry

*/

public function attr($key, $value) {

$this->attrs[$this->t_counter][$key] = $value;

return $this;

}

public function text($text = "") {

$this->text[$this->t_counter] = $text;

return $this;

}

public function close() {

$this->t_counter--; // update the counter so that we know that this tag is complete

return $this;

}

function __toString() {

$tag = $this->t_counter + 1;

$output = "tags[$tag];

foreach ($this->attrs[$tag] as $key => $value) {

$output .= " {$key}="" . htmlspecialchars($value) . """;

}

$output .= ">";

$output .= $this->text[$tag];

$output .= "" . $this->tags[$tag] . ">";

unset($this->tags[$tag]);

unset($this->attrs[$tag]);

unset($this->text[$tag]);

$this->t_counter = $tag;

return $output;

}

}

任何帮助将不胜感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值