Jump to content

Include error on AJAX request


a-ok
 Share

Recommended Posts

I apologise in advance if this is more a 'PHP' issue than a 'PW' issue.

I have an include in one of my templates:

foreach ($query as $item) {
    include('./inc/item__project--filter.inc');
}

And within this include I have another include (as I tend to build in re-usable modules):

include("./slider__v--horz.inc");

Both includes are within the `/inc/` folder within templates.

If I visit this page then it all renders fine, but when I use an AJAX request to render the first include (when the selector for the page changes), it returns an error:

PHP Warning: include(./inc/slider__v--horz.inc): failed to open stream: No such file or directory in .../www/lucent-lighting/inc/item__project--filter.inc:10

On the template that both these includes are being used (projects.php) I am executing the following check with the AJAX request (GET) is fired

if ($this->input->get->action == 'filterEndpoint') {

	$inputExists = $this->input->get->inputExists;
	$filterEndpoint = $this->input->get->filterEndpoint;

	if ($this->input->get->pageTemplate == 'projects') {

		if ($inputExists !== "") { //
			$query = $pages->find("template=products-filters-single, products_filters_projects.count>0, include=all, sort=sort, {$filterEndpoint}");
		} else {
			$query = $pages->find("template=projects-filters-single, projects_filters_projects.count>0, include=all, sort=sort, {$filterEndpoint}");
		}
		$itemTemplate = './inc/item__project--filter.inc';
		//bd($query);
	}

	$output = '';
	foreach ($query as $item) {
		$output .= wireRenderFile($itemTemplate, array('item' => $item, 'inputExists' => $inputExists));
	}

	return json_encode(['html' => $output]);

}

Any thoughts?

Link to comment
Share on other sites

4 hours ago, a-ok said:

include("./slider__v--horz.inc");

Both includes are within the `/inc/` folder within templates.

You can either try 

include $config->paths->templates . 'inc/item__project--filter.inc';

or:

include __DIR__ . '/inc/item__project--filter.inc';

 

Edited by szabesz
EDIT: removed not needed / (see below)
  • Like 5
Link to comment
Share on other sites

2 hours ago, szabesz said:

include $config->paths->templates . '/inc/item__project--filter.inc';

I think this should be

->templates . 'inc

because all paths should already have a trailing slash whereas __DIR__ does not have one. But I guess your example should also work ? 

  • Like 2
Link to comment
Share on other sites

5 minutes ago, bernhard said:

because all paths should already have a trailing slash whereas __DIR__ does not have one. But I guess your example should also work ? 

Sure! Thanks for fixing my copy-paste error! (BTW, both should work as an extra / does not make PHP trip over, still it is unnecessary for sure).

Link to comment
Share on other sites

16 minutes ago, a-ok said:

Is this a more preferred way?

Not really. It depends on how you find something easier to implement. Usually I use include but I try to avoid relative paths. There is a "problem" with them: http://yagudaev.com/posts/resolving-php-relative-path-problem/ 

BTW dirname('__FILE__') is the same as __DIR__

I only use wireRenderFile() when I want to store the rendered output in a variable, and I use this technique to avoid the hassle of passing lots of variables: https://processwire.com/talk/topic/14332-storing-templates-in-separate-directories/?do=findComment&comment=129024

 

Edited by szabesz
typo
  • Like 1
Link to comment
Share on other sites

1 hour ago, szabesz said:

Not really. It depends on how you find something easier to implement. Usually I use include but I try to avoid relative paths. There is a "problem" with them: http://yagudaev.com/posts/resolving-php-relative-path-problem/ 

BTW dirname('__FILE__') is the same as __DIR__

I only use wireRenderFile() when I want to store the rendered output in a variable, and I use this technique to avoid the hassle of passing lots of variables: https://processwire.com/talk/topic/14332-storing-templates-in-separate-directories/?do=findComment&comment=129024

 

Thanks, this is really great and helped me loads.

Is there a reason you use $config->paths over $config->urls?

  • Like 1
Link to comment
Share on other sites

9 minutes ago, a-ok said:

$config->paths over $config->urls?

Sure, absolute vs relative again, docs says:

"...paths, with one important difference: the returned value is the full disk path on the server."

+

"There are also a few items in $config->paths that aren't in $config->urls. All entries in $config->paths always end with a trailing slash."

Yeah, just as @bernhard reminded me ? 

Edited by szabesz
typo
  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...