首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法使用"asset“函数包含CSS

无法使用"asset“函数包含CSS
EN

Stack Overflow用户
提问于 2018-07-05 22:43:48
回答 3查看 678关注 0票数 0

我有一个小项目,我正在使用symfony 4.1和Webpack安可。我正在尝试包含一个CSS文件,但它不加载。打开Opera (dev tools)中的网络标签,加载时会出现404错误。我将“资产”放在一个新创建的"web“文件夹中,似乎这是”资产“功能识别它们的唯一位置。我已经没有主意了。

我的树枝模板看起来像这样:

代码语言:javascript
复制
{% 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 %}

我想包含的两个文件是: main.css和util.css (我并不拥有这两个文件)。我是一个菜鸟,在前端,才刚刚起步。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-12 05:58:48

我必须在C:\xampp\apache\conf\extra\httpd-vhosts.conf中修改xamp配置文件

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2018-07-05 23:04:53

要加载位于web > assets > css > style.css下的文件,应使用以下<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/style.css') }}">

票数 2
EN

Stack Overflow用户

发布于 2018-07-05 23:25:52

除了Andrew Vakhniuk回答

在html中,<body>中不应该有<head>标记。

你可能需要使用一个块来实现它。

代码语言:javascript
复制
{# 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>

然后,您可以使用以下命令仅将指定的css添加到此单个页面

代码语言:javascript
复制
{% 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">
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51194188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档