Html.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/4/1
  6. * Time: 下午6:28
  7. */
  8. namespace yii\helpers;
  9. class Html extends BaseHtml
  10. {
  11. public static function icon($name)
  12. {
  13. $options = ['class' => 'fa'];
  14. if (!StringHelper::startsWith($name, 'fa-')) {
  15. $name = 'fa-' . $name;
  16. }
  17. self::addCssClass($options, $name);
  18. return self::tag('i', '', $options);
  19. }
  20. public static function staticControl($value, $options = [])
  21. {
  22. static::addCssClass($options, 'form-control-static');
  23. $value = (string) $value;
  24. if (isset($options['encode'])) {
  25. $encode = $options['encode'];
  26. unset($options['encode']);
  27. } else {
  28. $encode = true;
  29. }
  30. return static::tag('p', $encode ? static::encode($value) : $value, $options);
  31. }
  32. public static function activeStaticControl($model, $attribute, $options = [])
  33. {
  34. if (isset($options['value'])) {
  35. $value = $options['value'];
  36. unset($options['value']);
  37. } else {
  38. $value = static::getAttributeValue($model, $attribute);
  39. }
  40. return static::staticControl($value, $options);
  41. }
  42. public static function boolean($name, $checked = false, $options = [])
  43. {
  44. $options['data-toggle'] = 'switcher';
  45. return static::booleanInput('checkbox', $name, $checked, $options);
  46. }
  47. public static function activeBoolean($model, $attribute, $options = [])
  48. {
  49. $options['data-toggle'] = 'switcher';
  50. return static::activeBooleanInput('checkbox', $model, $attribute, $options);
  51. }
  52. /**
  53. * 标红字符串中含有的关键词
  54. * @param $q string 关键词
  55. * @param $str string 待过滤字符串
  56. * @return string 处理后的html
  57. */
  58. public static function weight($q, $str)
  59. {
  60. return preg_replace('/' . $q . '/i', Html::tag('span', '$0', ['style' => 'color:#f00']), $str);
  61. }
  62. }