drl6054 2014-09-14 18:00 采纳率: 0%
浏览 55
已采纳

在symfony 2.5中包含css和js文件

I am working with symfony 2.5 and while including the css and js files although they seem to work but when i check the source in browser i see multiple css and js files being included where as i only included them once. This is what my header.html.twig looks like

{% block head %}
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Home | Flat Theme</title>
        {% block stylesheets %}
            {% stylesheets '@DefaultBundle/Resources/public/css/*' %}
            <link rel="stylesheet" href="{{ asset('bundles/default/css/bootstrap.min.css') }}"/>
            <link rel="stylesheet" href="{{ asset('bundles/default/css/font-awesome.min.css') }}"/>
            <link rel="stylesheet" href="{{ asset('bundles/default/css/prettyPhoto.css') }}"/>
            <link rel="stylesheet" href="{{ asset('bundles/default/css/animate.css') }}/">
            <link rel="stylesheet" href="{{ asset('bundles/default/css/main.css') }}"/>
            {% endstylesheets %}
        {% endblock %}

        {% block topjavascripts %}
            {% javascripts '@DefaultBundle/Resources/public/js/*' %}
            <script src="{{ asset('bundles/default/js/html5shiv.js') }}"></script>
            <script src="{{ asset('bundles/default/js/respond.min.js') }}"></script>
            {% endjavascripts %}
        {% endblock %}
    </head>
{% endblock %}
<body>

Here is the source i see in browser and you will notice each css and js file has been mentioned over 5 times.

I tried removing the @DefaultBundle/Resources/public/css/* from {% stylesheets '@DefaultBundle/Resources/public/css/*' %} but doing that removes all the style

What am i doing wrong?

enter image description here

  • 写回答

3条回答 默认 最新

  • duanniedang3946 2014-09-14 18:14
    关注

    Try this:

    {% block head %}
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Home | Flat Theme</title>
        <link rel="stylesheet" href="{{ asset('bundles/default/css/bootstrap.min.css') }}"/>
        <link rel="stylesheet" href="{{ asset('bundles/default/css/font-awesome.min.css') }}"/>
        <link rel="stylesheet" href="{{ asset('bundles/default/css/prettyPhoto.css') }}"/>
        <link rel="stylesheet" href="{{ asset('bundles/default/css/animate.css') }}/">
        <link rel="stylesheet" href="{{ asset('bundles/default/css/main.css') }}"/>
    
        <script src="{{ asset('bundles/default/js/html5shiv.js') }}"></script>
        <script src="{{ asset('bundles/default/js/respond.min.js') }}"></script>
    </head>
    {% endblock %}
    <body>
    

    Or, if you don't want to call each file explicitly, try this:

    {% block head %}
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <title>Home | Flat Theme</title>
        {% block stylesheets %}
            {% stylesheets 'bundles/default/css/*' filter='cssrewrite' %}
            <link rel="stylesheet" href="{{ asset_url }}"/>
            {% endstylesheets %}
        {% endblock %}
    
        {% block topjavascripts %}
            {% javascripts '@DefaultBundle/Resources/public/js/*' %}
                <script src="{{ asset_url }}"></script>
            {% endjavascripts %}
        {% endblock %}
    </head>
    {% endblock %}
    <body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化