使用smarty模板引擎从对象获取数组值

时间:2013-11-01 21:17:12

标签: php smarty template-engine

我无法弄清楚如何获取属性。这将被发送到我的智能模板。

object(Order)#19 (7) {
  ["order_id"]=>
  string(5) "10248"
  ["customer"]=>
  object(Customer)#28 (5) {
    ["customer_id"]=>
    string(5) "VINET"
    ["contact_name"]=>
    string(12) "Paul Henriot"
    ["company_name"]=>
    string(25) "Vins et alcools Chevalier"
    ["city"]=>
    NULL
    ["phone"]=>
    string(11) "26.47.15.10"
  }
  ["order_date"]=>
  string(19) "1996-07-04 00:00:00"
  ["shipped_date"]=>
  string(19) "1996-07-16 00:00:00"
  ["shipper"]=>
  object(Shipper)#30 (3) {
    ["shipper_id"]=>
    string(1) "3"
    ["shipper_name"]=>
    string(16) "Federal Shipping"
    ["shipper_phone"]=>
    string(14) "(503) 555-9931"
  }
  ["shipped_name"]=>
  string(25) "Vins et alcools Chevalier"
  ["order_detail"]=>
  array(3) {
    [0]=>
    object(OrderDetail)#32 (4) {
      ["order_id"]=>
      string(5) "10248"
      ["product"]=>
      object(Product)#35 (11) {
        ["product_id"]=>
        string(2) "11"
        ["product_name"]=>
        string(14) "Queso Cabrales"
        ["price"]=>
        string(7) "21.0000"
        ["supplier"]=>
        object(Supplier)#38 (6) {
          ["company_name"]=>
          string(34) "Cooperativa de Quesos 'Las Cabras'"
          ["address"]=>
          string(17) "Calle del Rosal 4"
          ["phone"]=>
          string(14) "(98) 598 76 54"
          ["city"]=>
          NULL
          ["contact_name"]=>
          string(27) "Antonio del Valle Saavedra "
          ["contact_title"]=>
          string(20) "Export Administrator"
        }
        ["units_in_stock"]=>
        string(2) "22"
        ["units_on_order"]=>
        string(2) "30"
        ["reorder_level"]=>
        string(2) "30"
        ["category_id"]=>
        string(1) "4"
        ["qty_per_unit"]=>
        string(9) "1 kg pkg."
        ["discontinued"]=>
        string(1) "0"
        ["supplier_id"]=>
        string(1) "5"
      }
      ["qty"]=>
      string(2) "12"
      ["discount"]=>
      string(1) "0"
    }
    [1]=>
    object(OrderDetail)#29 (4) {
      ["order_id"]=>
      string(5) "10248"
      ["product"]=>
      object(Product)#36 (11) {
        ["product_id"]=>
        string(2) "42"
        ["product_name"]=>
        string(29) "Singaporean Hokkien Fried Mee"
        ["price"]=>
        string(7) "14.0000"
        ["supplier"]=>
        object(Supplier)#41 (6) {
          ["company_name"]=>
          string(12) "Leka Trading"
          ["address"]=>
          string(30) "471 Serangoon Loop, Suite #402"
          ["phone"]=>
          string(8) "555-8787"
          ["city"]=>
          NULL
          ["contact_name"]=>
          string(12) "Chandra Leka"
          ["contact_title"]=>
          string(5) "Owner"
        }
        ["units_in_stock"]=>
        string(2) "26"
        ["units_on_order"]=>
        string(1) "0"
        ["reorder_level"]=>
        string(1) "0"
        ["category_id"]=>
        string(1) "5"
        ["qty_per_unit"]=>
        string(15) "32 - 1 kg pkgs."
        ["discontinued"]=>
        string(1) "1"
        ["supplier_id"]=>
        string(2) "20"
      }
      ["qty"]=>
      string(2) "10"
      ["discount"]=>
      string(1) "0"
    }
    [2]=>
    object(OrderDetail)#34 (4) {
      ["order_id"]=>
      string(5) "10248"
      ["product"]=>
      object(Product)#40 (11) {
        ["product_id"]=>
        string(2) "72"
        ["product_name"]=>
        string(22) "Mozzarella di Giovanni"
        ["price"]=>
        string(7) "34.8000"
        ["supplier"]=>
        object(Supplier)#44 (6) {
          ["company_name"]=>
          string(23) "Formaggi Fortini s.r.l."
          ["address"]=>
          string(15) "Viale Dante, 75"
          ["phone"]=>
          string(12) "(0544) 60323"
          ["city"]=>
          NULL
          ["contact_name"]=>
          string(10) "Elio Rossi"
          ["contact_title"]=>
          string(20) "Sales Representative"
        }
        ["units_in_stock"]=>
        string(2) "14"
        ["units_on_order"]=>
        string(1) "0"
        ["reorder_level"]=>
        string(1) "0"
        ["category_id"]=>
        string(1) "4"
        ["qty_per_unit"]=>
        string(16) "24 - 200 g pkgs."
        ["discontinued"]=>
        string(1) "0"
        ["supplier_id"]=>
        string(2) "14"
      }
      ["qty"]=>
      string(1) "5"
      ["discount"]=>
      string(1) "0"
    }
  }

这就是我在smarty模板中所拥有的,并且抛出的错误表示尝试非对象的属性。

    <tr>
         <td>{$result->order_id}</td><!--Works -->
             <td>{$result->order_date}</td><!--Works -->
             <td>{$result->customer->company_name}</td><!--Works -->

        <!--Doesn't Work, says trying to get property of non object-->
        {foreach $result as $r}
              <td>{$r->order_detail->product->product_id}</td>
        {/foreach}
     </tr>

任何帮助都将不胜感激,谢谢。

0 个答案:

没有答案