改进HTML和PHP的组合?

时间:2013-10-12 14:19:04

标签: php html

除了像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会是一个解决方案,但还有另一种方法吗?

2 个答案:

答案 0 :(得分: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个字符才能放回音,所以我坚持使用它!

答案 1 :(得分:2)

是的,您通常希望将PHP和HTML最多分开,只留下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 echo的{​​{1}},这在the manual中称为短标记,在这种情况下,代码看起来很多整洁:

<?=

但是,有几个注意事项需要您进一步更改代码。它们可能不是现在,但它们更适合您未来的代码:

  • 为什么要回显常量?通常,您希望将数据存储在变量中,然后回显它们。
  • 通常最好避免在html中放入太多的类,因为CSS通常会更好地缓存。我会选择<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> ... ,然后只需id="tableorders"class="id",依此类推。