Page 1 of 2
Populating page content..
Posted: Thu Aug 17, 2006 3:30 pm
by Benjamin
I built a small framework for a web site I am building.
app.php does the following:
- Loads configuration values
- Starts a session
- Instantiates an error logger
- Creates an autoloader
- Initializes the database connection
- Initializes a security system
- Initializes a user class which determines whether a client is logged in or not
- Initializes the rendering module
All the pages look like this..
Code: Select all
<?php
include 'includes/app.php';
$display->page_title = 'Welcome!';
$display->content = 'This is the content';
$display->display();
?>
My questions are:
- What is the best way to populate the $display->content?
- What is the best way to organize the classes so that I can dynamically create menu's based on the following factors. 1. The page they are viewing, 2. The type of user that is logged in. 3. Assign a custom order to the menu's
Posted: Thu Aug 17, 2006 3:36 pm
by feyd
MenuItem and Menu classes where MenuItem can contain other MenuItem objects. When adding MenuItems, it or some other class should determine if that item should infact exist based on your criteria. When and where this happens can be moved around, but I needed to convey the idea.
Posted: Thu Aug 17, 2006 7:23 pm
by Benjamin
Ok, now what would be the best way to create template files for each page and for the menus? I figure I can't just use a regular template because I need to buffer the content so I can cache pages when needed. I know I can buffer (ob_start, ob_end_flush) the templates and pull that into a variable but I'm thinking there is a better way.
Posted: Thu Aug 17, 2006 7:27 pm
by feyd
Better template classes. What I mean is, at least to me, a template class returns output generation to its View. Not necessarily calling the view, but just returning the resulting string(s) or an array, something.
Posted: Thu Aug 17, 2006 7:30 pm
by Benjamin
How would I apply that to forms, etc.. I just don't want the templates to look like this..
Code: Select all
$x = '<div class="box_top_center">' . "\n"
.' <div class="box_top_left">' . "\n"
.' <div class="box_top_right">' . "\n"
.' <h1 class="bar_title">' . $title . '</h1>' . "\n"
.' </div>' . "\n"
.' </div>' . "\n"
.'</div>' . "\n"
.'<div class="box_content">' . $content . '</div>' . "\n";
Posted: Thu Aug 17, 2006 7:37 pm
by feyd
I have rendering classes for each object type that can be rendered.
Posted: Thu Aug 17, 2006 8:40 pm
by Benjamin
Have any examples? I should have been more clear about what I was asking for.
Posted: Thu Aug 17, 2006 9:01 pm
by feyd
I don't have any examples I can go that'd make any lick of sense, so I'll try to explain how it all works.
Lets say I add a text field to the current window (page.) During rendering each renderable object is tallied into a distinct list of what render classes to load. Each of those classes is then loaded only once. When the renderer reaches the text field, it tells the text field render object to process the text field object. The render object interrogates the various settings of the text field. Information like it has a label, label text, label position, does it have hinting and so forth is pulled out and run through the standard output that covers that information. The output may be as basic as a standard <intput type="text">, but it could also be more complex with a <label> or some other control(s) attached to it.
All this guarantees me the same handling for that object 100% of the time. And that's what I'm making sure happens: consistency. Don't get me wrong, I have skins (templates and color changes) so I can change the visuals on a whim, but that doesn't change how the rendering is handled.
Re: Populating page content..
Posted: Fri Aug 18, 2006 12:16 am
by Christopher
You might want to try hierarchical Views. They can be as simple as templates within templates. For example, say to top level template looked like this:
Code: Select all
<html>
<body>
<div id="header">{header}</div>
<div id="leftmenu">{leftmenu}</div>
<div id="content">{content}</div>
</body>
</html>
Then super simplified code might be:
Code: Select all
$header->content = 'This is the header.';
$menu->content = '<a href="page1.php">Menu Item 1</a>';
$content->content = 'Hello world.';
$display->attach('header', $header);
$display->attach('menu', $menu);
$display->attach('content', $content);
$display->content = file_get_contents('top_level_template.html');
echo $display->render();
?>
Posted: Fri Aug 18, 2006 10:09 am
by Benjamin
Ok file_get_contents looks like the best way to go.
Then I just have to run replacement code on it. Would that be more efficient than
Code: Select all
$blah = <<<EOF
html html html blah
EOF;
What other options are used for this? What is the best way to go?
Posted: Fri Aug 18, 2006 10:30 am
by Benjamin
Ok I don't want to start a new thread, let me ask this a different way....
What is the best way to retain the ability to cache pages when using the MVC pattern?
Posted: Fri Aug 18, 2006 11:19 am
by Christopher
Why do you want to cache pages? I would suggest getting the design right and then adding caching as necessary if you have performance problems.
Posted: Fri Aug 18, 2006 11:37 am
by Benjamin
arborint wrote:Why do you want to cache pages? I would suggest getting the design right and then adding caching as necessary if you have performance problems.
I've got my reasons. I'm building a large site which will have a ~20gb database full of content that doesn't change very often. Since there would be a tremendous amount of data being requested from the database server, I'd like to minimize this by implementing a cache. The data will hardly ever change, so I might store the bulk of it in files instead of the database, then only store the reference in the database, but either way I would like to figure this out.
Posted: Sun Aug 20, 2006 8:17 am
by Yossarian
astions wrote:Ok I don't want to start a new thread, let me ask this a different way....
What is the best way to retain the ability to cache pages when using the MVC pattern?
I dont know if you are using your own MVC framework, but this comparison of well known frameworks shows 6 which it is claimed contain MVC and some kind of cacheing mechanism:
http://www.phpit.net/demo/framework%20c ... /chart.php
Most do. So it might be an idea to see how (where) they handle it.
Good question though, and one I need to look into very soon too.
Posted: Sun Aug 20, 2006 7:37 pm
by Christopher
Yossarian make an excellent point. In particular, using a Front Controller and an inherited base Action Controller gives you two clean points to inspect to request and decide of the requested page is in the cache or needs to be generated. If you architecture is designed right the caching layer can make that decision and if necessary turn on output buffering or grab the output from a Response object to transparently get the page and save it to the cache.