1

有谁知道如何使用 Symfony 1.4 在模板中添加样式表?

我已经尝试了我能想到的一切,从修改 frontend/config/view.yml 到修改模板本身 - 两者都有效。

我从搜索中看到,其他人也有同样的问题。使用 include_stylesheets 和 use_stylesheets 之间似乎存在某种冲突——但是这在任何地方都没有记录在任何地方 AFAIK。

4

3 回答 3

5

编辑:

好的,我想我现在明白了。您应该添加include_stylesheets()布局文件的head部分:

<html>
   <head>
       <title>This is the title</title>
       <?php include_stylesheets() ?>
   </head>
   <body>
   <!-- ... -->

然后在您的模板文件中,您可以使用use_stylesheet()为此模板添加特定样式表:

<?php use_stylesheet('/path/to/stylesheet.css') ?>

API 文档

include_stylesheets()
打印<link>view.yml 中配置或添加到响应对象的所有样式表的标签。

use_stylesheet()
将样式表添加到响应对象。

Javascript 也一样。


根据API 文档,它应该在 1.4 中仍然可以工作,sfWebResponse仍然有这个方法:

addStylesheet ($file, $position, $options)   
  $file   The stylesheet file  
  $position   Position   
  $options    Stylesheet options  
Adds a stylesheet to the current web response.

至少方法是存在的。
究竟是什么问题?如果您想调用该方法或者只是没有添加样式表,您会收到错误吗?

于 2010-01-20T09:09:06.980 回答
4

http://www.symfony-project.org/tutorial/1_4/en/upgrade#removal_of_the_common_filter

从 1.4 开始,您的 javascripts 和样式表不再自动注入到您的 head 标签中。相反,您需要在您希望放置它们的布局中包含以下内容:

<?php include_javascripts() ?>
<?php include_stylesheets() ?>

万一您的帖子标题不是错字,您需要在回复中使用 addStylesheet('...') :

$sf_response->addStylesheet('main');
于 2010-01-20T07:43:19.457 回答
0

$sf_context->getResponse()->addStylesheet('style.css')

$sf_context->getResponse()->addJavascript('script.js')

于 2010-01-20T08:53:23.100 回答