我尝试了几种方法将.css和.js文件应用于视图php文件。 (1)第一种方式如下图所示:
<head>
<meta charset="UTF-8" />
<?php echo html::meta('viewport', 'width=980'); ?>
<title>A-TRACK Tutoring</title>
<?php echo html::link('http://fonts.googleapis.com/css?family=Open+Sans:300,400,700',
'stylesheet','text/css', FALSE);?>
<?php echo html::link('media/css/atrack.css',
'stylesheet','text/css', FALSE, 'all');?>
</head>
......
</html>
(2)第二种方式如下所示:
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=980" />
<title>A-TRACK Tutoring</title>
<?php
if(isset($css)){
foreach($css as $type => $file){
?>
<link type='text/css' href='<?php echo $file ?>' rel='stylesheet' />
<?php
}}
?>
......
</head>
并在控制器的索引操作中
public function action_index()
{
$view = View::factory('singleanswer/index');
$view->css = array(
'normal' => 'media/css/atrack.css',
'normal' => 'http://fonts.googleapis.com/css?family=Open+Sans:300,400,700',
);
// Render the view
$this->response->body($view);
}
} // End Welcome
它们都不起作用。有谁知道原因?谢谢。
答案 0 :(得分:0)
在示例2中,您通过在数组中使用相同的键两次来写入atrack.css值。
按照以下方式更改您的控制器:
public function action_index()
{
$view = View::factory('singleanswer/index', array("css" => array(
Url::site("media/css/atrack.css"),
'http://fonts.googleapis.com/css?family=Open+Sans:300,400,700',
));
// Render the view
$this->response->body($view);
}
并将您的观点更新为:
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=980" />
<title>A-TRACK Tutoring</title>
<?php
if(isset($css))
{
foreach($css as $file){
echo "<link type='text/css' href='{$file}' rel='stylesheet' />";
}
}
?>
......
</head>