Welcome Guest, Not a member yet? Register   Sign In
Page library

[eluser]Al James[/eluser]
Hi there...

As discussed here: http://ellislab.com/forums/viewthread/49910/P45/#279320

I think that functions for linking css and js files should be broken out of the view library and put into a separate 'page' library.

I have written a basic page library. Features:

* Automatic linking of CSS and JS files (as per coolfactors idea in his view library)
* Ability to set title, description and keywords meta tags.
* Ability to pass values directly into the javascript environment (uses json_encode, so its PHP 5 or PHP 4 + the json extension)
* Ability to set javascript code to be executed in the window.onload function.

I will post the code in the next reply as its too long to include here!

Example usage:


$this->page->set_title('My page');
$this->page->set_description('My page is all about me!');



$data = array('greet' => 'Hello world!', 'bye' => 'Goodbye!');


In the view file:


<?php $this->page->do_head(); ?>




<title>My page</title>
<meta name="description" content="My page is all about me!"/>
<link rel="stylesheet" type="text/css" media="all" href="/css/my_css_file.css" />
<link rel="stylesheet" type="text/css" media="all" href="/css/another_css_file.css" />

<script type="text/javascript" src="/js/my_javascript_file.js"></script>
<script type="text/javascript" src="/js/another_javascript_file.js"></script>

<script type="text/javascript">
/* <![CDATA[ */
var greet="Hello world!";
var bye="Goodbye!";
/* ]]> */
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */


[eluser]Al James[/eluser]
Arg! Code too long to put here...

Here is a link: http://codeigniter.com/wiki/File:Page.zip/

Aside note: Why cant we attach PHP files to posts in this forum? That would be super useful!

This is the best!! Reeeaallly cleans up the header.php .

Thanks, Al.

One possible improvement would be if you added a parameter in the css_link method for media.

Al James! Nice to see you. Welcome back from Zend-land. ¿Qué Pasó allá?

A month ago you posted a farewell notice here in the sparks thread:


Does this mean you'll be picking up the "Sparks Library - Object Caching" project again?

Oh, I seem to have resurrected a thread that's been dead for a while.

Yep. No Problem. Al was a generous contributor. I got all fuddled up and forgot to read the dates too! :-) Welcome to CI, BTW!

Has anyone gotten the page library working? I just sent this email to Al to see whether he can shed some light on the config file issue:

Hi Al,

First, let me say thanks for your Page library. It looks really useful, and
I'm excited about using it.

The problem is that once I've added the library to /application/libraries/,
I get the following error message when I try to autoload the library:

"The configuration file page.php does not exist."

So, I tried adding an empty file called page.php in the config directory,
producing this error:

"Your page.php file does not appear to contain a valid configuration

So the big question would be, what should the config array contain? I
notice in the comments of the library, you mention the config once: "Set to
0 (can also be set from this libraries config file) to disable."

If this is an annoyance, I apologize, but based on the tone of some posts
on the CI board that I've read, I decided that you were a pretty affable

Hope to talk to you soon,


If anyone else is ever looking for a solution to this problem, commenting lines 36 + 38 of the library got things working for me. On to the next adventure...

What is the difference of this approach versus putting vars in a language file?

I've put all my metadata variables in a custom language file and called the objects via:



in my header.php file

Just curious...I am sure it's "Six One Way, Half a Dozen Another" type of thing. But I've only begun using CI, so I am curious to find out "best practices."

I guess that it's just more intuitive/readable to use the page library (really only because of the method names), but they both accomplish the same really.

Theme © iAndrew 2016 - Forum software by © MyBB