Свежайшие Пирожки от CakePHP по-русски

Полнейшее руководство CakePHP 1.2 на русском языке, горячие новости и полезные статьи

Inserting Well-Formatted elements

The most important task the HtmlHelper accomplishes is creating well formed markup. Don't be afraid to use it often - you can cache views in CakePHP in order to save some CPU cycles when views are being rendered and delivered. This section will cover some of the methods of the HtmlHelper and how to use them.

charset(string $charset=null)

Used to create a meta tag specifying the document's character. Defaults to UTF-8.

Простой текст
  1. <?php echo $html->charset(); ?>
  2. //Output
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <?php echo $html->charset('ISO-8859-1'); ?>
  5. //Output
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

css(mixed $path, string $rel = null, array $htmlAttributes = array(), boolean $inline = true)

Creates a link(s) to a CSS style-sheet. If $inline is set to false, the link tags are added to the $scripts_for_layout variable which you can print inside the head tag of the document.

This method of CSS inclusion assumes that the CSS file specified resides inside the /app/webroot/css directory.

Простой текст
  1. <?php echo $html->css('forms'); ?>
  2. //Output
  3. <link rel="stylesheet" type="text/css" href="/test/css/forms.css" />
  4. //Also accepts arrays in the first parameter
  5. <?php echo $html->css(array('forms','tables','menu')); ?>
  6. //Output
  7. <link rel="stylesheet" type="text/css" href="/test/css/forms.css" />
  8. <link rel="stylesheet" type="text/css" href="/test/css/tables.css" />
  9. <link rel="stylesheet" type="text/css" href="/test/css/menu.css" />

meta(string $type, string $url = null, array $attributes = array(), boolean $inline = true)

This method is handy for linking to external resources like RSS/Atom feeds and favicons. Like css(), you can specify whether or not you'd like this tag to appear inline or in the head tag using the fourth parameter.

Use the "type" attribute to control type tag to be generated:

This method is handy for linking to external resources like RSS/Atom feeds and favicons. Like css(), you can specify whether or not you'd like this tag to appear inline or in the head tag using the fourth parameter.

If you set the "type" attribute using the $htmlAttributes parameter, CakePHP contains a few shortcuts:

type translated value
html text/html
rss application/rss+xml
atom application/atom+xml
icon image/x-icon
Простой текст
  1. <?php echo $html->meta(
  2. 'favicon.ico',
  3. '/favicon.ico',
  4. array('type' => 'icon')
  5. );?> //Output (line breaks added) </p>
  6. <link
  7. href="http://example.com/favicon.ico"
  8. title="favicon.ico" type="image/x-icon"
  9. rel="alternate"
  10. />
  11. <?php echo $html->meta(
  12. 'Comments',
  13. '/comments/index.rss',
  14. array('type' => 'rss'));
  15. ?>
  16. //Output (line breaks added)
  17. <link
  18. href="http://example.com/comments/index.rss"
  19. title="Comments"
  20. type="application/rss+xml"
  21. rel="alternate"
  22. />

This method can also be used to add the meta keywords and descriptions. Example:

Простой текст
  1. <?php echo $html->meta(
  2. 'keywords',
  3. 'enter any meta keyword here',
  4. array(), false
  5. );?>
  6. //Output <meta name="keywords" content="enter any meta keyword here"/>
  7. //
  8. <?php echo $html->meta(
  9. 'description',
  10. 'enter any meta description here',
  11. array(), false
  12. );?>
  13. //Output <meta name="description" content="enter any meta description here"/>

docType(string $type = 'xhtml-strict')

Prints out a (X)HTML doctype tag. Supply the doctype according to the following table:

type translated value
html text/html
html4-strict HTML4 Strict
html4-trans HTML4 Transitional
html4-frame HTML4 Frameset
xhtml-strict XHTML1 Strict
xhtml-trans XHTML1 Transitional
xhtml-frame XHTML1 Frameset
xhtml11 XHTML 1.1
Простой текст
  1. <?php echo $html->docType(); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <?php echo $html->docType('html4-trans'); ?>
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

style(array $data, boolean $inline = true)

Builds CSS style definitions based on the keys and values of the array passed to the method. Especially handy if your CSS file is dynamic.

Простой текст
  1. <?php echo $html->style(array(
  2. 'background' => '#633',
  3. 'border-bottom' => '1px solid #000',
  4. 'padding' => '10px'
  5. )); ?>
  6.  
  7. //Output
  8. background:#633;
  9. border-bottom:1px solid #000;
  10. padding:10px;

image(string $path, array $htmlAttributes = array())

Creates a formatted image tag. The path supplied should be relative to /app/webroot/img/.

Простой текст
  1. <?php echo $html->image('cake_logo.png', array('alt' => 'CakePHP'))?>
  2. //Output
  3. <img src="/img/cake_logo.png" alt="CakePHP" />

div(string $class, string $text, array $htmlAttributes, boolean $escape = false)

Used for creating div-wrapped sections of markup. The first parameter specifies a CSS class, and the second is used to supply the text to be wrapped by div tags. If the last parameter has been set to true, $text will be printed HTML-escaped.

If no text is specified, only an opening div tag is returned.

