# Loading a template view into another view

**URL:** <https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304>\
**Category:** Need Help\
**Created:** [February 18, 2020, 4:18pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304 "2020-02-18T16:18:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 18, 2020, 4:18pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/1 "2020-02-18T16:18:02Z")

</div>

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??

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 18, 2020, 7:39pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/2 "2020-02-18T19:39:10Z")

</div>

Sounds like what you want maybe isn’t really a controller action, but rather a [cell](https://book.cakephp.org/3/en/views/cells.html).

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 18, 2020, 8:18pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/3 "2020-02-18T20:18:23Z")

</div>

Hi @Zuluru yes I tried the cell method first off, but couldn’t get it to work, then realized I was doing something wrong ☹ 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…

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 18, 2020, 8:23pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/4 "2020-02-18T20:23:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 18, 2020, 8:57pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/5 "2020-02-18T20:57:47Z")

</div>

> [@Zuluru](#):
>
> `debug($tfnnews` );

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??

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 18, 2020, 9:41pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/6 "2020-02-18T21:41:18Z")

</div>

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:

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

```

or, equivalently,

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

---

<div class="post-metadata">

**Author:** ![newbie](https://avatars.discourse-cdn.com/v4/letter/n/dc4da7/32.png) [@newbie](https://discourse.cakephp.org/u/newbie)\
**Post date:** [February 18, 2020, 10:14pm UTC](https://discourse.cakephp.org/t/loading-a-template-view-into-another-view/7304/7 "2020-02-18T22:14:22Z")

</div>

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
