html.php
来自「Cake Framwork , Excellent」· PHP 代码 · 共 838 行 · 第 1/2 页
PHP
838 行
* Creates a formatted IMG element. * * @param string $path Path to the image file, relative to the app/webroot/img/ directory. * @param array $htmlAttributes Array of HTML attributes. * @return string */ function image($path, $options = array()) { if (is_array($path)) { $path = Router::url($path); } elseif ($path{0} === '/') { $path = $this->webroot($path); } elseif (strpos($path, '://') !== false) { $path = $path; } else { if (Configure::read('Asset.timestamp') == true && Configure::read() > 0) { $path .= '?' . @filemtime(str_replace('/', DS, WWW_ROOT . IMAGES_URL . $path)); } $path = $this->webroot(IMAGES_URL . $path); } if (!isset($options['alt'])) { $options['alt'] = ''; } $url = false; if (!empty($options['url'])) { $url = $options['url']; unset($options['url']); } $image = sprintf($this->tags['image'], $path, $this->_parseAttributes($options, null, '', ' ')); if ($url) { return $this->output(sprintf($this->tags['link'], $this->url($url), null, $image)); } return $this->output($image); }/** * Returns a row of formatted and named TABLE headers. * * @param array $names Array of tablenames. * @param array $trOptions HTML options for TR elements. * @param array $thOptions HTML options for TH elements. * @return string */ function tableHeaders($names, $trOptions = null, $thOptions = null) { $out = array(); foreach ($names as $arg) { $out[] = sprintf($this->tags['tableheader'], $this->_parseAttributes($thOptions), $arg); } $data = sprintf($this->tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out)); return $this->output($data); }/** * Returns a formatted string of table rows (TR's with TD's in them). * * @param array $data Array of table data * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used * @param array $evenTrOptions HTML options for even TR elements * @param bool $useCount adds class "column-$i" * @param bool $continueOddEven If false, will use a non-static $count variable, so that the odd/even count is reset to zero just for that call * @return string Formatted HTML */ function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) { if (empty($data[0]) || !is_array($data[0])) { $data = array($data); } if ($oddTrOptions === true) { $useCount = true; $oddTrOptions = null; } if ($evenTrOptions === false) { $continueOddEven = false; $evenTrOptions = null; } if ($continueOddEven) { static $count = 0; } else { $count = 0; } foreach ($data as $line) { $count++; $cellsOut = array(); $i = 0; foreach ($line as $cell) { $cellOptions = array(); if (is_array($cell)) { $cellOptions = $cell[1]; $cell = $cell[0]; } elseif ($useCount) { $cellOptions['class'] = 'column-' . ++$i; } $cellsOut[] = sprintf($this->tags['tablecell'], $this->_parseAttributes($cellOptions), $cell); } $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions); $out[] = sprintf($this->tags['tablerow'], $options, join(' ', $cellsOut)); } return $this->output(join("\n", $out)); }/** * Returns a formatted block tag, i.e DIV, SPAN, P. * * @param string $name Tag name. * @param string $text String content that will appear inside the div element. * If null, only a start tag will be printed * @param array $attributes Additional HTML attributes of the DIV tag * @param boolean $escape If true, $text will be HTML-escaped * @return string The formatted tag element */ function tag($name, $text = null, $attributes = array(), $escape = false) { if ($escape) { $text = h($text); } if (!is_array($attributes)) { $attributes = array('class' => $attributes); } if ($text === null) { $tag = 'tagstart'; } else { $tag = 'tag'; } return $this->output(sprintf($this->tags[$tag], $name, $this->_parseAttributes($attributes, null, ' ', ''), $text, $name)); }/** * Returns a formatted DIV tag for HTML FORMs. * * @param string $class CSS class name of the div element. * @param string $text String content that will appear inside the div element. * If null, only a start tag will be printed * @param array $attributes Additional HTML attributes of the DIV tag * @param boolean $escape If true, $text will be HTML-escaped * @return string The formatted DIV element */ function div($class = null, $text = null, $attributes = array(), $escape = false) { if ($class != null && !empty($class)) { $attributes['class'] = $class; } return $this->tag('div', $text, $attributes, $escape); }/** * Returns a formatted P tag. * * @param string $class CSS class name of the p element. * @param string $text String content that will appear inside the p element. * @param array $attributes Additional HTML attributes of the P tag * @param boolean $escape If true, $text will be HTML-escaped * @return string The formatted P element */ function para($class, $text, $attributes = array(), $escape = false) { if ($escape) { $text = h($text); } if ($class != null && !empty($class)) { $attributes['class'] = $class; } if ($text === null) { $tag = 'parastart'; } else { $tag = 'para'; } return $this->output(sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $text)); }/** * Build a nested list (UL/OL) out of an associative array. * * @param array $list Set of elements to list * @param array $attributes Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag * @param array $itemAttributes Additional HTML attributes of the list item (LI) tag * @param string $tag Type of list tag to use (ol/ul) * @return string The nested list * @access public */ function nestedList($list, $attributes = array(), $itemAttributes = array(), $tag = 'ul') { if (is_string($attributes)) { $tag = $attributes; $attributes = array(); } $items = $this->__nestedListItem($list, $attributes, $itemAttributes, $tag); return sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $items); }/** * Internal function to build a nested list (UL/OL) out of an associative array. * * @param array $list Set of elements to list * @param array $attributes Additional HTML attributes of the list (ol/ul) tag * @param array $itemAttributes Additional HTML attributes of the list item (LI) tag * @param string $tag Type of list tag to use (ol/ul) * @return string The nested list element * @access private * @see nestedList() */ function __nestedListItem($items, $attributes, $itemAttributes, $tag) { $out = ''; $index = 1; foreach($items as $key => $item) { if (is_array($item)) { $item = $key . $this->nestedList($item, $attributes, $itemAttributes, $tag); } if (isset($itemAttributes['even']) && $index % 2 == 0) { $itemAttributes['class'] = $itemAttributes['even']; } else if (isset($itemAttributes['odd']) && $index % 2 != 0) { $itemAttributes['class'] = $itemAttributes['odd']; } $out .= sprintf($this->tags['li'], $this->_parseAttributes(array_diff_key($itemAttributes, array_flip(array('even', 'odd'))), null, ' ', ''), $item); $index++; } return $out; }/** * Creates a password input widget. * * @deprecated 1.2.0.5147 * @see FormHelper::input or FormHelper::password * @codeCoverageIgnoreStart */ function password($fieldName, $htmlAttributes = array()) { trigger_error(sprintf(__('Method password() is deprecated in %s: see FormHelper::input or FormHelper::password', true), get_class($this)), E_USER_NOTICE); $htmlAttributes = $this->value($htmlAttributes, $fieldName); $htmlAttributes = $this->domId($htmlAttributes); if ($this->tagIsInvalid()) { $htmlAttributes = $this->addClass($htmlAttributes, 'form_error'); } return $this->output(sprintf($this->tags['password'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); }/** * Creates a set of radio widgets. * * @deprecated */ function radio($fieldName, $options, $inbetween = null, $htmlAttributes = array()) { trigger_error(__('(HtmlHelper::radio) Deprecated: Use FormHelper::radio instead', true), E_USER_WARNING); $this->setEntity($fieldName); $value = isset($htmlAttributes['value']) ? $htmlAttributes['value'] : $this->value($fieldName); $out = array(); foreach ($options as $optValue => $optTitle) { $optionsHere = array('value' => $optValue); if (!empty($value) && $optValue == $value) { $optionsHere['checked'] = 'checked'; } $parsedOptions = $this->_parseAttributes(array_merge($htmlAttributes, $optionsHere), null, '', ' '); $individualTagName = $this->field() . "_{$optValue}"; $out[] = sprintf($this->tags['radio'], $this->model(), $this->field(), $individualTagName, $parsedOptions, $optTitle); } $out = join($inbetween, $out); return $this->output($out ? $out : null); }/** * Creates a textarea widget. * * @deprecated 1.2.0.5147 * @see FormHelper::input or FormHelper::textarea */ function textarea($fieldName, $htmlAttributes = array()) { trigger_error(sprintf(__('Method textarea() is deprecated in %s: see FormHelper::input or FormHelper::textarea', true), get_class($this)), E_USER_NOTICE); $htmlAttributes = $this->value($htmlAttributes, $fieldName); $value = null; if (isset($htmlAttributes['value']) && !empty($htmlAttributes['value'])) { $value = $htmlAttributes['value']; unset($htmlAttributes['value']); } $htmlAttributes = $this->domId($htmlAttributes); if ($this->tagIsInvalid()) { $htmlAttributes = $this->addClass($htmlAttributes, 'form_error'); } return $this->output(sprintf($this->tags['textarea'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' '), $value)); }/** * Creates a checkbox widget. * * @deprecated 1.2.0.5147 * @see FormHelper::input or FormHelper::checkbox */ function checkbox($fieldName, $title = null, $htmlAttributes = array()) { trigger_error(sprintf(__('Method checkbox() is deprecated in %s: see FormHelper::input or FormHelper::checkbox', true), get_class($this)), E_USER_NOTICE); $value = $this->tagValue($fieldName); $notCheckedValue = 0; if (isset($htmlAttributes['checked'])) { if ($htmlAttributes['checked'] == 'checked' || intval($htmlAttributes['checked']) === 1 || $htmlAttributes['checked'] === true) { $htmlAttributes['checked'] = 'checked'; } else { $htmlAttributes['checked'] = null; $notCheckedValue = -1; } } else { $model = $this->model(); if (isset($htmlAttributes['value']) || (!class_exists($model) && !App::import('Model', $model))) { if (isset($htmlAttributes['value']) && $htmlAttributes['value'] == $value) { $htmlAttributes['checked'] = 'checked'; } else { $htmlAttributes['checked'] = null; } if (isset($htmlAttributes['value']) && $htmlAttributes['value'] == '0') { $notCheckedValue = -1; } } else { $model = new $model; $db =& ConnectionManager::getDataSource($model->useDbConfig); $value = $db->boolean($value); $htmlAttributes['checked'] = $value ? 'checked' : null; $htmlAttributes['value'] = 1; } } $htmlAttributes = $this->domId($htmlAttributes); $output = $this->hidden($fieldName, array('value' => $notCheckedValue, 'id' => $htmlAttributes['id'] . '_'), true); $output .= sprintf($this->tags['checkbox'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, '', ' ')); return $this->output($output); }/** * Creates a hidden input field. * * @deprecated 1.2.0.5147 * @see FormHelper::input or FormHelper::hidden */ function hidden($fieldName, $htmlAttributes = array()) { trigger_error(sprintf(__('Method hidden() is deprecated in %s: see FormHelper::input or FormHelper::hidden', true), get_class($this)), E_USER_NOTICE); $htmlAttributes = $this->value($htmlAttributes, $fieldName); $htmlAttributes = $this->domId($htmlAttributes); return $this->output(sprintf($this->tags['hidden'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); }/** * Creates a text input widget. * * @deprecated 1.2.0.5147 * @see FormHelper::input or FormHelper::text */ function input($fieldName, $htmlAttributes = array()) { trigger_error(sprintf(__('Method input() is deprecated in %s: see FormHelper::input or FormHelper::text', true), get_class($this)), E_USER_NOTICE); $htmlAttributes = $this->value($htmlAttributes, $fieldName); $htmlAttributes = $this->domId($htmlAttributes); if (!isset($htmlAttributes['type'])) { $htmlAttributes['type'] = 'text'; } if ($this->tagIsInvalid()) { $htmlAttributes = $this->addClass($htmlAttributes, 'form_error'); } return $this->output(sprintf($this->tags['input'], $this->model(), $this->field(), $this->_parseAttributes($htmlAttributes, null, ' ', ' '))); }/** * Returns value of $fieldName. False if the tag does not exist. * * @deprecated 1.2.0.5147 * @see Helper::value */ function tagValue($fieldName) { trigger_error(sprintf(__('Method tagValue() is deprecated in %s: see Helper::value', true), get_class($this)), E_USER_NOTICE); $this->setEntity($fieldName); if (isset($this->data[$this->model()][$this->field()])) { return h($this->data[$this->model()][$this->field()]); } return false; }/** * Returns number of errors in a submitted FORM. * * @deprecated 1.2.0.5147 * @see FormHelper::errors */ function validate() { trigger_error(sprintf(__('Method validate() is deprecated in %s: see FormHelper::errors', true), get_class($this)), E_USER_NOTICE); $args = func_get_args(); $errors = call_user_func_array(array(&$this, 'validateErrors'), $args); return count($errors); }/** * Validates a FORM according to the rules set up in the Model. * * @deprecated 1.2.0.5147 * @see FormHelper::errors */ function validateErrors() { trigger_error(sprintf(__('Method validateErrors() is deprecated in %s: see FormHelper::errors', true), get_class($this)), E_USER_NOTICE); $objects = func_get_args(); if (!count($objects)) { return false; } $errors = array(); foreach ($objects as $object) { $errors = array_merge($errors, $object->invalidFields($object->data)); } return $this->validationErrors = (count($errors) ? $errors : false); }/** * Returns a formatted error message for given FORM field, NULL if no errors. * * @deprecated 1.2.0.5147 * @see FormHelper::error */ function tagErrorMsg($field, $text) { trigger_error(sprintf(__('Method tagErrorMsg() is deprecated in %s: see FormHelper::error', true), get_class($this)), E_USER_NOTICE); $error = 1; $this->setEntity($field); if ($error == $this->tagIsInvalid()) { return sprintf('<div class="error-message">%s</div>', is_array($text) ? (empty($text[$error - 1]) ? 'Error in field' : $text[$error - 1]) : $text); } else { return null; } }/* * @codeCoverageIgnoreEnd */}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?