Jump to content

Another idea for a ProcessWire 'MVC' approach


marcus
 Share

Recommended Posts

Hi altogether,
 
since I am currently working on a project that has (hopefully) the potential to reach a certain level of complexity at some point, I wanted to start the template structure in a scalable and "proper" way. In the following I'll try to give a summary about the approach I'm testing:
 
1. Directory structure inside site/templates/
/controllers/
/views/
 
2. Create init file named...
site/templates/init.php
...containing (for example):
 
<?php

function getPartial($name, $allpages) {

// Site wide
$settings = $allpages->get('/meta/settings');
$root = $allpages->get('/');

// User related
$loggedInUser = wire('user')->isLoggedin();

// Page related
$title = wire('page')->title;
$children = wire('page')->children;
$template = wire('page')->template;

include($config->urls->templates . 'controllers/' . $name . '.php');
include($config->urls->templates . 'views/' . $name . '.php');
}
3. Uncommenting prepandTemplatefile in /site/config
$config->prependTemplateFile = 'init.php';
 
4. Creation of partial (controller and view) in their respective folders
/site/templates/controllers/test-partial.php
/site/templates/views/test-partial.php
 
Example controller:
<?php
$homepagetitle = $root->title;

Example view:
<h1><?= $homepagetitle ?></h1>
<p><?= $title ?></p>
 
5. Creation of layout file. I tried to put these in a folder called /layouts, but that does results in PW not finding new templates anymore in the PW Admin when creating a new templates. Any ideas? Changing index.php's $config->urls->templates ?
 
Example layout:
<html>
<head>
<title>Foo</title>
</head>
<body>
<?php
getPartial('test-partial', $pages);
?>
</body>
</html>
 
So, for some reason I have to explicitly inject $pages or wire('pages') into getPartial() - but I have no idea why, since $page or wire('page') is working without problems in the functions scope.
 
But: The output is just as intended. The title of the root page in a h1 headline, and the title of the page using this "layout" in a paragraph.
 
Any feedback or pointing to pitfalls of this approach would be highly appreciated :)
 
Best,
marcus
 
edit: Funny, essentially this is a very stripped down way of Template Data Providers, which needed Twig the last time I tried to use it.
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

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