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

  • concrete5 Cookbook
  • David Strack
  • 269字
  • 2021-08-13 16:16:01

Creating custom block templates

All blocks come with a default view template, view.php. concrete5 also supports alternative templates, which users can enable through the concrete5 interface. You can also enable these alternative templates through your custom PHP code.

Getting ready

You will need a block type created and installed already. In this recipe, we are going to add a template to the block type that we created at the beginning of the chapter.

How to do it...

The steps for creating custom block templates are as follows:

  1. Open your block's directory.
  2. Create a new directory in your block's directory called templates/.
  3. Create a file called no_title.php in templates/.
  4. Add the following HTML code to no_title.php:
    <div class="content">
      <?php echo $content ?>
    </div>
  5. Activate the template by visiting a page that contains this block.
  6. Enter edit mode on the page and click on the block.
    Click on "Custom Template".
  7. Choose "No Title" and save your changes.

There's more...

You can specify alternative templates right from the block controller, so you can automatically render a different template depending on certain settings, conditions, or just about anything you can think of. Simply use the render() function in a callback that gets called before the view is rendered.

public function view() {
  $this->render('templates/no_title');
}

This will use the no_title.php file instead of view.php to render the block. Notice that adding the .php file extension is not required. Just like the block's regular view.php file, developers can include view.css and view.js files in their template directories to have those files automatically included on the page.

See also

  • The Using block controller callback functions recipe
  • The Creating a custom block type recipe
主站蜘蛛池模板: 喀什市| 衡阳市| 岚皋县| 大关县| 博爱县| 昌江| 永安市| 修文县| 桂平市| 明星| 长治市| 大名县| 林州市| 独山县| 张家川| 威海市| 平山县| 东明县| 平武县| 东阳市| 醴陵市| 洛宁县| 根河市| 岳池县| 社旗县| 灯塔市| 中超| 吴江市| 遂川县| 札达县| 兴仁县| 荃湾区| 信宜市| 方正县| 福鼎市| 时尚| 高阳县| 罗甸县| 宝丰县| 岚皋县| 武汉市|