Jump to content

Strange error using $files->render() vs $files->include() with "nested" $files->include() statements


LAPS
 Share

Recommended Posts

Hi, in a template file I successfully used $files->include():

File: /site/templates/a-template-file.php

<?php namespace ProcessWire;
$files->include('partials/file-name.php');
// ...

This loads the /site/templates/partials/file-name.php file containing other ("nested") $files->include() statements, for instance:

File: /site/templates/partials/file-name.php

<?php namespace ProcessWire;
$files->include('partials/other-file-name.php')
// ...

 

Now, in the template file, I would like to use $files->render() (instead of $files->include()) to retrieve output as a return value:

File: /site/templates/a-template-file.php

<?php namespace ProcessWire;
$output = $files->render('partials/file-name.php');
// ...

But, by using the render() (instead of the include()) function, I get the error message coming from within the rendered /site/templates/partials/file-name.php file:

Quote

Error: Exception: ___include: File does not exist: ./partials/other-file-name.php
In /wire/core/WireFileTools.php line 1947

I think the problem is the "./" as shown in the above error message.

Note: In the rendered /site/templates/partials/file-name.php file, I tried to use the "nested" $files->include() with the allowedPaths option set (i.e. $files->include('partials/other-file-name.php', [], ['allowedPaths' => [$config->urls->templates]])) and many other things, without success.

 

How can I solve the problem in order to use the render() (instead of the include()) function?

Link to comment
Share on other sites

  • LAPS changed the title to Strange error using $files->render() vs $files->include() with "nested" $files->include() statements
5 hours ago, bernhard said:

Did you try this?

echo $files->render('other-file-name.php');

@bernhard, your code works.

Is there any reason for the code to work with render() and not with include()? I would expect that it works when I render() a template that has include() functions.

Link to comment
Share on other sites

Your error tells you that the requested file does not exist. If the file exists, then it means that your provided path is wrong.

You seem to include/render "partials/foo.php", but if you are already rendering/including a file in the partials folder, than from within that file you cant render/include "partials/foo.php" because that would look for "partials/partials/foo.php". That's why you just provide "foo.php" and it should work.

If you use render() than you can RETURN something in a file and that will be returned to the place where you made the $files->render() call. If you do an "echo $files->render(...)" then it will echo the output. If you do $foo = $files->render(...); you can do whatever you want with $foo after that call.

If you use include() on the other hand the code in the included file will directly be executed and nothing will be returned, so you can't use include() to populate variables with the content of a file.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...