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

  • concrete5 Cookbook
  • David Strack
  • 382字
  • 2021-08-13 16:16:02

Adding a block to a page

Users can use the intuitive concrete5 interface to add blocks to the various areas of pages on the website. You can also programmatically add blocks to pages using the concrete5 API.

Getting ready

The code in this chapter can be run anywhere that you would like to create a block. To keep things simple, we are going to use the /config/site_post.php file to run some arbitrary code.

This example assumes that a page with a path of /about exists on your concrete5 site. Feel free to create that page, or adapt this recipe to suit your needs. Also, this recipe assumes that /about has a content area called content. Again, adapt according to your own website's configuration.

We will be using the block that was created at the beginning of this chapter.

How to do it...

The steps for adding a block to a page are as follows:

  1. Open /config/site_post.php in your code editor.
  2. Load the page that you would like to add a block to:
    $page = Page::getByPath('/about');
  3. Load the block by its handle:
    $block = BlockType::getByHandle('hello_world');
  4. Define the data that will be sent to the block:
    $data = array(
      'title' => 'An Exciting Title',
      'content' => 'This is the content!'
    );
  5. Add the block to the page's content area:
     $page->addBlock($block, 'content', $data);

How it works...

First you need to get the target page. In this recipe, we get it by its path, but you can use this function on any Page object. Next, we need to load the block type that we are adding. In this case, we are using the one that was created earlier in the chapter. The block type handle is the same as the directory name for the block.

We are using the $data variable to pass in the block's configuration options. If there are no options, you will need to pass in an empty array, as concrete5 does not allow that parameter to be blank. Finally, you will need to know the name of the content area; in this case, the content area is called "content".

See also

  • The Creating a custom block type recipe
  • The Loading a block type by its handle recipe
  • The Getting a page by its path recipe in Chapter 1, Pages and Page Types
主站蜘蛛池模板: 开封市| 安泽县| 禹州市| 全椒县| 焉耆| 娱乐| 河间市| 扎鲁特旗| 满城县| 长海县| 安阳县| 余江县| 永嘉县| 高邮市| 建昌县| 台南县| 曲阜市| 平远县| 和平县| 林芝县| 调兵山市| 当涂县| 荔波县| 乌鲁木齐县| 大冶市| 邮箱| 石狮市| 昭通市| 昌黎县| 龙口市| 保山市| 繁峙县| 都昌县| 惠安县| 闽侯县| 大丰市| 磐安县| 临泉县| 天津市| 安福县| 芜湖县|