Holder.js 图片未在 Symfony 项目中呈现
Holder.js image is not rendered in Symfony project
我创建了一个包含 Encore Webpack 的 Symfony 4 项目。下面给出了我项目中的微型 post 页面的模板。如您所见,我正在使用 holder.js 来显示图像占位符,但它们根本没有显示。
{% extends 'base.html.twig' %}
{% block body %}
{% for post in posts %}
<div class="media text-muted pt-3">
<img data-src="holder.js/32x32?text=MJ&bg=e83e8c&fg=fff&size=8" alt="" class="mr-2 rounded">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<span class="d-block"><strong class="text-gray-dark">@username</strong> <small>{{ post.time|date("d/m/y") }}</small></span>
{{ post.text }}
</p>
</div>
{% endfor %}
{% endblock %}
页面呈现时没有图像
webpack.config.js
.addEntry('app', [
'./node_modules/jquery/dist/jquery.slim.js',
'./node_modules/popper.js/dist/popper.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.min.js',
'./node_modules/holderjs/holder.min.js'
])
.addStyleEntry('css/app', [
'./node_modules/bootstrap/dist/css/bootstrap.min.css',
'./assets/css/app.css'
])
一切看起来都很好,但您没有包含整个 webpack 配置。只需确保在 webpack.config.js 文件中注释掉以下行:
//.enableSingleRuntimeChunk()
完成后,运行再次安可,然后 holder 应该正确渲染图像。
vagrant@homestead:~/badland-retail$ ./node_modules/.bin/encore dev
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('js/app',
[
'./node_modules/jquery/dist/jquery.slim.js',
'./node_modules/popper.js/dist/popper.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.min.js',
'./node_modules/holderjs/holder.min.js'
])
.addStyleEntry('css/app', [
'./node_modules/bootstrap/dist/css/bootstrap.min.css',
'./assets/css/app.css'
])
// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
//.enableSingleRuntimeChunk()
/*
* 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()
// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin', './assets/js/admin.js')
;
module.exports = Encore.getWebpackConfig();
app.js 的路径错误 - build\js\app.js
。通过向基本模板添加有效的 app.js 路径解决:
base.html.twig
...
{% block javascripts %}
<script src="{{ asset('build/app.js') }}"></script>
{% endblock %}
</body>
我创建了一个包含 Encore Webpack 的 Symfony 4 项目。下面给出了我项目中的微型 post 页面的模板。如您所见,我正在使用 holder.js 来显示图像占位符,但它们根本没有显示。
{% extends 'base.html.twig' %}
{% block body %}
{% for post in posts %}
<div class="media text-muted pt-3">
<img data-src="holder.js/32x32?text=MJ&bg=e83e8c&fg=fff&size=8" alt="" class="mr-2 rounded">
<p class="media-body pb-3 mb-0 small lh-125 border-bottom border-gray">
<span class="d-block"><strong class="text-gray-dark">@username</strong> <small>{{ post.time|date("d/m/y") }}</small></span>
{{ post.text }}
</p>
</div>
{% endfor %}
{% endblock %}
页面呈现时没有图像
webpack.config.js
.addEntry('app', [
'./node_modules/jquery/dist/jquery.slim.js',
'./node_modules/popper.js/dist/popper.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.min.js',
'./node_modules/holderjs/holder.min.js'
])
.addStyleEntry('css/app', [
'./node_modules/bootstrap/dist/css/bootstrap.min.css',
'./assets/css/app.css'
])
一切看起来都很好,但您没有包含整个 webpack 配置。只需确保在 webpack.config.js 文件中注释掉以下行:
//.enableSingleRuntimeChunk()
完成后,运行再次安可,然后 holder 应该正确渲染图像。
vagrant@homestead:~/badland-retail$ ./node_modules/.bin/encore dev
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('js/app',
[
'./node_modules/jquery/dist/jquery.slim.js',
'./node_modules/popper.js/dist/popper.min.js',
'./node_modules/bootstrap/dist/js/bootstrap.min.js',
'./node_modules/holderjs/holder.min.js'
])
.addStyleEntry('css/app', [
'./node_modules/bootstrap/dist/css/bootstrap.min.css',
'./assets/css/app.css'
])
// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
//.enableSingleRuntimeChunk()
/*
* 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()
// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin', './assets/js/admin.js')
;
module.exports = Encore.getWebpackConfig();
app.js 的路径错误 - build\js\app.js
。通过向基本模板添加有效的 app.js 路径解决:
base.html.twig
...
{% block javascripts %}
<script src="{{ asset('build/app.js') }}"></script>
{% endblock %}
</body>