We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Getting "Wrong number of parameters" error when trying to render view?

The title says it all. I've turned off automatic view rendering and I'm trying to render a view from the indexAction method of my IndexController class with the following code: echo $this->view->render('index');.

One suspected cause of the problem might be the configuration in my services file. I have uploaded that here: https://pastebin.com/uNRpJT5T



125.8k
Accepted
answer

I believe the documentation is wrong with regard to this method. I think you need to provide both a controller name and action name, so:

echo $this->view->render('index','index');

I really dont understand why i need to supply both of those. Surprisingly, it works. Now I have a new problem. Phalcon only renders the index view. For example if my hierarchy is;

  • /views
  • /test
  • /test/index.volt
  • index.volt

And I change echo $this->view->render('index', 'index'); to echo $this->view->render('test', 'index'); it still renders /views/index.volt

I believe the documentation is wrong with regard to this method. I think you need to provide both a controller name and action name, so:

echo $this->view->render('index','index');

You have to supply both of those because render() doesn't know what view file you're talking about when you just provide 'index'. Volt works on the concept of [folder name]/[view template file] - so you need to provide both of those.

As to your second problem - have you set up Volt in your bootstrap file? What directory did you say all your template files were in? I'm guessing you said '/views'. By default, if Volt can't find the template file you specify, it tries to find the index file for the directory you specified. If it can't find that, it tries to display the base index file. Since there is no '/views/test' directory, or a '/views/test/index.volt' file, it just displayed '/views/index.volt'. Try moving the 'test' directory inside '/views'.

Your explanation is valid, it's just Phalcon's documentation led me to believe the render function takes one argument being 'folder/view' instead of 2.

Sorry but my directory structure is unclear. I have a views folder. Inside the views folder is an index.volt file and a directory called test. Inside the test directory is an index.volt file. I know the render function can find test/index.volt because a file with similar contents shows up in the app/cache directory, but it still displays views/index.volt which is strange behavior. And yes, I have Volt setup in my services file.

Sorry for taking up your time, but this is a problem which is halting progress on my project.

You have to supply both of those because render() doesn't know what view file you're talking about when you just provide 'index'. Volt works on the concept of [folder name]/[view template file] - so you need to provide both of those.

As to your second problem - have you set up Volt in your bootstrap file? What directory did you say all your template files were in? I'm guessing you said '/views'. By default, if Volt can't find the template file you specify, it tries to find the index file for the directory you specified. If it can't find that, it tries to display the base index file. Since there is no '/views/test' directory, or a '/views/test/index.volt' file, it just displayed '/views/index.volt'. Try moving the 'test' directory inside '/views'.

So your structure is like this:

  • views/
    • index.volt
    • test/
      • index.volt

Is it possible the desired file is cached? That is - at some point you tried to access views/test/index.volt, it wasn't found so Volt displayed views/index.volt and cached that? Try emptying your cache.

Yes, I'm using that structure. I cleared my cache and when i try to render test/index.volt, it caches both test/index.volt and views/index.volt and displays index.volt. Anyway I did some testing and it appears views/index.volt has precedence over all the other views. I wonder if this is a Phalcon 1.3.4 behavior (or error). If I delete views/index.volt and run render('test', 'index') it renders test/index.volt (the one i wanted!).

As a workaround my new directory structure is:

  • views/
    • index/
      • index.volt
    • test/
      • index.volt

And now the views render like they're supposed to.



98.9k

If you are using Phalcon\Mvc\View it must be echo $this->view->render('index', 'index');. if you're using Phalcon\Mvc\View\Simple it must be echo $this->view->render('index');.

Yes, I'm using that structure. I cleared my cache and when i try to render test/index.volt, it caches both test/index.volt and views/index.volt and displays index.volt. Anyway I did some testing and it appears views/index.volt has precedence over all the other views. I wonder if this is a Phalcon 1.3.4 behavior (or error). If I delete views/index.volt and run render('test', 'index') it renders test/index.volt (the one i wanted!).

As a workaround my new directory structure is:

  • views/
    • index/
      • index.volt
    • test/
      • index.volt

And now the views render like they're supposed to.



125.8k
Accepted
answer

I'm guessing the behaviour you stated is expected. For what it's worth, I always have my views directory laid out like you do - all files in subdirectories.

edited Nov '14

I am using Phalcon\Mvc\View, but the main problem is that having a top level .volt file in views seems to be rendered always regardless of what arguments are supplied to render().



98.9k

Most of the time you don't have to call render manually because this is automatically done according to the latest controller and action used. If you want to obtain the result of executing a view you can use getRender: https://github.com/phalcon/vokuro/blob/master/app/library/Mail/Mail.php#L65-L67

I am using Phalcon\Mvc\View, but the main problem is that having a top level .volt file in views seems to be rendered always regardless of what arguments are supplied to render().

Yes, I've turned off that automatic view rendering. Anyway the new directory structure solves the problem. Thanks for all your help guys.

Most of the time you don't have to call render manually because this is automatically done according to the latest controller and action used. If you want to obtain the result of executing a view you can use getRender: https://github.com/phalcon/vokuro/blob/master/app/library/Mail/Mail.php#L65-L67

I am using Phalcon\Mvc\View, but the main problem is that having a top level .volt file in views seems to be rendered always regardless of what arguments are supplied to render().