英文:
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 = "",
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;
}
The call:
// 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
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论