0 レビュー
2 回答
PHP:htmlコンテナヘルパーを作成する方法は?
たとえば、さまざまなコンテンツを何度も入れたい、非常に具体的なフォーマットのリストがあります。
だから私は関数を持っているかもしれません:
<?
function fancy_container (contents_array) {
<?
<ul>
<? for($contents_array as $content) { ?>
<li class='special'><? echo $content ?><div class='other-specials'></div></li>
<? } ?>
</ul>
?>
}
?>
それは機能しますが、私はそれを次のように呼びたいと思います:
<?
fancy_container(array(?>
<div class='whatever'>hi there</div>
<div class='whatever'>hi there</div>
<div class='whatever'>hi there</div>
<?), ?>
<div class='other-junk'>hiya</div>
<div class='other-junk'>hiya</div>
<div class='other-junk'>hiya</div>
<?))
?>
ヒアドキュメントでこれを行う方法を理解しましたが、それはちょっと厄介なようです。私はこれを間違った方法で行っていますか?私はPHPの人ではないので、物事や制限を行う通常の方法に精通していません。私はrubyのyieldを使用してこれを行う方法を知っていますが、phpではわかりません。
HTMLコンテンツを1つまたは複数のコンテナに挿入したいだけで、ヒアドキュメントテキストではなくhtmlをhtmlにしたいのです。
ありがとう
わからない
0
レビュー
答え :
解決策:
マーティン・ラインが述べたように、それは少し逆行していて、奇妙なやり方だと思います。最終的な使用法の完全な範囲を意図的に示していないため、おそらくもっとそうです。これがあなたのコードをクリーンアップし、もう少し正気にしたものです。構文エラーがあり、関数の呼び出し方法など、PHPでは許可されていないものがいくつかあります。
<?php
function fancy_container ($contents_array) {
if (!is_array($contents_array) || empty($contents_array)) {
return '';
}
$output = '';
$output .= '<ul>'.PHP_EOL;
foreach ($contents_array as $content) {
$output .= '<li class="special">'.$content.'<div class="other-specials"></div></li>'.PHP_EOL;
}
$output .= '</ul>'.PHP_EOL;
return $output;
}
$contents = array();
ob_start();
?>
<div class="whatever">hi there</div>
<div class="whatever">hi there</div>
<div class="whatever">hi there</div>
<?php
$contents[] = ob_get_contents();
ob_end_clean();
ob_start();
?>
<div class="other-junk">hiya</div>
<div class="other-junk">hiya</div>
<div class="other-junk">hiya</div>
<?php
$contents[] = ob_get_contents();
ob_end_clean();
echo fancy_container($contents);
?>
出力マークアップ
<ul>
<li class="special">
<div class="whatever">hi there</div>
<div class="whatever">hi there</div>
<div class="whatever">hi there</div>
<div class="other-specials"></div></li>
<li class="special">
<div class="other-junk">hiya</div>
<div class="other-junk">hiya</div>
<div class="other-junk">hiya</div>
<div class="other-specials"></div></li>
</ul>
わからない
0
レビュー
答え :
解決策:
php関数ではなく、divタグをそこに配置しないのはなぜですか? PHPの影響を受けるスタイルが必要な場合は、クラス <div class="<?php echo getClass();?>">..content..</div>
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。