Zend_Text_Table.xml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.text.table.introduction">
  4. <title>Zend_Text_Table</title>
  5. <para>
  6. <classname>Zend_Text_Table</classname> is a component to create text based tables
  7. on the fly with different decorators. This can be helpful, if you either
  8. want to send structured data in text emails, which are used to have
  9. mono-spaced fonts, or to display table information in a CLI application.
  10. <classname>Zend_Text_Table</classname> supports multi-line columns, colspan and
  11. align as well.
  12. </para>
  13. <note>
  14. <title>Encoding</title>
  15. <para>
  16. <classname>Zend_Text_Table</classname> expects your strings to be UTF-8 encoded
  17. by default. If this is not the case, you can either supply the character
  18. encoding as a parameter to the <methodname>constructor()</methodname> or the
  19. <methodname>setContent()</methodname> method of
  20. <classname>Zend_Text_Table_Column</classname>. Alternatively if you have a different
  21. encoding in the entire process, you can define the standard input charset with
  22. <methodname>Zend_Text_Table::setInputCharset($charset)</methodname>. In
  23. case you need another output charset for the table, you can set
  24. this with <methodname>Zend_Text_Table::setOutputCharset($charset)</methodname>.
  25. </para>
  26. </note>
  27. <para>
  28. A <classname>Zend_Text_Table</classname> object consists of rows, which contain
  29. columns, represented by <classname>Zend_Text_Table_Row</classname> and
  30. <classname>Zend_Text_Table_Column</classname>. When creating a table, you can
  31. supply an array with options for the table. Those are:
  32. <itemizedlist>
  33. <listitem>
  34. <para>
  35. <property>columnWidths</property> (required): An array defining
  36. all columns width their widths in characters.
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. <property>decorator</property>: The decorator to use for the
  42. table borders. The default is <emphasis>unicode</emphasis>, but
  43. you may also specify <emphasis>ascii</emphasis> or give an instance
  44. of a custom decorator object.
  45. </para>
  46. </listitem>
  47. <listitem>
  48. <para>
  49. <property>padding</property>: The left and right padding withing
  50. the columns in characters. The default padding is zero.
  51. </para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. <property>AutoSeparate</property>: The way how the rows are
  56. separated with horizontal lines. The default is a
  57. separation between all rows. This is defined as a bitmask
  58. containing one ore more of the following constants of
  59. <classname>Zend_Text_Table</classname>:
  60. <itemizedlist>
  61. <listitem>
  62. <para><constant>Zend_Text_Table::AUTO_SEPARATE_NONE</constant></para>
  63. </listitem>
  64. <listitem>
  65. <para><constant>Zend_Text_Table::AUTO_SEPARATE_HEADER</constant></para>
  66. </listitem>
  67. <listitem>
  68. <para><constant>Zend_Text_Table::AUTO_SEPARATE_FOOTER</constant></para>
  69. </listitem>
  70. <listitem>
  71. <para><constant>Zend_Text_Table::AUTO_SEPARATE_ALL</constant></para>
  72. </listitem>
  73. </itemizedlist>
  74. Where header is always the first row, and the footer is
  75. always the last row.
  76. </para>
  77. </listitem>
  78. </itemizedlist>
  79. </para>
  80. <para>
  81. Rows are simply added to the table by creating a new instance of
  82. <classname>Zend_Text_Table_Row</classname>, and appending it to the table via the
  83. <methodname>appendRow()</methodname> method. Rows themselves have no options. You can also
  84. give an array to directly to the <methodname>appendRow()</methodname> method, which then
  85. will automatically converted to a row object, containing multiple column
  86. objects.
  87. </para>
  88. <para>
  89. The same way you can add columns to the rows. Create a new instance of
  90. <classname>Zend_Text_Table_Column</classname> and then either set the column
  91. options in the constructor or later with the <methodname>set*()</methodname> methods.
  92. The first parameter is the content of the column which may have
  93. multiple lines, which in the best case are separated by just the
  94. '\n' character. The second parameter defines the align, which
  95. is 'left' by default and can be one of the class constants of
  96. <classname>Zend_Text_Table_Column</classname>:
  97. <itemizedlist>
  98. <listitem>
  99. <para>
  100. <constant>ALIGN_LEFT</constant>
  101. </para>
  102. </listitem>
  103. <listitem>
  104. <para>
  105. <constant>ALIGN_CENTER</constant>
  106. </para>
  107. </listitem>
  108. <listitem>
  109. <para>
  110. <constant>ALIGN_RIGHT</constant>
  111. </para>
  112. </listitem>
  113. </itemizedlist>
  114. The third parameter is the colspan of the column. For example, when you
  115. choose "2" as colspan, the column will span over two columns of the table.
  116. The last parameter defines the encoding of the content, which should be
  117. supplied, if the content is neither ASCII nor UTF-8. To append the column
  118. to the row, you simply call <methodname>appendColumn()</methodname> in your row object
  119. with the column object as parameter. Alternatively you can directly
  120. give a string to the <methodname>appendColumn()</methodname> method.
  121. </para>
  122. <para>
  123. To finally render the table, you can either use the <methodname>render()</methodname>
  124. method of the table, or use the magic method <methodname>__toString()</methodname>
  125. by doing <command>echo $table;</command> or
  126. <command>$tableString = (string) $table</command>.
  127. </para>
  128. <example id="zend.text.table.example.using">
  129. <title>Using Zend_Text_Table</title>
  130. <para>
  131. This example illustrates the basic use of <classname>Zend_Text_Table</classname>
  132. to create a simple table:
  133. </para>
  134. <programlisting language="php"><![CDATA[
  135. $table = new Zend_Text_Table(array('columnWidths' => array(10, 20)));
  136. // Either simple
  137. $table->appendRow(array('Zend', 'Framework'));
  138. // Or verbose
  139. $row = new Zend_Text_Table_Row();
  140. $row->appendColumn(new Zend_Text_Table_Column('Zend'));
  141. $row->appendColumn(new Zend_Text_Table_Column('Framework'));
  142. $table->appendRow($row);
  143. echo $table;
  144. ]]></programlisting>
  145. <para>
  146. This will result in the following output:
  147. </para>
  148. <programlisting language="text"><![CDATA[
  149. ┌──────────┬────────────────────┐
  150. │Zend │Framework │
  151. └──────────┴────────────────────┘
  152. ]]></programlisting>
  153. </example>
  154. </sect1>
  155. <!--
  156. vim:se ts=4 sw=4 et:
  157. -->