Простой текст
  1. <?php echo $html->div('error', 'Please enter your credit card number.');?>
  2.  
  3. //Output
  4. <div class="error">Please enter your credit card number.</div>

link(string $title, mixed $url = null, array $htmlAttributes = array(), string $confirmMessage = false, boolean $escapeTitle = true)

General purpose method for creating HTML links.

Простой текст
  1. <?php echo $html->link('Enter', '/pages/home', array('class'=>'button')); ?>
  2. //Output
  3. <a href="/pages/home" class="button">Enter</a>
  4. <?php echo $html->link(
  5. 'Delete',
  6. array('controller'=>'recipes', 'action'=>'delete', 6),
  7. array(),
  8. "Are you sure you wish to delete this recipe?"
  9. );?>
  10. //Output
  11. <a href="/recipes/delete/6" onclick="return confirm('Are you sure you wish to delete this recipe?');">Delete</a>
  12. //Query strings can also be created with link()
  13. <?php echo $html->link('View image', array(
  14. 'controller' => 'images',
  15. 'action' => 'view',
  16. 1,
  17. '?' => array( 'height' => 400, 'width' => 500))
  18. );
  19. //Output
  20. <a href="/images/view/1?height=400&width=500">View image</a>

There are two ways you can create an image link, the first is by using the url option of $html->image() :

Простой текст
  1. <?php echo $html->image("recipes/6.jpg", array(
  2. "alt" => "Brownies",
  3. 'url' => array('controller' => 'recipies', 'action' => 'view', 6)
  4. )); ?>

The second is to genenerate an image, pass it to the link method specifying not to escape the title:

Простой текст
  1. <?php echo $html->link(
  2. $html->image("recipes/6.jpg", array("alt" => "Brownies")),
  3. "recipes/view/6",
  4. null, null, false
  5. );
  6. ?>

Both of which will produce the same output:

<a href="/recipes/view/6">
    <img src="/img/recipes/6.jpg" alt="Brownies" />
</a>

You would use the second compound-call if you need to add other html parameters to your link, such as an id or class.

para(string $class, string $text, array $htmlAttributes, boolean $escape = false)

Returns a text wrapped in a CSS-classed <p> tag. If no text is supplied, only a starting <p> tag is returned. Простой текст

  1. <?php echo $html->para(null, 'Hello World.');?>
  2. //Output
  3. <p>Hello World.</p>

tableHeaders(array $names, array $trOptions = null, array $thOptions = null)

Creates a row of table header cells to be placed inside of <table> tags. Простой текст

  1. <?php echo $html->tableHeaders(array('Date','Title','Active'));?> //Output
  2. <tr><th>Date</th><th>Title</th><th>Active</th></tr>
  3. <?php echo $html->tableHeaders(
  4. array('Date','Title','Active'),
  5. array('class' => 'status'),
  6. array('class' => 'product_table')
  7. );?>
  8. //Output
  9. <tr class="status">
  10. <th class="product_table">Date</th>
  11. <th class="product_table">Title</th>
  12. <th class="product_table">Active</th>
  13. </tr>

tableCells(array $data, array $oddTrOptions = null, array $evenTrOptions = null)

Creates table cells, in rows, assigning <tr> attributes differently for odd- and even-numbered rows. Wrap a single table cell within an array() for specific <td>-attributes.

Простой текст
  1. <?php echo $html->tableCells(array(
  2. array('Jul 7th, 2007', 'Best Brownies', 'Yes'),
  3. array('Jun 21st, 2007', 'Smart Cookies', 'Yes'),
  4. array('Aug 1st, 2006', 'Anti-Java Cake', 'No'),
  5. ));
  6. ?>
  7. //Output
  8. <tr><td>Jul 7th, 2007</td><td>Best Brownies</td><td>Yes</td></tr>
  9. <tr><td>Jun 21st, 2007</td><td>Smart Cookies</td><td>Yes</td></tr>
  10. <tr><td>Aug 1st, 2006</td><td>Anti-Java Cake</td><td>No</td></tr>
  11. <?php echo $html->tableCells(array(
  12. array('Jul 7th, 2007', array('Best Brownies', array('class'=>'highlight')) , 'Yes'),
  13. array('Jun 21st, 2007', 'Smart Cookies', 'Yes'),
  14. array('Aug 1st, 2006', 'Anti-Java Cake', array('No', array('id'=>'special'))),
  15. ));
  16. ?>
  17. //Output
  18. <tr><td>Jul 7th, 2007</td><td class="highlight">Best Brownies</td><td>Yes</td></tr>
  19. <tr><td>Jun 21st, 2007</td><td>Smart Cookies</td><td>Yes</td></tr>
  20. <tr><td>Aug 1st, 2006</td><td>Anti-Java Cake</td><td id="special">No</td></tr>
  21. <?php echo $html->tableCells(
  22. array(
  23. array('Red', 'Apple'),
  24. array('Orange', 'Orange'),
  25. array('Yellow', 'Banana'),
  26. ),
  27. array('class' => 'darker')
  28. );
  29. ?>
  30. //Output
  31. <tr class="darker"><td>Red</td><td>Apple</td></tr>
  32. <tr><td>Orange</td><td>Orange</td></tr>
  33. <tr class="darker"><td>Yellow</td><td>Banana</td></tr>