PHP HTML Template with Loop capabilities

前端 未结 3 506
故里飘歌
故里飘歌 2021-01-07 07:28

I would like to ask some help and ideas on how to implement a loop inside the template. I can do foearch below but how can i include it to the template and show it in the re

相关标签:
3条回答
  • 2021-01-07 07:37

    Your PHP code:

    $htmldata ="";
    
    ($results as $row) {
     $name = $row['name'];
     $address = $row['address'];
    $htmldata .="
    <tr><td>
    <h3>Hello William!</h3>
    <p>The time is: 03/10/04</p>
    <p>Embedded PHP works too!</p>
    <p>".$name."</p>
    <p>".$address." </p>
    </td>
    </tr>
    ";
    }
    

    Then in your template design.html, you will pass the $htmltable variable and embedd there:

    <html>
    <head>
    <title>#title#</title>
    </head>
    <body>
    <h3>Hello #name#!</h3>
    <p>The time is: #datetime#</p>
    <? echo "<p>Embedded PHP works too!</p>"; ?>
    <table>
    <?php echo $htmltable; ?>
    </table>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-07 07:46

    Just don't re-invent the wheel, PHP works wonderfully as a templating language:

    The template class

    <?
    class Template
    {
       private $template;
       private $vars;
       function load($filepath) {
          $this->template = $filepath;
       }
       function replace($var, $content)
       {
          $this->vars[$var] = $content;
       }
       function publish()
       {
           extract($this->vars);
           include($this->template);
       }
    }
    ?>
    

    The template design.phtml

    <html>
    <head>
    <title><?php echo $title ?></title>
    </head>
    <body>
    <?php foreach($rows as $row) { extract($row); ?>
      <h3>Hello <?php echo $name; ?></h3>
      <p>The time is: <?php echo $datetime; ?></p>
      <?php echo "<p>Embedded PHP works too!</p>"; ?>
    <?php } ?>
    </body>
    </html>
    

    The use is pretty much the same, just assign more than one row to make use of it:

    <?
    include "template.class.php";
    $template = new Template;
    $template->load("design.phtml");
    $template->replace("title", "My Template Class");
    $rows = array();
    $rows[] = array(
        "name" => "William",
        "datetime" => date("m/d/y"),
    );
    $template->replace("rows", $rows);
    $template->publish();
    ?>
    

    Hope this is helpful.

    0 讨论(0)
  • 2021-01-07 07:52

    PHP itself is as good at templates as any other engine.
    No need anything else

    $pagetitle = "My Template Class";
    foreach($results as $row) {
      $row['date'] = date("m/d/y");
      $data[] = $row;
    }
    $data = chunk_split($data,3);    
    

    Then in template

    <html>
    <head>
    <title><?=$pagetitle?></title>
    </head>
    <body>
     <table>
    <?php foreach ($data as $chunk): ?>
      <tr>
    <?php foreach ($chunk as $row): ?>
       <td>
        <h3>Hello <?=$name?>!</h3>
        <p>The time is: <?=$date?></p>
        <p>Embedded PHP works in the template</p>
        <p><b>But embed PHP in the data is a VERY BAD IDEA</b></p>
        <p><?=$address?></p>
       </td>
    <?php endforeach ?>
      </tr>
    <?php endforeach ?>
     </table>
    </body>
    </html>
    

    I made your example a bit more complicated yet closer to the real life.
    It will print your table in the rows by 3 columns in each

    0 讨论(0)
提交回复
热议问题