- 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
推薦閱讀
- Java開發入行真功夫
- GeoServer Beginner's Guide(Second Edition)
- 持續輕量級Java EE開發:編寫可測試的代碼
- GameMaker Essentials
- Laravel Application Development Blueprints
- 物聯網系統架構設計與邊緣計算(原書第2版)
- 從零開始:C語言快速入門教程
- Android智能手機APP界面設計實戰教程
- Scratch編程從入門到精通
- LabVIEW入門與實戰開發100例(第4版)
- 微信公眾平臺開發最佳實踐
- Improving your Penetration Testing Skills
- Visual FoxPro數據庫程序設計
- Web前端開發精品課:HTML5 Canvas開發詳解
- JavaScript全棧開發