官术网_书友最值得收藏!

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:

  1. Open your block's controller.php file.
  2. Add a function called view() if it doesn't already exist:
    public function view() {
    }
  3. Set a variable called name to the view.
    $this->set('name', 'John Doe');
  4. Open view.php in your block's directory.
  5. Output the value of the name variable.
    <div class="content">
      <?php echo $name ?>
    </div>

See also

  • The Using block controller callback functions recipe
主站蜘蛛池模板: 新沂市| 南投县| 崇阳县| 宣威市| 临武县| 横山县| 集贤县| 莱芜市| 日照市| 新蔡县| 武宣县| 惠东县| 都匀市| 白玉县| 陕西省| 诸城市| 乡宁县| 岢岚县| 津南区| 赤城县| 克拉玛依市| 甘孜| 祁连县| 乌兰浩特市| 保定市| 白沙| 博乐市| 南岸区| 合作市| 玉环县| 罗田县| 青铜峡市| 偏关县| 邳州市| 九江县| 丰原市| 油尖旺区| 石台县| 和顺县| 镇沅| 柳州市|