• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Page library

#1
[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:

Controller:

Code:
$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:

Code:
<html>
<head>

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

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

Outputs:

Code:
<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>

#2
[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!

#3
[eluser]NBrepresent[/eluser]
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.

#4
[eluser]sophistry[/eluser]
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?

#5
[eluser]NBrepresent[/eluser]
Oh, I seem to have resurrected a thread that's been dead for a while.

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

#7
[eluser]NBrepresent[/eluser]
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

#8
[eluser]NBrepresent[/eluser]
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...

#9
[eluser]anthrotech[/eluser]
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."

#10
[eluser]NBrepresent[/eluser]
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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.