简体   繁体   中英

PHP: How to create html container helper?

For example, I have a list with very specific formatting that I want to put different content in over and over.

So I might have a function:

<?
function fancy_container (contents_array) {
  <?
    <ul>
      <? for($contents_array as $content) { ?>
        <li class='special'><? echo $content ?><div class='other-specials'></div></li>
      <? } ?>
    </ul>
  ?>
}
?>

That works, but I want to call it like this:

<?
  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>
  <?))
?>

I figured out how to do this with a heredoc but that seems sort of nasty. Am I going about this the wrong way? I'm not a php guy so I'm not familiar with the normal way of doing things or limitations. I know how to do this using ruby yield, but no idea in php.

I just want to inject html content into a container (or containers) and I want to have html be html, not heredoc text.

Thanks

Why would you not just put the div tag there, instead of in your php function? If you must have style affected by PHP I'd recommend a PHP function that spits out a class <div class="<?php echo getClass();?>">..content..</div>

Like Martin Lyne mentioned, it's a bit backwards and I guess an odd way of doing things. Probably more so because your intentionally not showing the full extent of the final usage. Here's your code cleaned up and made to be a bit more sane. You had syntax errors and a few stuff is just not allowed in PHP like how your calling the function.

<?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);

?>

Outputs markup

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM