改进HTML和PHP的组合?

Is there a better solution, apart from a template engine like smarty, for huge code like this?

<table class="table table-striped dataTable table-bordered">
    <thead>
        <tr>
            <?php
            $output  = '<th class="sorting_numeric_html sorting_default_desc">' . TH_ORDERS_ID . '</th>';
            $output .= '<th class="sorting_date_eu">' . TH_ORDERS_DATE . '</th>';
            $output .= '<th>' . TH_ORDERS_NAME . '</th>';
            $output .= '<th>' . TH_ORDERS_STATUS . '</th>';
            $output .= '<th class="sorting_disabled">' . TH_ACTION . '</th>';
            echo $output;
            ?>
        </tr>
     </thead>
     <tbody>
        <?php
            $output = '';
            foreach ($orders['RESULT'] as $order) {
                $output .= '<tr>';
                $output .= '<td class="text-right">' . inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']) . '</td>';
                $output .= '<td>' . inc_datetime_short($order['date_purchased']) . '</td>';
                $output .= '<td>' . inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id']. '&action=edit'), $order['delivery_name']) . '</td>';
                $output .= '<td class="status' . $order['orders_status'] . '">' . $order['orders_status_name'] . '</td>';
                $output .= '<td class="btn-group actions">';
                $output .= inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
                inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"');
                $output .= modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"');
                $output .= '</td>';
                $output .= '</tr>';
            }
            echo $output;
        ?>
     </tbody>
</table>

For sure Smarty would be a solution, but is there another way?

Yes, you normally want to separate the most you can PHP and HTML, leaving the html alone, like this:

<table class="table table-striped dataTable table-bordered">
  <thead>
    <tr>
      <th class="sorting_numeric_html sorting_default_desc">
        <?php echo TH_ORDERS_ID; ?>
      </th>
      <th class="sorting_date_eu">
        <?php echo TH_ORDERS_DATE; ?>
      </th>
      ...

If your system supports it, you can try instead changing the <?php echo for <?=, which is known as short tags as read in the manual, in which case the code would look much neater:

<table class="table table-striped dataTable table-bordered">
  <thead>
    <tr>
      <th class="sorting_numeric_html sorting_default_desc">
        <?= TH_ORDERS_ID; ?>
      </th>
      <th class="sorting_date_eu">
        <?= TH_ORDERS_DATE; ?>
      </th>
      ...

However, a couple of notes that would require you to change your code further down. They might not be for now, but they more for your future code:

  • Why are you echoing constants? Normally you'd like to store your data in variables and then echo them.
  • It's better normally to avoid putting too many classes in the html since it's the CSS what gets cached better normally. I'd go with an id="tableorders" and then simply class="id", class="date" and so on.

I'd do it like this, makes it easier to read as the HTML is a little bit more separated from the PHP:

<table class="table table-striped dataTable table-bordered">
<thead>
<tr>
    <th class="sorting_numeric_html sorting_default_desc"><?php echo TH_ORDERS_ID; ?></th>
    <th class="sorting_date_eu"><?php echo TH_ORDERS_DATE; ?></th>
    <th><?php echo TH_ORDERS_NAME; ?></th>
    <th><?php echo TH_ORDERS_STATUS; ?></th>
    <th class="sorting_disabled"><?php echo TH_ACTION; ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($orders['RESULT'] as $order): ?>
    <tr>
        <td class="text-right"><?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'), $order['orders_id']); ?></td>
        <td><?php echo inc_datetime_short($order['date_purchased']); ?></td>
        <td><?php echo inc_buildLink(inc_url(FILENAME_CUSTOMERS, 'cID=' . $order['customers_id'] . '&action=edit'), $order['delivery_name']); ?></td>
        <td class="status<?php echo $order['orders_status']; ?>"><?php echo $order['orders_status_name']; ?></td>
        <td class="btn-group actions">
            <?php echo inc_buildLink(inc_url(FILENAME_ORDERS, 'oID=' . $order['orders_id'] . '&action=edit'),
                inc_image(DIR_WS_ICONS . 'edit.png', ''), TOOLTIP_EDIT, 'class="tip btn btn-mini"'),
            modal_delete_orders($order['customers_name'], $order['orders_id'], 'class="tip btn btn-mini"'); ?>
        </td>
    </tr>
<?php endforeach; ?>
</tbody>

Instead of <php echo you can also use <?=, but lots of people dislike the use of it. It's only 4 more characters to put echo so i'd stick with that!