Jump to content

Theme Support Into Template Files with Autoloading


joer80
 Share

Recommended Posts

I have been working on a way to layout the files in my template folder that allows people to have more than one theme per website, and better organize the template files and pages, and this is what I came up with.  What do you guys think?  Has anyone done anything similar?

File Layout:

/templates/

  _init.php. (includes and variables you want all pages to access.  I put setting('theme', 'kick-off-news'); to define the active theme.)
  default.php (Template files can be told to use this for default behavior, or you can clone its contents for more control.  I haven't needed to yet.)

What is in the default.php file:

<? namespace ProcessWire;

//sitewide variables and/or functions. ie. Define setting('theme') as active theme.
include('_init.php');

//The default temmplate can use the default HTML for the active theme
$templateFilePath = $config->paths->templates . 'themes/' . setting('theme') . '/default.php';

//Render template file for the active theme
echo $files->render($templateFilePath);
?>


/temlates/themes/

(Holds all theme folders.)



/temlates/themes/kick-off-news/

Here is what I am putting in the theme folder:

/content/  (This has content subfolders for organization. ie. I did /by-page/ and /by-template/.  (folders can be autoloaded from page path and template name.)
/includes/ (Used for code that will be included across templates, like navigation.)
/css/
/js/
/img/
default.php (You can clone for more control if needed, but I have not need to.)

Below is an example of what could be in the /temlates/themes/kick-off-news/default.php file.

<? namespace ProcessWire; ?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<? include('./includes/head.php'); ?>
	</head>
	<body>
		<? 
		include('./includes/topAd.php');
		include('./includes/header.php');
		include('./includes/navigation.php');
		
  		//Use selected homepage if no page path.
		if($page->path == '/'){
			$pagePath = setting('home'); //active homepage path. ie /pages/home/
		} else {
			$pagePath = $page->path;
		}

		//Load content file for this page if it exists
		if(file_exists('./content/by-page' . $pagePath . $page->name . '.php')){
			include('./content/by-page' . $pagePath . $page->name . '.php');
		} else {
			//Load content file for this template if it exists
        	if(file_exists('./content/by-template/' . $page->template . '/' . $page->template . '.php')){
            	include('./content/by-template/' . $page->template . '/' . $page->template . '.php');
        	}
		}
	
		include('./includes/footer.php');
		include('./includes/mobilenav.php'); 
		
		include('./includes/bottom.php'); 
		?>
			
	</body>
</html>

The reason why I like to be able to select an active homepage is to be able to make another homepage, and flip back and forth on which one is active as needed.  No need to copy any paste data, or upload files to another page after it is approved.

I mainly only use one or the other, as far as page or template html goes, but I guess you could use both at the same time with a few changes.  The advantage to using using one at a time is you dont need to check for a template content file if there is a page content file. 

If you want to be able to load both, you probably need to have them set output variables, and after they are both loaded, push the results out.  Maybe an array with a priority key and html value and loop through the array after they both load. Was overkill for me though.

How I did site Tree to organize and separate the pages from the news articles.

Screen Shot 2020-07-25 at 4.42.42 PM.png

I also think there is an advantage to this over sticking all template files in the same folder, as you can put related supporting files in the same group.  A news category page sidebar can be in the same folder as the news category template.  Or related news layouts are next to their parent file.

Screen Shot 2020-07-25 at 6.55.52 PM.png

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...