将 css 添加到 Magento 中模板文件的头部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8739793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 02:33:27  来源:igfitidea点击:

Add css to head from template file in Magento

cssmagento

提问by Sohail

I want to add a CSS file inside tag from template (.phtml) file in magento. Is it possible ?

我想在 magento 中的模板(.phtml)文件中的标签内添加一个 CSS 文件。是否可以 ?

There is a reason to do so: the CSS file name is dynamic, so I don't know until the template executes. Is it possible to do so?

这样做是有原因的:CSS文件名是动态的,所以直到模板执行我才知道。有可能这样做吗?

回答by Nick

To add a CSS file from a controller after you've loaded the layout, but before you've rendered the layout, you'd do something along the lines of:

要在加载布局之后从控制器添加 CSS 文件,但在渲染布局之前,您需要执行以下操作:

public function indexAction() {
    $this->loadLayout();

    $head = Mage::app()->getLayout()->getBlock('head');
    $head->addItem('skin_css', 'css/additional.css');

    $this->renderLayout();
}

The problem with doing something this this in the template file is that it's highly likely that the headtemplate has already been rendered, and so the additional directives you give the block instance are useless because they are too late.

在模板文件中执行此操作的问题在于模板很可能head已经被渲染,因此您为块实例提供的附加指令没有用,因为它们为时已晚。

Just use a layout file and do the following:

只需使用布局文件并执行以下操作:

<?xml version="1.0">
<layout>
    <default>
        <reference name="head">
            <action method="addItem"><type>skin_css</type><file>css/additional.css</file></action>
        </reference>
    </default>
</layout>

回答by atif

yes it is possible and nothing different just like this:

是的,这是可能的,没有什么不同,就像这样:

<head>
<link rel="stylesheet" href="<?php echo $this->getSkinUrl(); ?>css/yourCssfile.css" type="text/css" />
</head>

it will pick ur theme folder from skin/themefolder/css/yourCss

它会从 skin/themefolder/css/yourCss 中选择你的主题文件夹

回答by user4049366

hi work fine this type called css in my first project http://satyak.co/go on head.phtml after all css and js and call getSkinUrl('css/bootstrap.css');?>" rel="stylesheet">

嗨,在我的第一个项目http://satyak.co/ 中,在我的第一个项目http://satyak.co/上运行 head.phtml 并调用 getSkinUrl('css/bootstrap.css');?>" rel="stylesheet" >