PHP HTML div 样式表格渲染类:缩进错误

huangapple go评论67阅读模式
英文:

PHP HTML div styled table rendering class: wrong indentation

问题

getHtmlDiv函数中,在</div>"之前添加以下代码:

if (substr($output, -2) !== "\n") {
    $output .= "\n" . str_repeat("    ", $this->tabs + $tabcount - 1);
}

这段代码将确保<div>的闭合标签在新的一行且正确缩进。

英文:

I am writing a class that renders div styled tables. The rendered table structure is as follows:

<div class="table">
    <!-- title, column headers -->
    <div class="table-body">
        <div class="table-row">
            <div class="table-row-cell">data11</div>
            <div class="table-row-cell">data12</div>
            <div class="table-row-cell">data13</div></div> <!-- this -->
        <div class="table-row">
            <div class="table-row-cell">data21</div>
            <div class="table-row-cell">data22</div>
            <div class="table-row-cell">data23</div></div> <!-- and this -->
    </div>
    <!-- footer -->
</div>

If you take a look, <div> closing tags for table rows should be in a new line correctly indented but that is not happening.

This is how I build the div's:

public function get(): string
{
    // some code and loops

    $row_build .= $this->getHtmlDiv(
                            $html_cell_class,
                            $cell_data ?? "",
                            $this->html_attributes['cell'],
                            3
                        );
    // more code
}

private function getHtmlDiv(
    string $class,
    string $content = "",
    array $attrs = [],
    int $tabcount = 0
): string {
    $output = "\n" . str_repeat("    ", $this->tabs + $tabcount) . "<div";

    $output .= " class='$class'";

    foreach ($attrs as $attr => $value) {
        $output .= " $attr='$value'";
    }

    $output .= ">";
    $output .= $content;
    $output .= "</div>"; // <- here something should be done: what? IDK
    return $output;
}

I tried some alternatives and the closest I could get is this code right before return $output;:

if (substr($output, -2) !== "\n") {
    $output .= "\n" . str_repeat("    ", $this->tabs + $tabcount - 1);
}

that renders:

<div class='table-row'>
                
    <div class='table-row-cell'>data21</div>
                
    <div class='table-row-cell'>data22</div>
                
    <div class='table-row-cell'>data23</div>

</div>

Any ideas or suggestions on how to do it will be greatly welcome.

Thanks in advance for your time.

PS: if further code/explanation is needed, let me know and will append it to the original question.

答案1

得分: 0

我通过向 getHtmlDiv() 添加一个新参数来解决这个问题。这个新参数是 $add_newline(默认为 false),是一个标志,用于指示何时添加新行和制表符。

函数:

private function getHtmlDiv(
    string $class,
    string $content = "",
    array $attrs = [],
    int $tabcount = 0,
    bool $add_newline = false
): string {
    $output = "\n" . str_repeat("    ", $this->tabs + $tabcount) . "<div";

    $output .= " class='$class'";

    foreach ($attrs as $attr => $value) {
        $output .= " $attr='$value'";
    }

    $output .= ">";
    $output .= $content;

    if ($add_newline) {
        $output .= "\n" . str_repeat("    ", $this->tabs + $tabcount);
    }

    $output .= "</div>";

    return $output;
}

调用:

// this output is correct
$row_build .= $this->getHtmlDiv(
                    $html_cell_class,
                    $cell_data ?? "",
                    $this->html_attributes['cell'],
                    3
                );

// and when I need a newline
$body_build .= $this->getHtmlDiv(
                $html_row_class,
                $row_build,
                $this->html_attributes['row'],
                2,
                true
            );
英文:

I solved it adding a new parameter to getHtmlDiv(). This new parameter, $add_newline (false by default) is a flag that tells when to add a new line and tabs.

The function:

private function getHtmlDiv(
    string $class,
    string $content = &quot;&quot;,
    array $attrs = [],
    int $tabcount = 0,
    bool $add_newline = false
): string {
    $output = &quot;\n&quot; . str_repeat(&quot;    &quot;, $this-&gt;tabs + $tabcount) . &quot;&lt;div&quot;;

    $output .= &quot; class=&#39;$class&#39;&quot;;

    foreach ($attrs as $attr =&gt; $value) {
        $output .= &quot; $attr=&#39;$value&#39;&quot;;
    }

    $output .= &quot;&gt;&quot;;
    $output .= $content;

    if ($add_newline) {
        $output .= &quot;\n&quot; . str_repeat(&quot;    &quot;, $this-&gt;tabs + $tabcount);
    }

    $output .= &quot;&lt;/div&gt;&quot;;

    return $output;
}

The call:

// this output is correct
$row_build .= $this-&gt;getHtmlDiv(
                        $html_cell_class,
                        $cell_data ?? &quot;&quot;,
                        $this-&gt;html_attributes[&#39;cell&#39;],
                        3
                    );

// and when I need a newline
$body_build .= $this-&gt;getHtmlDiv(
                $html_row_class,
                $row_build,
                $this-&gt;html_attributes[&#39;row&#39;],
                2,
                true
            );

huangapple
  • 本文由 发表于 2023年5月14日 06:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定