Section

Introduction

Dcat Admin provides the section feature that allows developers to change the content of various sections of the page at runtime without having to modify the template directly.

{tip} Dcat Admin‘s section function refers to Blade template engine’s @section function and Wordpress add_filter function, if the developer understands one of these two can quickly start.

Use

admin_section

Output section content, this function is similar to the @yield command in the Blade template and the apply_filters function in WordPress.

Parameters.

  • $section {string} Block Name
  • $default {string|Closure} Default Value
  • The $options {array} parameter, please note that the value of key must start with an English letter, otherwise it cannot be retrieved.

Returned values:

  • {string}
  1. echo admin_section('navigation', null, ['count' => 4]);

admin_inject_section

Note the content of section, this function is similar to the add_filter function in WordPress.

Parameters:

  • $section {string} Block Name
  • $content {string|Illuminate\Contracts\Support\Renderable|Closure} block content
  • If $append {bool} is passed as true, whether to append it to the last injected content or not, if false is passed, then the previous injected content will be replaced.
  • $priority {int} Default 10, priority, the higher the value, the higher the order.

When the second argument is passed to the anonymous function, the anonymous function receives an Illuminate\Support\Fluent object. The anonymous function receives an IlluminateSupport\Fluent object that contains the contents of the previous attribute, which can be obtained by accessing the section attribute; if the section has other parameters, they can also be obtained by accessing the attribute, as follows:

  1. admin_inject_section('navigation', e("<navigation>1</navigation>"));
  2. admin_inject_section('navigation', function ($options) {
  3. // Get the last content injected into this block
  4. $previous = $options->previous;
  5. // Get custom parameters
  6. $count = $options->count;
  7. return e("<navigation>count:{$count}</navigation>");
  8. }, true, 11);
  9. // output
  10. echo admin_section('navigation', null, ['count' => 4]);
  11. // The final output is
  12. // <navigation>count:4</navigation><navigation>1</navigation>

admin_inject_default_section

Inject default content, which does not take effect if the admin_inject_section function is called to inject content (either before or after).

parameters:

  • $section {string} block name
  • $content {string|Illuminate\Contracts\Support\Renderable|Closure} Block content, consistent with the second parameter of admin_inject_section
  1. admin_inject_default_section('navigation', 'No data available');

admin_has_section

This function returns a value of type bool to determine if content has been injected into section.

  1. var_dump(admin_has_section('navigation'));

admin_has_default_section

This function returns a value of type bool to determine if default content has been injected into section.

  1. var_dump(admin_has_default_section('navigation'));

System predefined blocks

The Dcat Admin predefines blocks through which the developer can change the content of the page.

All predefined block names are defined in the Dcat\Admin\Admin::SECTION class constant, which is accessed by way of class constants.

Enter content into the <head> tag

Here the Admin::SECTION['HEAD'] block allows you to enter content into the <head> tag.

Add the following code to app\Admin\bootstrap.php

  1. use Dcat\Admin\Admin;
  2. admin_inject_section(Admin::SECTION['HEAD'], function () {
  3. return '<script src="//oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>';
  4. });

Enter content into the <body> tag

The Admin::SECTION['BODY_INNER_BEFORE'] block allows you to enter content into the beginning of the interior of the <body> tag.

The Admin::SECTION['BODY_INNER_AFTER'] block allows you to enter content into the end position inside the <body> tag.

Enter content into the <div id=”app”> tag

The Admin::SECTION['APP_INNER_BEFORE'] block allows you to enter content into the beginning of the <div id="app"> tag interior.

The Admin::SECTION['APP_INNER_AFTER'] block allows you to enter content into the end position inside the <div id="app"> tag.

Changing the contents of the user information panel in the top navigation bar

The Admin::SECTION['NAVBAR_USER_PANEL'] block allows you to change the contents of the user information panel in the top navigation bar.

  1. admin_inject_section(Admin::SECTION['NAVBAR_USER_PANEL'], view('admin::partials.navbar-user-panel'));

Change the content in front of the top navigation bar

  1. admin_inject_section(Admin::SECTION['NAVBAR_BEFORE'], view('...'));

Change the content behind the top navigation bar

  1. admin_inject_section(Admin::SECTION['NAVBAR_AFTER'], view('...'));

Changing the content behind the user information panel in the top navigation bar

The Admin::SECTION['NAVBAR_AFTER_USER_PANEL'] block allows you to change the content behind the user information panel in the top navigation bar.

  1. admin_inject_section(Admin::SECTION['NAVBAR_AFTER_USER_PANEL'], function () {
  2. return <<<HTML
  3. <li>
  4. <a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
  5. </li>
  6. HTML;
  7. });

Changing the contents of the menu bar user information panel

The Admin::SECTION['LEFT_SIDEBAR_USER_PANEL'] block allows you to change the contents of the user information panel in the menu bar.

  1. admin_inject_section(Admin::SECTION['LEFT_SIDEBAR_USER_PANEL'], view('admin::partials.sidebar-user-panel'));

Changing the menu bar

The entire menu bar content can be changed with Admin::SECTION['LEFT_SIDEBAR_MENU'].

{tip} The menu of Dcat Admin is built by injecting default content into the LEFT_SIDEBAR_MENU block, which allows the developer to replace the system’s default menu rendering logic.

  1. use Dcat\Admin\Support\Helper;
  2. use Dcat\Admin\Admin;
  3. admin_inject_section(Admin::SECTION['LEFT_SIDEBAR_MENU'], function () {
  4. $menuModel = config('admin.database.menu_model');
  5. $builder = Admin::menu();
  6. $html = '';
  7. foreach (Helper::buildNestedArray((new $menuModel())->allNodes()) as $item) {
  8. $html .= view('admin::partials.menu', ['item' => $item, 'builder' => $builder])->render();
  9. }
  10. return $html;
  11. });