Welcome Guest, Not a member yet? Register   Sign In
Setting global variables for view files
#1

[eluser]Madmartigan1[/eluser]
Hello everyone,

This is my first post and first time using CI, so forgive my ignorance. I've been searching for a solution to this all morning.

What I'm trying to do is set some variables and make them available to ALL view files that are loaded from any controller. I'm using this for things like company name, copyright info, address and phone numbers that will appear on every page. I am used to doing this with a simple include from each "view" file, but it seems like I should be able to have CI automatically load this file and make the variables available to every page.

My normal "settings" file may look something like this:
Code:
<?php
//file settings.php
$MYcompanyName='WM Transportation Inc.';
$CompanyMotto='Always on time, every time.';
$defaultPageTitle=$MYcompanyName.': '.$CompanyMotto;
?>

I'd like the variable (or function) names to be short, easy to understand, and clean for use in my view files, like $company_motto, so something like getVar($data['config'][0]); simply won't do.

What is the best, most practical way to preform this simple task?

Thanks in advance!
#2

[eluser]cmark[/eluser]
You can make a config file for that pourpose. If you want autoload the config, you can add this to the autoload.
For access to the vars, you can use $this->config->item('item name')

All this and much more you can find at http://ellislab.com/codeigniter/user-gui...onfig.html
#3

[eluser]jedd[/eluser]
Have a read of [url="http://ellislab.com/forums/viewthread/109698/"]this thread[/url].

Unsurprisingly, this topic pops up fairly regularly - so you're in good company.

EDIT: I hasten to add - this is an extension to the solution you seek - and possibly a better one than just preparing variables ahead of time, and still having your final view dump them. Preparing mini-views, and having your final view just echo them, I think, is a cleaner approach. But it's probably six of one .. etc.
#4

[eluser]Madmartigan1[/eluser]
Awesome, thanks a lot guys. After spending the day learning CI and practically burning my eye sockets out from the monitor (brightness adjustment to the rescue!), I see that there are MANY ways to do this, but as usual it's debatable which is the "best" way.

I like the config file idea because it makes sense, but I don't like the awkward looking syntax needed to call the variable.

Jedd, I will look more into your method - it looks useful. I did read that post before I started this thread, but didn't quite understand it at the time.

Thanks a lot! I'm looking forward to learning more about CodeIgniter. This is my first time using a framework, and I have to say that it's just invaluable. Already saved about 2 hours in form validation time (:

I tried out MANY other frameworks over the past month and found CI to be the easiest to use, easiest to import my own code, and by far the fastest. Most of the other PHP frameworks I found were bloated and pretty much overkill for what I needed to do.

Good stuff.
#5

[eluser]Colin Williams[/eluser]
Quote:I like the config file idea because it makes sense, but I don’t like the awkward looking syntax needed to call the variable.

You can use config_item('key') for short. But I recommend going with the MY_Controller approach. Opens up a lot of opportunities.

In fact, CI should just ship with MY_Controller and the docs should instruct you to extend MY_Controller.
#6

[eluser]xwero[/eluser]
I don't think the MY_controller approach isn't the solution here. General data can appear everywhere on the page, multiple times. If it only appears only once on the page in a fixed place i would recommend to hardcode it. The data is not going to change every hour.

I would create a plugin function named company which contains all the data and returns pieces. Then in your view you have
Code:
<h1>&lt;?php echo company('name') ?&gt;</h1>
<h2>Address</h2>
<p>&lt;?php echo company('street_and_number') ?&gt;<br>
&lt;?php echo company('zip_and_city') ?&gt;
&lt;!-- and so on --&gt;
this makes is readable even for designers Wink
#7

[eluser]Colin Williams[/eluser]
That's fine, xwero, but you have just replaced the conventional 'config_item' function with a very limited 'company' function.
#8

[eluser]Madmartigan1[/eluser]
[quote author="xwero" date="1239453387"]I don't think the MY_controller approach isn't the solution here. General data can appear everywhere on the page, multiple times. If it only appears only once on the page in a fixed place i would recommend to hardcode it. The data is not going to change every hour.[/quote]

One of the main things I'm really trying to do is keep as many configuration settings in one file as possible, so that when I start a new project I only have to edit hopefully 1 file to get things rolling quickly, instead of hunting around here and there and all over the place.

Ideally, I'd like to consolidate database settings, server settings, data like company name and copyright begin date, default menu items, custom defaults for meta tags, etc. etc. into as few files as possible with as little code (of course) as possible. 75% of the time when starting a new project, I just upload my (old) framework and templates, import a default database, edit the master config file, and I'm working with the css and images in minutes.

Sorry, my first example was a bit too narrow and probably misleading. Nevermind the inconsistent naming style too, that was just for the example.
#9

[eluser]xwero[/eluser]
Colin you are right but the config items need unique names because you can't specify the file with the config_item function. I like the filename is groupname idea, classes work that way too.

Madmartigan1 i advise against one big config. file Even if you create similar sites there are going to be things that are different. I would put more time in creating an install script that creates and fills the files for you.
#10

[eluser]Madmartigan1[/eluser]
@xwero: Installation script sounds like a good idea, since 99% of the data will never change. I will look into that, thank you. If anyone can link to threads or docs that may assist in doing this, much appreciated. I can see creating a master config file in CI being more trouble than its worth.
Could you explain your reasons why you advise against using one big config file? Is this advice for CI specifically or is it general advice that applies to any application?




Theme © iAndrew 2016 - Forum software by © MyBB