- 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
推薦閱讀
- Spring 5企業級開發實戰
- Reactive Programming with Swift
- Python高效開發實戰:Django、Tornado、Flask、Twisted(第3版)
- 用戶體驗增長:數字化·智能化·綠色化
- Instant PHP Web Scraping
- 大話Java:程序設計從入門到精通
- Django Design Patterns and Best Practices
- Arduino機器人系統設計及開發
- ABAQUS6.14中文版有限元分析與實例詳解
- Flink核心技術:源碼剖析與特性開發
- TypeScript全棧開發
- Building Microservices with .NET Core 2.0(Second Edition)
- Isomorphic JavaScript Web Development
- C++ Windows Programming
- React.js 16從入門到實戰