Welcome Guest, Not a member yet? Register   Sign In
Loading stylesheets with Smarty and CodeIgniter
#1

[eluser]1cookie[/eluser]
Hi

From a project I was working on a colleague was using a tricky piece of code to loop through some stylesheets initialized in the calling controller, like thus:

Code:
// header.tpl
{* CSS *}
        {foreach from=$ci->page_info.css item=css_file}<link type="text/css" rel="stylesheet" href="/assets/css?from={$css_file}&_t=admin" />{/foreach}

And my calling controller:

Code:
// page.php
class Page extends MY_Controller {

      public function __construct() {
               parent::__construct();

                $this->page_info['css'][] = "admin.css";
                $this->page_info['css'][] = "reset.css";
       }

       public function index(){
            
                $this->smarty->view("admin/home.tpl", (array)$data);
        }
        
}

Naturally my full view/template would look something like:

Code:
// admin/home.tpl
{include file="header.tpl"}

<!-- content here -->

{include file="footer.tpl"}

Problem is:

Code:
href="/assets/css?from={$css_file}&_t=admin"
doesn't build the correct HTML links? From Firebug:

Code:
<link href="/assets/css?from=reset.css&_t=admin" rel="stylesheet" type="text/css">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Index of /assets/css&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>Index of /assets/css</h1>
<table>
<tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
<tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[PARENTDIR]"></td><td><a href="/assets/">Parent Directory</a></td><td>&nbsp;</td><td align="right"> - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="admin.css">admin.css</a></td><td align="right">2014-03-15 15:08 </td><td align="right">268 </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="style.css">style.css</a></td><td align="right">2014-03-11 14:32 </td><td align="right">4.1K</td><td>&nbsp;</td></tr>
<tr><th colspan="5"><hr></th></tr>
</table>
<address>Apache/2.4.6 (Ubuntu) Server at localhost Port 80</address>
&lt;/body&gt;&lt;/html>
&lt;/link&gt;

This is simply Apache displaying the contents of '/assets/css'. Do you understand how the above would build the correct HTML links? i.e.

Code:
&lt;link type="text/css" rel="stylesheet" href="/assets/css/admin.css&_t=admin" /&gt;
&lt;link type="text/css" rel="stylesheet" href="/assets/css/style.css&_t=style" /&gt; etc...

Have you seen this technique used before?

Just to add to that, I *can* loop through the above array successfully in my view. For example:

Code:
{foreach from=$ci->page_info.css item=css_file}{$css_file}{/foreach}

will print:

Code:
// admin/home.tpl
admin.css style.css etc...

in the browser.

#2

[eluser]boltsabre[/eluser]
From what I can see, you don't need this: ?from=

So
Code:
href="/assets/css?from={$css_file}&_t=admin"
becomes
Code:
href="/assets/css/{$css_file}&_t=admin"
#3

[eluser]1cookie[/eluser]
Yes, that's one way of looking at it - albeit a compromise as opposed to the original.




Theme © iAndrew 2016 - Forum software by © MyBB