EllisLab text mark
Advanced Search
     
Page library
Posted: 19 July 2007 05:57 AM
Joined: 2007-07-14
26 posts

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:

Controller:

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

$this->page->js_link('/js/my_javascript_file.js');
$this->page->js_link('/js/another_javascript_file.js');

$this->page->css_link('/css/my_css_file.css');
$this->page->css_link('/css/another_css_file.css');


$data = array('greet' => 'Hello world!''bye' => 'Goodbye!');
$this->page->js_set($data);

$this->page->js_init('alert(greet);')

In the view file:

<html>
<
head>

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

</head>
<
body>
Content
</body>
</
html

Outputs:

<html>
<
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>
<script type="text/javascript">
/* <![CDATA[ */
window.onload=function(){
alert
(greet);
}
/* ]]> */
</script>
 

</head>
<
body>
Content
</body>
</
html
 
Posted: 19 July 2007 06:00 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-07-14
26 posts

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!

 
Posted: 10 December 2007 04:50 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-12-10
25 posts

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.

 
Posted: 10 December 2007 05:06 PM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

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:

http://ellislab.com/forums/viewthread/56456/P20/

Does this mean you’ll be picking up the “Sparks Library - Object Caching” project again?

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 10 December 2007 05:34 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-12-10
25 posts

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

 
Posted: 10 December 2007 05:55 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

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

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

 
Posted: 10 December 2007 06:54 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-12-10
25 posts

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
array.”

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
guy.

Hope to talk to you soon,

Perry

 
Posted: 10 December 2007 09:10 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2007-12-10
25 posts

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…

 
Posted: 13 December 2007 10:24 AM   [ # 8 ]   [ Rating: 0 ]
Joined: 2007-12-12
17 posts

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:

$this->lang->line(‘some_var’)

<?=$this->lang->line(‘some_var’)?>

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.”

 
Posted: 13 December 2007 10:38 AM   [ # 9 ]   [ Rating: 0 ]
Joined: 2007-12-10
25 posts

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.

 
Posted: 20 April 2011 11:04 AM   [ # 10 ]   [ Rating: 0 ]
Joined: 2011-04-19
6 posts

Thank you so much for this code. It really is much more intuitive, and a lot cleaner than putting variables in a language file. I am very new to this stuff, and I am excited about this little discovery. I am learning quite a bit from this forum, and I am very grateful for all of you who make time to help those of us in need.