1. 程式人生 > >thinkphp5 自定義標籤

thinkphp5 自定義標籤

<?php
namespace app\backend\taglib;

use think\template\TagLib;

class Tytag extends TagLib {
 
  protected $tags   =  [
    // 標籤定義: attr 屬性列表 close 是否閉合(0 或者1 預設1) alias 標籤別名 level 巢狀層次
    'textfield'     => ['attr' => 'label,name,value,help', 'close' => 0], //閉合標籤,預設為不閉合
    'textareafield'     => ['attr' => 'label,name,value,help', 'close' => 0],
    'filefield'     => ['attr' => 'label,name,value,updir,ftype,help', 'close' => 0],
    'mulfilefield'     => ['attr' => 'label,name,value,updir,help', 'close' => 0],
    'submitfield'     => ['close' => 0],
  ];

  /**
   * text 表單
   * label: 表單標題
   * name: 表單name
   * value: 表單值
   * help: 表單說明
   */
  public function tagTextfield($tag) {
    $html = "<div class='form-group'>";
    $html .= "<div class='form-label'>".$tag['label']."</div> <div class='form-input'>";
    if (empty($tag['value'])) {
      $html .= "<input type='text' name='".$tag['name']."' placeholder='".$tag['label']."' value='' />";
    } else {
      $html .= "<input type='text' name='".$tag['name']."' placeholder='".$tag['label']."' value='";
      $html .= "<?php echo ".$tag['value']."?>";
      $html .= "' />";
    }
    if(!empty($tag['help'])) {
      $html .= "<div class='help-block'><img src='/backend/ui_tip.png' /><p>".$tag['help']."</p></div>";
    }
    $html .= "</div></div>";
   
    return $html;
  } 

  /**
   * textarea 表單
   * label: 表單標題
   * name: 表單name
   * value: 表單值
   * help: 表單說明
   */
  public function tagTextareafield($tag) {
    $html = "<div class='form-group'>";
    $html .= "<div class='form-label'>".$tag['label']."</div> <div class='form-input'>";
    if (empty($tag['value'])) {
      $html .= "<textarea name='".$tag['name']."' placeholder='".$tag['label']."'></textarea>";
    } else {
      $html .= "<textarea name='".$tag['name']."' placeholder='".$tag['label']."'>";
      $html .= "<?php echo ".$tag['value']."?>";
      $html .= "</textarea>";
    }
    if(!empty($tag['help'])) {
      $html .= "<div class='help-block'><img src='/backend/ui_tip.png' /><p>".$tag['help']."</p></div>";
    }
    $html .= "</div></div>";
   
    return $html;
  } 

  /**
   * 單圖片,
   * label: 表單標題
   * name: 表單name
   * updir: 上傳路徑
   * value: 表單值
   * ftype: 上傳型別: image, video
   * help: 表單說明
   */
  public function tagFilefield($tag) {
    $html = "<div class='form-group'>";
    $html .= "<div class='form-label'>".$tag['label']."</div> <div class='form-input media-picker'>";
    $html .= "<a href='javascript:void(0)' class='button media-picker-button' data-id='".$tag['name']."' id='".$tag['name']."_uploader'>";
    $html .= "<span>+</span>";
    if (empty($tag['value'])) {
      $html .= "<input type='hidden' name='".$tag['name']."' upload-path='".$tag['updir']."' value='' />";
    } else {
      $html .= "<input type='hidden' name='".$tag['name']."' upload-path='".$tag['updir']."' value='";
      $html .= "<?php echo ".$tag['value']."?>";
      $html .= "' />";
    }
    $html .="</a>";
    if(!empty($tag['help'])) {
      $html .= "<div class='help-block'><img src='/backend/ui_tip.png' /><p>".$tag['help']."</p></div>";
    }
    $html .= "<ul class='clearfix image-list'>";
    if(!empty($tag['value'])) {
      // 這裡不能簡單的用if (empty($tag['value'])) 判斷,因為$tag['value']實際上是字串 比如$model['logo'],所以要使用模板標籤解析後判斷
      $html .= '{if '.$tag['value'].' != ""}';
      if($tag['ftype'] == 'video') {
        $html .= "<li><video controls src='<?php echo ".$tag['value']."?>'></video><span class='delete-image'>✖</span></li>";
      } else {
        $html .= "<li><img src='<?php echo ".$tag['value']."?>' /><span class='delete-image'>✖</span></li>";
      }
      $html .= '{/if}';
    }
    $html .= "</ul></div></div>";
   
    return $html;
  } 

  /**
   * 多圖片,多圖片不考慮視訊
   * label: 表單標題
   * name: 表單name
   * updir: 上傳路徑
   * value: 表單值
   * help: 表單說明
   */
  public function tagMulfilefield($tag) {
    $html = "<div class='form-group'>";
    $html .= "<div class='form-label'>".$tag['label']."</div> <div class='form-input media-picker'>";
    $html .= "<a href='javascript:void(0)' class='button media-picker-button' data-id='".$tag['name']."' id='".$tag['name']."_uploader' data-multiple='multiple'>";
    $html .= "<span>+</span>";
    if (empty($tag['value'])) {
      $html .= "<input type='hidden' name='".$tag['name']."' upload-path='".$tag['updir']."' value='' />";
    } else {
      $html .= "<input type='hidden' name='".$tag['name']."' upload-path='".$tag['updir']."' value='";
      $html .= "<?php echo ".$tag['value']."?>";
      $html .= "' />";
    }
    $html .="</a>";
    if(!empty($tag['help'])) {
      $html .= "<div class='help-block'><img src='/backend/ui_tip.png' /><p>".$tag['help']."</p></div>";
    }
    $html .= "<ul class='clearfix image-list'>";
    if(!empty($tag['value'])) {
      // 這裡不能簡單的用if (empty($tag['value'])) 判斷,因為$tag['value']實際上是字串 比如$model['logo'],所以要使用模板標籤解析後判斷
      $html .= '{if '.$tag['value'].' != ""}';
      $html .= "<li><img src='<?php echo ".$tag['value']."?>' /><span class='delete-image'>✖</span></li>";
      $html .= '{/if}';
    }
    $html .= "</ul></div></div>";
   
    return $html;
  } 

  public function tagSubmitfield() {
    $html = "<div class='form-group'>";
    $html .= "<div class='form-label'>&nbsp;</div> <div class='form-input'>";
    $html .= "<input type='submit' class='btn-submit' value='提 交' /></div></div>";
    return $html;
  }
  
  
}
?>

在config.php的template下新增

'taglib_pre_load' => 'app\backend\taglib\Tytag',

 

使用:

{tytag:textfield label="seo標題" name="seo_title" value="" /}