Holder.js image is not rendered in Symfony project

610 views Asked by At

I've created a Symfony 4 project with Encore Webpack included. The template of the micro post page from my project is given below. As You can see, I'm using holder.js to display image placeholders, but they are not displaying at all.

{% 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 %}

The page is rendered without images PAGE EXAMPLE

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'
    ])
2

There are 2 answers

0
AudioBubble On BEST ANSWER

Wrong path to app.js was given - build\js\app.js. Solved by adding a valid app.js path to the base template:

base.html.twig

...

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

</body>

6
D_Breedt On

Everything looks fine, but you didn't include the entire webpack config. Just make sure the following line is commented out in your webpack.config.js file:

//.enableSingleRuntimeChunk()

Once this is done, run encore again, and holder should then render the images properly.

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();