0

このような巨大なコードに対して、smarty のようなテンプレート エンジン以外に、より良い解決策はありますか?

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

確かに Smarty は解決策ですが、別の方法はありますか?

4

2 に答える 2

3

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

代わりに<php echoを使用することもできますが<?=、多くの人はその使用を嫌います。エコーを追加できるのはあと 4 文字だけなので、そのままにしておきます。

于 2013-10-12T14:25:26.820 に答える
2

はい、通常、次のように、HTML だけを残して、できる限り PHP と HTML を分離する必要があります。

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

お使いのシステムがサポートしている場合は、代わりに<?php echoforを変更してみてください。これはマニュアル<?=で読む短いタグとして知られています。その場合、コードはよりきれいに見えます:

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

ただし、コードをさらに下に変更する必要があるいくつかの注意事項があります。それらは今のところそうではないかもしれませんが、将来のコードのためのものです:

  • なぜあなたは定数をエコーし​​ているのですか? 通常、データを変数に保存してから、それらをエコーし​​ます。
  • 通常、キャッシュされるのは CSS であるため、html に多くのクラスを配置しないようにすることをお勧めします。私はid="tableorders"、そして単純class="id"class="date"などを使います。
于 2013-10-12T14:23:59.643 に答える