doujinai2183 2017-05-23 01:26
浏览 41
已采纳

CodeIgniter 3.x - 使用用户输入重新加载具有不同CSS文件的视图

Problem:

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.

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.

Current Code:

Note: I've also set .htaccess to remove index.php from the URL.

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

<?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

<?php
$route['sakura/(:any)'] = 'sakura/$1';
$route['sakura'] = 'sakura';
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

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);
}

Desired URL Structure: <-- Not necessary, but would be great!

Default View: mywebsite.com/sakura/ -or- mywebsite.com/sakura/standard

Theme View: mywebsite.com/sakura/selected-theme

Final Thoughts:

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.

  • 写回答

2条回答 默认 最新

  • dsbc80836 2017-05-23 01:44
    关注

    Just a guess, but try something like...?

    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.
    

    View

    <link rel="stylesheet" href="<?php echo asset_url() ;?>css/<?php echo (!isset($css) ? 'standard' : $css);?>.css" media="screen" />
    

    Route

    Change the first line to:

    $route['sakura/(:any)'] = 'sakura/index/$1';
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置