I tried to create a small project myself (using the thinkphp framework). here I encountered a problem about template assignment: each template has a header & amp; lt; includefile & quot; publicheader & quot; & amp; gt;, no problem. but the problem is that there is a variable in the header that needs to be output to each cited... I tried to create a small project myself (using the thinkphp framework). here I encountered a problem about template assignment: each template has a header. There is no problem. but the problem is that there is a variable in the header that needs to be output to every template referenced to it. this variable is generated by a method in the controller:
Public function header (){
$value=$name; $this->assign('lo_id',$value);}
I cannot write this method once on the controller of each template. of course, I cannot place this method on the parent class controller of the template controller. the template controller:
Namespace HomeController;
Use ThinkController;
Class IndexController extends Controller
{
public function index(){$this->display("url");}
}
How can I output the above variable {$ lo_id} to each template?
Or I may not express the requirement clearly-that is, each template can output a variable.
Reply content:
I tried to create a small project myself (using the thinkphp framework). here I encountered a problem about template assignment: each template has a header. There is no problem. but the problem is that there is a variable in the header that needs to be output to every template referenced to it. this variable is generated by a method in the controller:
Public function header (){
$value=$name; $this->assign('lo_id',$value);}
I cannot write this method once on the controller of each template. of course, I cannot place this method on the parent class controller of the template controller. the template controller:
Namespace HomeController;
Use ThinkController;
Class IndexController extends Controller
{
public function index(){$this->display("url");}
}
How can I output the above variable {$ lo_id} to each template?
Or I may not express the requirement clearly-that is, each template can output a variable.
Put the public part in the parent class, and inherit the subclass.
display("url"); }}
header(); }}
1. every page will have $ this-> assign ('data', $ data); method.
Just attach this variable. you don't need to write a method separately.
2. write a base class. Inherit Controller, implement this method, and then your page will inherit the base class, so you don't need to write it again
(Sample Code already provided on the first floor)