0

I recently started a new project in Symfony and I did everything here but it didn't help.

I work for first time with Symfony and this is my first project. I strictly follow the documentation and I am trying to add some CSS but its now working. I use Encore and downloaded Yarn as the documentation says to do. Below I will upload some of my code. It seems fine but it's not.

html.twig

<!DOCTYPE html>
<html lang="en" xmlns:th="http:thymeleaf.org">

<head>
<title>Events & People</title>
<meta charset="UTF-8">


{% block stylesheets %}
{#{% stylesheets '@AppBundle/public/build/app.css' %}#}
    {{ encore_entry_link_tags('app') }}
    <link src="/build/app.css" rel="stylesheet"/>

{#{% endstylesheets %}#}
{% endblock %}
</head>

webpack.config.js

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')
//.addEntry('page1', './assets/js/page1.js')
//.addEntry('page2', './assets/js/page2.js')

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

//11111111111
// .enableSassLoader()
//
// // processes files ending in .less
// .enableLessLoader()
//
// // processes files ending in .styl
// .enableStylusLoader()
//11111111111

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

Here is an image of my tree of files:

File tree

If it's done right it should load the files from my /build/app.css, but it's not loading it.

3
  • Just to be sure, your public/build/app.css is not empty right ? I didn't know the EncoreBundle changed so I don't know exactly what does those encore_entry_* functions. I'll take a look tonight. Also, why do you have a style.scss file in /public ? And do you have any error in the network tab (browser dev console) ? Commented Jan 3, 2019 at 15:06
  • 1 Yes, it's not empty 2 When I was reading the tutorial in here and I did it, it created a public folder and in the documentation was written to use it. 3 Yes I have an error but I don't know if it's from the CSS here Commented Jan 6, 2019 at 15:55
  • Hmmm where this message is displayed ? seems like there is more than a CSS/JS problem if the whole debug bar is bugged. Do you have more information about that error or you have this message only ? IN browser console (F12 > console tabs), do you have any error message ? Commented Jan 6, 2019 at 20:08

7 Answers 7

2

For me, it was just a caching problem. let's try to delete the cache.

Sign up to request clarification or add additional context in comments.

Comments

1

I didn't search deeply, but one bad think is your link tag...

Replace (src by href)

<link src="/build/app.css" rel="stylesheet"/>

By

<link href="/build/app.css" rel="stylesheet"/>

Comments

1

Have you tried this:

<link src="build/app.css" rel="stylesheet"/>

removing the leading "/" only and see if that works? not certain though.

Comments

1

I solved my problem I just have an error in my CSS and yarn didn't compile it

Comments

0

Move your link tag outside of the stylesheet block's. And use twig asset() function instead of the absolute path. PS: use href instead of src in link tag

<!DOCTYPE html>
<html lang="en" xmlns:th="http:thymeleaf.org">

<head>
    <title>Events & People</title>
    <meta charset="UTF-8">

    <link href="{{ asset('build/app.css') }}" type="text/css" rel="stylesheet"/>

    {% block stylesheets %} {% endblock %}
</head>

If you really want the place the link tag inside your stylesheet block's, you must call this by using twig parent() function's in each template view

{% extends 'base.html.twig' %}
{% block stylesheets %}
    {# call the parent content inside stylesheet block #}
    {{ parent() }}

    {# other css code here #}
{% endblock %}

1 Comment

It didn't help either.
0

the script & stylesheets should be outside the body tag like this!

> <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Welcome!{% endblock %}</title>
       
    </head>
    <body>
        {% block body %}{% endblock %}
    </body>
     {% block stylesheets %}
        {{ encore_entry_link_tags('app') }}
     {% endblock %}
     {% block javascripts %}
        {{ encore_entry_script_tags('app') }}
     {% endblock %}
</html>

Comments

0

The script and stylesheets should be outside the body tag, like this:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Welcome!{% endblock %}</title>
       
    </head>
    <body>
        {% block body %}{% endblock %}
    </body>
     {% block stylesheets %}
        {{ encore_entry_link_tags('app') }}
     {% endblock %}
     {% block javascripts %}
        {{ encore_entry_script_tags('app') }}
     {% endblock %}
</html>

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.