3

I would like to use bootstrap with Webpack Encore in Symfony 4.1 but bootstrap does not function. In the template/base.html.twig file in this post, I used some bootstrap classes but it is not taken into account and I don't understand why.

I installed the dependencies I need for bootstrap with yarn:

yarn add bootstrap --dev
yarn add jquery --dev
yarn add popper.js --dev

template/base.html.twig

In this file, I used the asset function in order to take into account the files: build/app.scss and build/app.js

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}
        <link rel="stylesheet" href="{{ asset('build/app.scss') }}">
        {% endblock %}
    </head>
    <body>
        {% block body %}
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <a class="navbar-brand" href="#">Navbar</a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" ar$
            <span class="navbar-toggler-icon"></span>
          </button>
        </nav>

        {% endblock %}
        {% block javascripts %}
            <script src="{{ asset('build/app.js') }}"></script>
        {% endblock %}
    </body>
</html>

In the two following files, I required and imported what I need for bootstrap.

assets/js/app.js

require('../css/app.scss');
var $ = require('jquery');
require('bootstrap');

assets/css/app.scss

@import "~bootstrap/scss/bootstrap";

webpack.config.js

In this file, I used enableSassLoader() in order to activate Sass and autoProvidejQuery() in order to have access to jQuery as a global variable.

var Encore = require('@symfony/webpack-encore');

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    // only needed for CDN's or sub-directory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
     */
    .addEntry('app', './assets/js/app.js')

    /*
     * FEATURE CONFIG
     *
     * Enable & configure other features below. For a full
     * list of features, see:
     * https://symfony.com/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())

    // enables Sass/SCSS support
    .enableSassLoader()

    // uncomment if you use TypeScript
    //.enableTypeScriptLoader()

    // uncomment if you're having problems with a jQuery plugin
    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

The command yarn encore dev builds everything correctly. But, I don't see the bootstrap theme in the screen.

Thanks in advance,

2 Answers 2

6

Two Twig helpers from WebpackEncoreBundle can do most of the work for you:

https://symfony.com/doc/current/frontend/encore/simple-example.html#the-import-and-export-statements

{# templates/base.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        <!-- ... -->

        {% block stylesheets %}
            {# 'app' must match the first argument to addEntry() in webpack.config.js #}
            {{ encore_entry_link_tags('app') }}

            <!-- Renders a link tag (if your module requires any CSS)
                 <link rel="stylesheet" href="/build/app.css"> -->
        {% endblock %}
    </head>
    <body>
        <!-- ... -->

        {% block javascripts %}
            {{ encore_entry_script_tags('app') }}

            <!-- Renders app.js & a webpack runtime.js file
                <script src="/build/runtime.js"></script>
                <script src="/build/app.js"></script> -->
        {% endblock %}
    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

There is an error in the template/base.html.twig file in the head. The result of the global asset/js/app.scss in the public/build directory is a app.css file and not a .scss extension file. It is the result of the build of all the javascript files we can find in the assets/js/ directory.

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>{% block title %}Welcome!{% endblock %}</title>
    {% block stylesheets %}
    <link rel="stylesheet" href="{{ asset('build/app.css') }}">
    {% endblock %}
</head>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.