Welcome Guest, Not a member yet? Register   Sign In
A document model
#1

[eluser]wr5aw[/eluser]
Some time ago, I created some methods to manipulate and render various pieces of an html document. It's evolved into a pseudo document model. I've been using it in some CI projects and it really comes in handy.

The model is a collection of properties and containers with methods to render tags or groups of tags. I also wanted to be able to append or prepend items so they could be rendered in a specific order.

Here an example of working with the page title:
Code:
$this->document->title = 'My Site';

// then somewhere else you can do this
$this->document->appendTitle('Page 2');

//then in the view
echo $this->document->render('title');

// and get this
<title>My Site : Page 2</title>

Here's an example of rendering the DTD:
Code:
// in a controller or anywhere else
$this->document->doctype = 'xhtml11';

//then in the view
echo $this->document->render('doctype');

// or you can do this (same as CI doctype())
echo $this->document->doctype('xhtml11');

How about rendering a complete html opening tag:
Code:
echo $this->document->render('htmlopen');

// to get this based on the doctype, language, and direction
// (looks like xmlns got stripped from the post but it's there)
<html xml:lang="en" dir="ltr">

Let's add some stylesheets:
Code:
$this->document->addStylesheet('/path/to/css/print.css', 'print');
$this->document->addStylesheet('/path/to/css/controller.css');
$this->document->addStylesheet('/path/to/css/method.css', 'all');
$this->document->prependStylesheet('/path/to/css/site.css');

// then in the view
echo $this->document->render('stylesheets');

// to get this
<link rel="stylesheet" type="text/css" href="/path/to/css/site.css" />
<link rel="stylesheet" type="text/css" href="/path/to/css/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="/path/to/css/controller.css" />
<link rel="stylesheet" type="text/css" href="/path/to/css/method.css" media="all" />

There are seven core containers - title, metas, stylesheets, css blocks, scripts, script blocks, and bottom script blocks (really just another script blocks container). All of the containers can be appended or prepended and have render methods.

Then there are a number of properties including doctype, character set, language, language direction, meta description, meta keyword, etc. Doctype, character set, meta description and meta keywords all have render methods.

Finally, there's the ultimate lazy view:
Code:
echo $this->document->render('doctype');
echo $this->document->render('htmlopen');
echo $this->document->render('head');
<body>
  <h1>Hello World!</h1>
&lt;?php echo $this->document->render('scriptblocksbottom'); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

I was thinking about posting it on github. If you're interested, post back and I'll go ahead and put it in the wild.
#2

[eluser]wr5aw[/eluser]
Pushed to github - https://github.com/alwarren/codeigniter-document_model

Still haven't added the renderHead() method that I cited in the last example. Should only take a few minutes but I'm feeling a little lazy. No docs either. The original post should be self-explanatory.




Theme © iAndrew 2016 - Forum software by © MyBB