mrjayviper
mrjayviper

Reputation: 2388

view file not displaying when I change the template and other issues (using Zend Framework 2)

I have this code in my controller file.

public function indexAction() { return new ViewModel(); }

It just displays index.phtml whose content is just:

<?php echo phpinfo();

And this works as intended.

I want to use the company's own look/style so I copied the the default template (module\Application\view\layout\layout.phtml) and placed the file into my own folder.

I made some changes to the 2 template files. On the default template, I added:

<hr>default<hr>

On my new template file, I added:

<hr>new<hr>

No other changes were made.

I then changed my controller function to this:

public function indexAction() {
    $viewModel = new ViewModel();
    $viewModel->setTemplate('directory/templates/flinders.phtml');

    return $viewModel;
}

My problem now is that it seems my index.phtml was never called. phpinfo() was never displayed on the browser unlike before.

and also, the browser displays the added text I made on the default and new templates. I thought using the setTemplate function would let me override the default template. So I assumed it should only display whatever changes I made on the new template. And even if I removed all the contents of the new template, my browser still displays the content of the default template.

Any ideas on how to fix my issue? thanks

Upvotes: 0

Views: 718

Answers (1)

mrjayviper
mrjayviper

Reputation: 2388

It seems I have misunderstood the meaning of template and layout.

template = the view file assigned to an action. indexAction would automatically look for index.phtml. If I change the template via setTemplate, the framework would not look for index.phtml but instead use whatever I supplied.

layout = the look/feel of all the pages in the website. $this->layout('some_phtml_file_here') would override the default layout.phtml that is supplied with ZF2 skeleton.

This website (http://zf2test.akrabat.com/) help me understand it much better!

Upvotes: 1

Related Questions