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

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
主站蜘蛛池模板: 正宁县| 湖口县| 安西县| 屏南县| 许昌县| 博湖县| 晴隆县| 汝阳县| 阿鲁科尔沁旗| 婺源县| 高淳县| 松桃| 图木舒克市| 屯门区| 海丰县| 景泰县| 株洲县| 新和县| 伊金霍洛旗| 靖西县| 拉萨市| 清苑县| 龙江县| 双辽市| 杭锦旗| 临汾市| 广昌县| 永丰县| 丹阳市| 台东市| 和平区| 霍山县| 黔西| 安龙县| 巴彦淖尔市| 庄浪县| 阜新市| 白玉县| 贺兰县| 逊克县| 奈曼旗|