Loading a template view into another view

Hello,

not sure if I’m going mad or just plain stupid? I’m trying to create a news feed using CakePHP 4.x to display the latest news in the footer of the site elements/standardfooter.php

I have ran the batch script cake bake all TfnNews which has created MCV for the TfnNews, In templates/TfnNews I have created a file called latest.php and put some basic code for testing and added the following code to src/Controller/NewsController.php
public function latest($id = 1)
{
$tfnNews = $this->TfnNews->get($id, [
‘contain’ => [],
]);

        $this->set('tfnNews', $tfnNews);
    }

latest.php code
<?= h($tfnNews->title) ?>

I have tried everything to load it in the the footer to display NOTHING???

I can view the content of latest in the URL /news/latest but can’t get it to load in the in the footer??

how do I actually load the latest news in to the footer??

Sounds like what you want maybe isn’t really a controller action, but rather a cell.

Hi @Zuluru yes I tried the cell method first off, but couldn’t get it to work, then realized I was doing something wrong :frowning: anyway I revisited the cell option after realizing my initial mistake and got it working in a fashion…
I’m trying to load the follow from src/View/Cell/TfnNewsCell.php into /templates/cells/TfnNews/display.php
but getting the following error?
Undefined variable: title [ **ROOT\templates\cell\TfnNews\display.php** , line **1** ]

src/View/Cell/TfnNewsCell.php code.

<?php

declare(strict_types=1);

namespace App\View\Cell;

use Cake\View\Cell;

/**

 * TfnNews cell

 */

class TfnNewsCell extends Cell

{

    /**

     * List of valid options that can be passed into this

     * cell's constructor.

     *

     * @var array

     */

    protected $_validCellOptions = [];

    /**

     * Initialization logic run at the end of object construction.

     *

     * @return void

     */

    public function initialize(): void

    {

    }

    /**

     * Default display method.

     *

     * @return void

     */

    //protected $limit = 1;

    public function display()

    {

        $this->loadModel('TfnNews');

        $tfnNews = $this->TfnNews->find('all', [

            'order' => ['TfnNews.created' => 'DESC'],

            'conditions' => ['TfnNews.status' => '1'],

             'limit' => 1

        ]);

        $this->set('tfnnews', $tfnNews);

     

    }

}

/templates/cells/TfnNews/display.php

<?= h($tfnnews->title) ?>

Could you please explain what I’ve done wrong as all I want it to do is load the last news article where status equals 1

my head hurts so much at the moment, but getting there…

The query you’ve done will give you a query for a record set with one record in it. You’re then trying to reference the title attribute of that query object. (Prove this to yourself with debug($tfnnews);` in your template.)

If you want only the first record, then remove the limit clause, and add ->first() at the end of the find call. That will give you just a single entity, which you can then get the title of.

I’m sorry but you have totally lost me? what exactly do I have to write in the controller and display to get it to actually work??

That line is a debugging tool, you can put it in your template to see exactly what’s in that variable. I try to help people learn how to debug their own code.

What you need in your controller is to change your find to:

$tfnNews = $this->TfnNews->find('all', [
    'order' => ['TfnNews.created' => 'DESC'],
    'conditions' => ['TfnNews.status' => '1'],
])->first();

or, equivalently,

$tfnNews = $this->TfnNews->find()
    ->order(['TfnNews.created' => 'DESC'])
    ->where(['TfnNews.status' => '1'])
    ->first();
1 Like

Thanks that makes a lot more sense that your first answer, as I can clearly see how it is constructed, making it easier to follow.

I understand what you mean about getting people to debug their own code, but reading the book it is confusing sometimes as there always seems 5 different solutions to one way of doing something, but there never seems to be one clear full examples from start to finish bit of code to look at to make any sense to a simpleton like myself.

once I see something like the above easy to read makes more sense than spending 4 hours in the book trying to piece things together…

Thanks @Zuluru for the help it means a lot been able to see it how it should be written.

Cheers