Include Template from File

include external templates

for a long time I have been using a snippet to include external files as templates and chunks. I found this snippet in an older forum. But since I’ve been working with Modx 3 and PHP 8, the snippet no longer works. It works only one my frontpage but the subpages are all blank pages now.

This is the snippet I have used so far:

# Snippet to include template files from file system
# usage: [[includeTemplate? &tpl=`template.tpl`]]

if (!isset($tpl)||$tpl=="") return "Missing Template file!";

$tpl = $base_path .'assets/templates/page/'.$tpl;
return ob_get_clean();

I think this code has just to be updated for php 8. Is there anyone who could help me with this?

Thanks in advance


what about just using static elements (chunks or templates)?

Hi Brono,

I would like to keep my nested templates out of the database and in the past, this is how my colleagues and I have done it because of Git.

One of my templates look like this:

[[includeTemplate? &tpl=`head.tpl`]]
	<div id="container" class="producer">
		[[includeTemplate? &tpl=`header.tpl`]]
		<div id="inner_container">
			<main id="main">
				<article class="content">
			[[includeTemplate? &tpl=`sidebar.tpl`]]
[[includeTemplate? &tpl=`footer.tpl`]]

Use $modx->getOption('base_path') in your snippet instead of just $base_path.

Thank you Harry,
I have changed this as you wrote, but I still have the same issue with the subpages. They all are still blank.
In PHP 7.4 I don’t have this issue. Could it be that because of PHP 8 I have to adapt the code somehow? Unfortunately, I am not familiar with PHP. So far I have done very little.

Or have I done something wrong with this?
$tpl = $modx->getOption('base_path') .'assets/templates/page/'.$tpl;

I have tried to reproduce this with MODX 3.0.1 and PHP 8, but the snippet works correctly on my installation.
To my knowledge nothing in this snippet code shouldn’t work with PHP 8.

What exactly do you mean by subpages? Just resources further down in the resource tree or is there something special about them?

Could you rewrite the whole code so that it would work with PHP 8?

What I mean with Subpages: “further down in the resource tree”. That code works only on the startpage. All others are blank.
You can see it here. - This is a page of my old neighbour.His provider wants to switch off PHP 7.4 in November so that I have to fix that code.
I have been helping him for years for free but this is over my skils

You don’t have to do it for free. I would even donate a coffee to you or someone else via Paypal if you agree.

Ps: I tried the static solution from Modx before in another project, and then I saw that I would have to rebuild everything in this projekt (link) because the nested templates would no longer work.
As I wrote, I don’t get anything from that and I’m tired to start everything again.

PPs: I use MODX 3.0.2 and PHP 8.1.8

In my opinion there is nothing to rewrite here.
My guess is that there is something else (another snippet, maybe a plugin) that doesn’t work with PHP 8.

When I open one of your subpages, I can see (in the browser’s developer tools) that it returns a 500 Internal Server Error.

Please try to locate the PHP error log on your server to check the actual error message. (This is not the error log inside MODX. The exact location depends on the server configuration.)

BTW: MODX 3.0.2 isn’t released yet.

Thanx a lot Harry for your time and effort!
I’ll have a look at the plugins. Maybe I can locate the error there. If a snippet or plugin is causing the problem I will get back to you and this topic can be closed than.

You can disable the plugins by right-clicking on them in the tree and selecting “Deactivate Plugin.” Be sure to clear the cache before testing.

You may also have to clear your browser cache.

Hi Bob,
I have just done what you and Harry said and I found out that none of the plugins I use cause this error. It was a Snippet called “siblingsnav” - a prev next navigation from
. I will check on githup if this problem is already known and report it if necessary.
Thanx for all of your help!

You probably have to change one line like described in this issue:

Oh wow - his change in the code solved my problem in no time. :slight_smile:
I think Bruno will update this in the package in the near future.
Thanx a lot again, Harry!

1 Like