Example of display method of thinkPHP controller variables in template

  • 2021-09-16 06:31:35
  • OfStack

In this paper, the display method of thinkPHP controller variables in template is described with examples. Share it for your reference, as follows:

Variables in controller


public function register() {
  $type = I("param.type");//1. Student registration  2. Teacher registration  3. Other registrations 
  $this -> assign("type", $type);
    //q All departments 
  $depart1 = M("Depart") -> where("status=1 and fid=0") -> order("id asc") -> select();
  $this -> assign("depart1", $depart1);
  $this -> display();
}

In the template reference position 1: php code, directly use $i;


<php>
  echo $i;
</php

Reference Position 2 in Template: Direct Application in Template {$i} Or class="{$unlogined}"


<font color="red"> Note: 1. Non-related personnel are strictly prohibited from registration. {$i}</font><br>
<php>
  $logined = is_array($_SESSION['userInfo']) ? "" : "hide-div";
  $unlogined = $logined == "hide-div" ? "" : "hide-div";
</php>
<div id="unlogined-div" class="{$unlogined}">

Reference position 3 in template: used in template label, such as condition, without {}.


<if condition="$type neq 4">
  <div class="form-group">
    <label for="" class="control-label col-sm-3">1 Level department : <span class="text-danger">*</span></label>
    <div class="col-sm-9">
      <select name="depart1_id" id="depart1_id" onchange="depart1change()" class="form-control input-sm">
        <option value="-1">----- Please select 1 Level department -----</option>
        <foreach name="depart1" item="vo">
          <option value="{$vo.id}">{$vo.name}</option>
        </foreach>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label for="" class="control-label col-sm-3">2 Level department : <span class="text-danger">*</span></label>
    <div class="col-sm-9">
      <select name="depart2_id" id="depart2_id" onchange="depart2change()" class="form-control input-sm">
        <option selected='selected'>----- Please select first 1 Level department -----</option>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label for="" class="control-label col-sm-3">3 Level department : <span class="text-danger">*</span></label>
    <div class="col-sm-9">
      <select name="depart3_id" id="depart3_id" class="form-control input-sm">
        <option selected='selected'>----- Please select first 2 Level department -----</option>
      </select>
    </div>
  </div>
</if>

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to PHP programming based on ThinkPHP framework.


Related articles: