- 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企業級開發實戰
- 深度學習經典案例解析:基于MATLAB
- Vue.js 2 and Bootstrap 4 Web Development
- 深入淺出Spring Boot 2.x
- 從0到1:HTML+CSS快速上手
- MATLAB實用教程
- 差分進化算法及其高維多目標優化應用
- Drupal 8 Configuration Management
- Learning DHTMLX Suite UI
- Getting Started with Greenplum for Big Data Analytics
- D3.js By Example
- Python語言實用教程
- 智能搜索和推薦系統:原理、算法與應用
- Rust游戲開發實戰
- Hack與HHVM權威指南