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

  • 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
主站蜘蛛池模板: 即墨市| 卓尼县| 灵石县| 昌平区| 习水县| 友谊县| 桃园县| 江津市| 安徽省| 静宁县| 娱乐| 阳原县| 安化县| 富川| 涟源市| 彭山县| 育儿| 井陉县| 肥东县| 清流县| 鹰潭市| 湖口县| 肇庆市| 辽中县| 永丰县| 雷波县| 安化县| 龙游县| 闸北区| 麦盖提县| 泾源县| 中方县| 印江| 新丰县| 汉沽区| 邓州市| 花莲县| 潮安县| 托里县| 拜城县| 贞丰县|