Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. NelmioApiDocBundle
  5. Customization

Customization

Edit this page

The look and feel of the Swagger UI can be customized.

Overwrite Twig Template

If you want to customize parts of the template, you can create your own Twig template. This allows to change Swagger UI configuration, page title, page header, add additional or replace existing styles or scripts.

Take a look at the Twig documentation how to extend templates.

The following example will add additional scripts and a custom style to the template. Just create a file templates/bundles/NelmioApiDocBundle/SwaggerUi/index.html.twig.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{# templates/bundles/NelmioApiDocBundle/SwaggerUi/index.html.twig #}

{#
    To avoid a "reached nested level" error an exclamation mark `!` has to be added
    See https://symfony.com/blog/new-in-symfony-3-4-improved-the-overriding-of-templates
#}
{% extends '@!NelmioApiDoc/SwaggerUi/index.html.twig' %}

{#
    Change Swagger UI configuration
    All parameters are explained on Swagger UI website:
    https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/
#}
{% block swagger_initialization %}
    <script type="text/javascript">
        window.onload = () => {
            loadSwaggerUI({
                defaultModelsExpandDepth: -1,
                deepLinking: true,
            });
        };
    </script>
{% endblock %}

{#
    Change Redocly configuration
    All parameters are explained on Redocly website:
    https://redocly.com/docs/redoc/config/
#}
{% block swagger_initialization %}
    <script type="text/javascript">
        window.onload = () => {
            loadRedocly({
                expandResponses: '200,201',
                hideDownloadButton: true,
            });
        };
    </script>
{% endblock %}

{# Import your own stylesheet #}
{% block stylesheets %}
    {{ parent() }}
    <link rel="stylesheet" href="{{ asset('css/custom-swagger-styles.css') }}">
{% endblock stylesheets %}

{# Import your own script #}
{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript" src="{{ asset('js/custom-request-signer.js') }}"></script>
{% endblock javascripts %}

You can have a look at the original template, in /templates/SwaggerUi/index.html.twig, to see which blocks can be overridden.

Assets Loading Options

The `html_config` settings allow you to configure how assets are loaded for the UI. The `assets_mode` option supports three values: `cdn`, `bundle`, and `offline`.

1
2
3
nelmio_api_doc:
    html_config:
        assets_mode: 'cdn' # Other values: 'bundle', 'offline'

`assets_mode`

The three values possible values can be found in AssetsMode.php - cdn: Loads assets from jsDelivr. - bundle: Fetches assets from the bundle in the vendor directory, including updates. - offline: Loads assets from the local `assets` directory, requiring the developer to update them manually.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Online exam, become Sylius certified today

    Online exam, become Sylius certified today

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Version:

    Table of Contents

    • Overwrite Twig Template
    • Assets Loading Options
      • `assets_mode`

    Symfony footer

    Avatar of Peter Gribanov, a Symfony contributor

    Thanks Peter Gribanov for being a Symfony contributor

    1 commit • 2 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony