- concrete5 Cookbook
- David Strack
- 193字
- 2021-08-13 16:16:00
Sending variables from the controller to the view
A common task in MVC programming is the concept of setting variables from a controller to a view. In concrete5, blocks follow the same principles. Fortunately, setting variables to the view is quite easy.
Getting ready
This recipe will use the block type that was created in the first recipe of this chapter. Feel free to adapt this code to work in any block controller, though.
How to do it...
In your block's controller, use the set()
function of the controller
class to send a variable and a value to the view. Note that the view doesn't necessarily have to be the view.php
template of your block. You can send variables to add.php
and edit.php
as well. In this recipe, we will send a variable to view.php
. The steps are as follows:
- Open your block's
controller.php
file. - Add a function called
view()
if it doesn't already exist:public function view() { }
- Set a variable called
name
to the view.$this->set('name', 'John Doe');
- Open
view.php
in your block's directory. - Output the value of the name variable.
<div class="content"> <?php echo $name ?> </div>
See also
- The Using block controller callback functions recipe
推薦閱讀
- JavaScript百煉成仙
- ExtGWT Rich Internet Application Cookbook
- Progressive Web Apps with React
- 簡(jiǎn)單高效LATEX
- Scala Design Patterns
- Production Ready OpenStack:Recipes for Successful Environments
- Python機(jī)器學(xué)習(xí)經(jīng)典實(shí)例
- bbPress Complete
- OpenStack Orchestration
- Visual Basic程序設(shè)計(jì)實(shí)踐教程
- Learning OpenStack Networking(Neutron)(Second Edition)
- Lighttpd源碼分析
- Android驅(qū)動(dòng)開發(fā)權(quán)威指南
- 案例式C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)
- 人工智能算法(卷1):基礎(chǔ)算法