在 Symfony 4 中使用 bootstrap 和 Webpack Encore
Using bootstrap with Webpack Encore in Symfony 4
我想在 Symfony 4.1 中将 bootstrap 与 Webpack Encore 一起使用,但 bootstrap 不起作用。在这个post的template/base.html.twig文件中,我用了一些bootstrap类但是没有考虑到,我不明白为什么。
我用 yarn 安装了 bootstrap 所需的依赖项:
yarn add bootstrap --dev
yarn add jquery --dev
yarn add popper.js --dev
template/base.html.twig
在这个文件中,我使用了 asset 函数来考虑文件:build/app.scss 和 [=58= .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>
在以下两个文件中,我需要并导入了 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
在这个文件中,我使用了 enableSassLoader() 来激活 Sass 和 autoProvidejQuery() 以便作为全局变量访问 jQuery。
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();
命令 yarn encore dev 可以正确构建所有内容。但是,我在屏幕上没有看到 bootstrap 主题。
提前致谢,
template/base.html.twig文件头部有错误。 asset/js/app.scss 在 public/build 目录中的结果是一个 app.css 文件而不是.scss 扩展文件。它是我们可以在 assets/js/ 目录中找到的所有 javascript 文件的构建结果。
<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>
来自 WebpackEncoreBundle 的两个 Twig 助手可以为您完成大部分工作:
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>
我想在 Symfony 4.1 中将 bootstrap 与 Webpack Encore 一起使用,但 bootstrap 不起作用。在这个post的template/base.html.twig文件中,我用了一些bootstrap类但是没有考虑到,我不明白为什么。
我用 yarn 安装了 bootstrap 所需的依赖项:
yarn add bootstrap --dev
yarn add jquery --dev
yarn add popper.js --dev
template/base.html.twig
在这个文件中,我使用了 asset 函数来考虑文件:build/app.scss 和 [=58= .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>
在以下两个文件中,我需要并导入了 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
在这个文件中,我使用了 enableSassLoader() 来激活 Sass 和 autoProvidejQuery() 以便作为全局变量访问 jQuery。
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();
命令 yarn encore dev 可以正确构建所有内容。但是,我在屏幕上没有看到 bootstrap 主题。
提前致谢,
template/base.html.twig文件头部有错误。 asset/js/app.scss 在 public/build 目录中的结果是一个 app.css 文件而不是.scss 扩展文件。它是我们可以在 assets/js/ 目录中找到的所有 javascript 文件的构建结果。
<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>
来自 WebpackEncoreBundle 的两个 Twig 助手可以为您完成大部分工作:
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>