0
votes

I have a small project and I am using symfony 4.1 with Webpack encore. I am trying to include a CSS file but it does not load. Opening network tab in Opera (dev tools), I can see a 404 error when loading. I have placed the "assets" inside a newly created "web" folder, as it seems that it is the only place where "asset" function recognizes them. I am out of ideas.

My twig template looks like this:

{% block title %} Ads {% endblock %}

{% block body %}

    <head>
        <link rel="stylesheet" type="text/css" href="{{ asset('css/main.css') }}">
        <link rel="stylesheet" type="text/css" href="{{ asset('css/util.css') }}">
    </head>

    <div class="limiter">
                <div class="container-table100">
                    <div class="wrap-table100">
                        <div class="table100">
                            <table class="table">
                                <thead class="thead-dark">
                                    <tr>
                                        <th scope="col">ID</th>
                                        <th scope="col">Title</th>
                                        <th scope="col">Description</th>
                                        <th scope="col">Price</th>
                                        <th scope="col">Category</th>
                                        <th scope="col">Start</th>
                                        <th scope="col">End</th>
                                        <th scope="col"></th>
                                        <th scope="col"></th>
                                    </tr>
                                </thead>

                                <tbody>
                                    {% for ad in ads %}
                                        <tr>
                                            <th scope="row">{{ ad.id }}</th>
                                            <td> <a href="{{ path('ad',{'id':ad.id}) }}">{{ ad.title }}</a></td>
                                            <td>{{ ad.description }}</td>
                                            <td>{{ ad.price }}</td>
                                            <td>{{ ad.category.name}}</td>
                                            <td>{{ ad.startDate|date('d:m:Y') }}</td>
                                            <td>{{ ad.expiryDate|date('d:m:Y') }}</td>
                                            <td><a class="btn btn-primary btn" href="{{ path('edit_ad',{'id':ad.id}) }}" role="button"> Edit</a></td>
                                            <td> <form action="{{ path('delete_ad',{'id':ad.id}) }}" method="post"> <button type="submit" class="btn btn-default">Delete</button> </form> </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
{% endblock %}

The tow files I want to include are: main.css and util.css (I do not own those two). I am a rookie when it comes to front-end, just started.

3

3 Answers

2
votes

To load file which is located under web > assets > css > style.css You should use following <link rel="stylesheet" type="text/css" href="{{ asset('assets/css/style.css') }}">

2
votes

In addition of Andrew Vakhniuk answer

In html, <head> tags are not supposed to be in <body>

You probably have to use a block for it

{# templates/base.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Test Application{% endblock %}</title>

        {# CSS included in all pages #}
        <link rel="stylesheet" type="text/css" href="{{ asset('assets/css/main.css') }}">
        {# CSS included in special pages #}
        {% block stylesheets %}{%enblock%}
    </head>

then you can add only the css specified to this single page using

{% block title %} Ads {% endblock %}

{% block stylesheets %}
     {# it will replace the content of the block of base.html.twig". use {{ parent() }} to add cotnent isntead of replace it %}
     <link rel="stylesheet" type="text/css" href="{{ asset('assets/css/util.css') }}">
{% endblock %}

{% block body %}
    <div class="limiter">
                <div class="container-table100">
                    <div class="wrap-table100">
0
votes

I had to modify xamp config file in : C:\xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
    ServerName adsmanagement.test
    ServerAlias adsmanagement.test

    DocumentRoot C:/xampp/htdocs/ads-management/public
    <Directory C:/xampp/htdocs/ads-management/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All

        FallbackResource /index.php
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    ErrorLog logs/ads_management.log
    CustomLog logs/ads_management_custom.log combined
</VirtualHost>