Saturday, September 24, 2011

Re: Model Data on the home.ctp

Here's my code and the errors that cakephp gives me:
class PagesController extends AppController {
var $name = 'Pages';
var $helpers = array('Html');
var $uses = array('Post');

function display() {
$path = func_get_args();

$count = count($path);
if (!$count) {
$this->redirect('/');
}
$page = $subpage = $title_for_layout = null;

if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count -
1]);
}
//$posts = $this->Post->find('all');
$this->set(compact('page', 'subpage',
'title_for_layout'));
$this->render(implode('/', $path));
}

function home(){
$posts = $this->Post->find('all');
$this->set(compact('posts'));
}
}


//home.ctp
<table cellpadding="0" cellspacing="0">
<?php
$i = 0;
foreach ($posts as $post):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $post['Post']['id']; ?>�</td>
<td><?php echo $post['Post']['title']; ?>�</td>
</td>
</tr>
<?php endforeach; ?>
</table>

//cakephp errors
Notice (8): Undefined variable: posts [APP\views\pages\home.ctp,
line 4]
Warning (2): Invalid argument supplied for foreach() [APP\views
\pages\home.ctp, line 4]

On Sep 24, 12:09 pm, Charles Blackwell <charlesblackwell...@gmail.com>
wrote:
> So it looks like the $uses attribute will do the trick. But, are those
> models that I enable via the attribute available to all of my pages in
> the pages directory? Say for example, I created an about.ctp. Would
> the model be available in that about view as well?
>
> Charles
>
> On Sep 24, 11:22 am, Charles Blackwell <charlesblackwell...@gmail.com>
> wrote:
>
>
>
>
>
>
>
> > How can I display data on the home.ctp template? Do I use the set
> > method on the pages controller?

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: