CodeIgniter 3.x-使用用户输入重新加载具有不同CSS文件的视图 [英] CodeIgniter 3.x - Reload View with Different CSS File Using User Input
问题描述
问题:
我创建了一个静态页面视图(在其自己的视图文件夹中),该页面演示了新的Sakura.css框架。该框架带有四个CSS主题。我希望能够从链接列表中选择一个主题,并通过控制器使用关联的CSS主题文件重新加载相同的视图。 我还需要将视图默认设置为'standard.css'主题。最后,我想避免使用JavaScript / jQuery解决方案。
I created a static page view (in its own view folder) that demos the new Sakura.css framework. The framework comes with four CSS themes. I want to be able to select a theme from a list of links and reload the same view with the associated CSS theme file via the controller. I also need the view to default to the 'standard.css' theme. Finally, I'd like to avoid JavaScript/jQuery solutions.
我正在努力掌握控制器功能名称,自定义路由和URI之间的关系。我想我正处于 aha时刻的边缘,找到解决该问题的方法肯定会有所帮助。
I'm struggling to grasp the relationship between controller function names, custom routing, and the URI. I think I'm at the brink of an 'aha' moment, and finding a solution this issue out is definitely going to help.
当前代码:
注意:我还设置了.htaccess以便从URL中删除index.php。
views / sakura / index.php
views/sakura/index.php
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Sakura Demo</title>
<link rel="stylesheet" href="<?php echo asset_url() ;?>css/<?php echo $theme; ?>.css" media="screen" />
</head>
<body>
<header>
<nav>
<ul>
<li><a <?php if (isset($theme) && ($theme = 'standard')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/standard'); ?>">Standard</a></li>
<li><a <?php if (isset($theme) && ($theme = 'dark')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/dark'); ?>">Dark</a></li>
<li><a <?php if (isset($theme) && ($theme = 'earthly')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/earthly'); ?>">Earthly</a></li>
<li><a <?php if (isset($theme) && ($theme = 'vader')) { echo 'class="current"'; } ?>href="<?php echo base_url('sakura/vader'); ?>">Vader</a></li>
</ul>
</nav>
</header>
<main>
<h1>Sakura.css Demo</h1>
<p>Sakura.css is a minimal css framework/theme that can be dropped into any project for an instantaneous modern-looking website.</p>
</main>
<footer>
<p>Footer Text</p>
</footer>
</body>
</html>
controllers / Sakura.php
controllers/Sakura.php
<?php
class Sakura extends CI_Controller {
public function theme($theme)
{
$data['theme'] = array(
'standard' => "standard",
'dark' => "dark",
'earthly' => "earthly",
'vader' => "vader"
);
$this->load->view('sakura/theme', $data);
}
}
config / routes.php
config/routes.php
<?php
$route['sakura/(:any)'] = 'sakura/$1';
$route['sakura'] = 'sakura';
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
我能够完全加载视图的唯一方法是使用此功能Sakura.php控制器:
The only way I've been able to get the view to load at all is with this function in the Sakura.php controller:
public function index()
{
$data['theme'] = "standard";
$this->load->view('sakura/index', $data);
}
所需的URL结构:<-不
默认视图:mywebsite.com/sakura/ -或- mywebsite.com/sakura/standard
Default View: mywebsite.com/sakura/ -or- mywebsite.com/sakura/standard
主题视图:mywebsite.com/sakura/selected-theme
Theme View: mywebsite.com/sakura/selected-theme
最终想法:
在此问题上,感谢大家的帮助。我经常依赖StackOverflow社区,我非常感谢解决我的问题所花费的时间和考虑。
Thank you all in advance for any help on this issue. I rely on the StackOverflow community often and I greatly appreciate the time and consideration that goes into solving my issues.
推荐答案
只是一个猜测,但是尝试类似...?
Just a guess, but try something like...?
Controller
Controller
$css = $this->uri->segment(2);
$allowed_input = array('standard', 'dark', 'earthly', 'vader');
if ((isset($css)) && (!in_array($css, $allowed_input))) {
echo 'Filthy human!';
}
$data['css'] = $css; // You can probably just do this above, it's just harder to type.
查看
<link rel="stylesheet" href="<?php echo asset_url() ;?>css/<?php echo (!isset($css) ? 'standard' : $css);?>.css" media="screen" />
路线
将第一行更改为:
$route['sakura/(:any)'] = 'sakura/index/$1';
然后mywebsite.com/sakura/standard应该可以,但是如果 standard位不起作用在那里,它将默认加载标准。放入in_array()位只是一个小小的完整性检查,但您可能想更全面地进行验证。
Then mywebsite.com/sakura/standard should work, but if the "standard" bit isn't there, it'll load the standard by default. Threw in the in_array() bit as a small sanity check, but you'll probably want to be more thorough in your validation.
这篇关于CodeIgniter 3.x-使用用户输入重新加载具有不同CSS文件的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!