如何正确创建 HTML 类?

发布于 2024-07-27 01:30:37 字数 2047 浏览 3 评论 0原文

我正在 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 = "<" . $this->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;
    }
}

任何帮助将不胜感激。

I'm working on an HTML class in PHP, so that we can keep all our HTML output consistent. However, I'm having some trouble wrapping my head around the logic. I'm working in PHP, but answers in any language will work.

I want the class to properly nest the tags, so I want to be able to call like this:

$html = new HTML;

$html->tag("html");
$html->tag("head");
$html->close();
$html->tag("body");
$html->close();
$html->close();

The class code is working behind the scenes with arrays, and pushing data on, popping data off. I'm fairly certain I need to create a sub-array to have the <head> underneath <html>, but I can't quite figure out the logic. Here's the actual code to the HTML class as it stands:

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 = "<" . $this->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;
    }
}

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2024-08-03 01:30:37

归根结底,使用 PHP 的现有 DOM 构造函数之一可能会更简单。

如果这看起来不合理; 简单地使用一个数组作为类的成员来保存子元素应该会产生奇迹。

It might, when it all comes down to it, be simpler to use one of the existing DOM constructors for PHP.

If that doesn't seem reasonable; simply having an array as a member of the class to keep children elements in should do wonders.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文