Welcome Guest, Not a member yet? Register   Sign In
help needed adding CSS and Images
#1

[eluser]learning_php[/eluser]
Hi,

I am new to CI and have followed the two video tutorials and would like some help adding a stylesheet and some images to the project. I have looked a a few post but I am still unsure how to get this working.

Thanks
#2

[eluser]lbolognini[/eluser]
[quote author="learning_php" date="1238863079"]Hi,

I am new to CI and have followed the two video tutorials and would like some help adding a stylesheet and some images to the project. I have looked a a few post but I am still unsure how to get this working.

Thanks[/quote]

I'm coming from Django so this might sound a bit of a django-ism to CI people.
a value on the config.php:

$config['site_media'] = 'site_media'; //css, js, images, etc...

Then from my Controller

$this->mysmarty->assign('site_media', base_url() . $this->config->item('site_media'));

Which is being passed to a Smarty template

You might have a problem with your htaccess though if you're doing url rewriting

L.
#3

[eluser]Colin Williams[/eluser]
All you really need to involve PHP (or CI) for is keeping the paths dynamic. Nothing really has to happen in the controller (unless there is some front end for changing the paths).

Code:
$config['img_base'] = 'http://cdn.example.com/images/';
$config['css_base'] = 'http://www.example.com/css/';

Code:
<link rel="stylesheet" type="text/css" href="<?php print config_item('css_base') ?>style.css" />

Code:
<img alt="" src="&lt;?php print config_item('img_base') ?&gt;smile.gif" />

You could go further and create helpers:

Code:
function get_path($path, $type = 'img')
{
   return config_item($type .'_base') . $path;
}

then:
Code:
&lt;link rel="stylesheet" type="text/css" href="&lt;?php print get_path('style.css', 'css') ?&gt;" /&gt;
Code:
<img alt="" src="&lt;?php print get_path('smile.gif') ?&gt;" />
#4

[eluser]djreed[/eluser]
This seems like an intuitive solution. However, I have one more question. When using the anchor() function in the URI helper, how do you indicate your custom config prefix for a link? For example:
Quote:&lt;?php echo anchor('rent','<img src="images/my_image.jpg" />');?&gt;

How would I keep the above path to the image dynamic?

Thanks for your help,

-Danny
#5

[eluser]Thorpe Obazee[/eluser]
Code:
&lt;?php echo anchor(‘rent’,$your_variable_here);?&gt;

This would work if you want it dynamic.
#6

[eluser]djreed[/eluser]
Great thanks! Fast response Wink
#7

[eluser]djreed[/eluser]
One more question: What would be the contents of the variable being passed to anchor() be and how would you incorporate the image prefix from the config file into this?

I have come up with my own solution but it seems rather messy. I feel like there is an easier way to do this that I am not thinking of...

-Danny
#8

[eluser]Thorpe Obazee[/eluser]
It depends on how your application works. Is the path stored in the database that is based on some username or category?
#9

[eluser]djreed[/eluser]
No they are coming from the config file like described above:
Code:
$config['img_base'] = 'http://cdn.example.com/images/';
$config['css_base'] = 'http://www.example.com/css/';
#10

[eluser]Thorpe Obazee[/eluser]
if you used Colin's helper, then it's just the same. You just need to have a variable in it.

Code:
<img alt="" src="&lt;?php print get_path($yourvariable) ?&gt;" />




Theme © iAndrew 2016 - Forum software by © MyBB