Document::set('title', 'My Site');
// then somewhere else you can do this
Document::append('title', 'My Site');
//then in the view
echo Document::render('title');
// and get this
My Site : Page 2
// you can also prepend a value
Document::prepend('title', 'Some Text');
// set the title separator like this
Document::set('separator', ' :: ');
Here's an example of rendering the DTD:
// in a controller or anywhere else
Document::set('doctype', 'xhtml11');
//then in the view
echo Document::render('dtd');
// or you can do this
echo Document::render('dtd', 'xhtml11');
// or this
echo Document::render('doctype', 'xhtml11');
How about rendering a complete html opening tag:
Document::set('doctype', 'xhtml11');
Document::set('language', 'en');
Document::set('direction', 'ltr');
echo Document::render('htmlopen');
// to get this based on the doctype, language, and direction
// (looks like the editor stripped the xmlns but it's there)
<html xml:lang="en" dir="ltr">
The following Asset methods are wrapped: css, js, and img (for consistency since the original class had something similar).
// See the FuelPHP documentation for more information
// to add a stylesheet
Document::css('mycss.css');
// to render stylesheets
echo Document::css();
You could use it as a global registry:
// create a global variable
Document::set('myVar', 'myValue');
// access the global variable
$myVar = Document::get('myVar');
Create a container as an array and append/prepend values:
// Create the container
Document::set('myBucket', array());
// add some values to the container
Document::append('myBucket', 'myValue1');
Document::append('myBucket', 'myValue2');
// prepend a value to the container
Document::prepend('myBucket', 'myValueA');
For more information, see the inline documentation.
class ViewModel extends \Fuel\Core\ViewModel
{
function before()
{
$menu_items = array('menu_items' => Model_Sessions_Title::find('all',
array(
'where' => array(
array('classorder', '>', 0),
),
'order_by' => array('classorder' => 'asc'),
)
)
);
$this->header = View::forge('cms/partials/header');
$this->main_nav = View::forge('cms/partials/main_nav');
$this->sidebar_left = View::forge('cms/partials/sidebar_left', $menu_items);
$this->sidebar_right = null;
$this->footer = View::forge('cms/partials/footer');
Document::set('doctype', 'xhtml11');
Document::set('charset', 'UTF-8');
Document::set('title', 'CAN | Training');
Document::set('separator', ' | ');
Document::set('language', 'en');
}
}
As you an see, I'm setting up my layout and initializing document properties.
Here's what one of my layout views looks like:
<?php
Document::append('title', 'Courses');
echo Document::render('doctype');
echo Document::render('htmlopen')
?>
<head>
<?php echo Document::render('charset')?>
<?php echo Document::render('title')?>
<?php echo Document::css('template.css'); ?>
<?php echo Document::js(array('main.js', 'jsval.js')); ?>
</head>
<body id="page_bg">
<div class="wrapper">
<?php echo $header ?>
<?php echo $main_nav ?>
...
<?php echo $sidebar_left ?>
...
<?php echo $content ?>
...
<?php echo $sidebar_right ?>
...
<?php echo $footer ?>
</div>
</body>
</html>
Notice how the first line appends to the title. That can be done anywhere. In a controller, a view, a view model, or anywhere else. That's what started the idea of a document model. But like everything else, you create a feature and then feature creap sets in. Before you know it, you have a swiss army knife of capabilities.
Is it the most efficient approach? In terms of system performance, no. But it's not even distinguishable unless you're counting microseconds. But, in terms of development, time, and money, it is defintately an efficient approach.
I still have some work to do. I'm not satisfied with how the Asset library works. I'll be tweaking the model to allow prepending as well as appending to css and js containers. The original did that but I decided to simply wrap Asset methods. I may rethink that.
And oh yea, since it's a static container model, it's globally accessable so you can pretty much use it anywhere for any type of storage you need. It doesn't have to store just document properties. // anywhere in your code:
Document::set('css', 'template.css');
Document::set('js', array('main.js', 'jsval.js'));
// in a view or anywhere else:
// add some css
Document::append('css', 'more_css.css');
// prepend some javascript
Document::prepend('js', 'i_come_first.js');
//render in the view
echo Document::render('css');
echo Document::render('js');
// you can also pass something straight to the render method
echo Document::render('css', 'mycss.css');
You can accomplish the same thing without using the render method:
echo Document::css(Document::get('css));
Why did I go to the trouble? Maybe I missed something. But I spent a few hours trying to get this functionality out of the native Asset class. Just couldn't make it do what I wanted it to do.
It looks like you're new here. If you want to get involved, click one of these buttons!