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

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
主站蜘蛛池模板: 二连浩特市| 陵水| 依安县| 民勤县| 波密县| 渭源县| 古浪县| 云浮市| 钟祥市| 黄浦区| 东光县| 清新县| 四子王旗| 新宾| 原阳县| 永定县| 泸水县| 商城县| 墨玉县| 兴义市| 靖宇县| 蒲江县| 根河市| 汝州市| 新兴县| 汶川县| 车险| 五大连池市| 绵竹市| 南岸区| 海丰县| 东方市| 福泉市| 东乌珠穆沁旗| 平塘县| 万源市| 青河县| 洱源县| 体育| 靖边县| 尼勒克县|