- 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
推薦閱讀
- Learn ECMAScript(Second Edition)
- PHP程序設(shè)計(jì)(慕課版)
- Reactive Programming with Swift
- R語(yǔ)言數(shù)據(jù)可視化實(shí)戰(zhàn)
- C#程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- 微信小程序入門(mén)指南
- 精通Linux(第2版)
- INSTANT Passbook App Development for iOS How-to
- Python之光:Python編程入門(mén)與實(shí)戰(zhàn)
- AutoCAD 2009實(shí)訓(xùn)指導(dǎo)
- Getting Started with Python and Raspberry Pi
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)教程(Windows 7+Office 2010)
- Flink技術(shù)內(nèi)幕:架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- LabVIEW數(shù)據(jù)采集
- 計(jì)算機(jī)系統(tǒng)解密:從理解計(jì)算機(jī)到編寫(xiě)高效代碼