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

  • 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:

  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
主站蜘蛛池模板: 上林县| 德钦县| 蕉岭县| 阜新市| 金华市| 建平县| 东乌珠穆沁旗| 湟源县| 甘泉县| 桃源县| 娄烦县| 米林县| 南丰县| 濉溪县| 昔阳县| 中宁县| 平利县| 金乡县| 南漳县| 鱼台县| 隆德县| 调兵山市| 十堰市| 同江市| 天气| 康马县| 武汉市| 前郭尔| 花莲市| 巴青县| 麻栗坡县| 韩城市| 株洲市| 垦利县| 枣强县| 芮城县| 新疆| 绥芬河市| 宝山区| 阳原县| 溧水县|