marcus

Another idea for a ProcessWire 'MVC' approach

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

Share this post


Link to post
Share on other sites

This is pretty similar to how Spex works (shameless plug). It doesn't use a separate file for controllers however, the template file is used as a dual purpose: controller and initial generation.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for the hint! Looks very sophisticated, will look into it!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By EyeDentify
      I as have been a bit confused for some time about how the "Markup Regions" functionality in Processwire worked.
      But i have know read a bit more and think that i am getting to grips with it. And Markup Regions is going to be huge.

      To aid me in understanding Markup Regions better i started to read the Source code for the new "Regular" theme in conjunction with the Blog about the markup regions.
      It helped me a great deal to understand the basics and more fine details of it.

      A tip is to open both links and use the Source code of the "Regular" theme while reading the blog post.
       
      The Source code:
      https://github.com/processwire/processwire/blob/dev/site-regular/templates/_main.php
      The Blog post:
      https://processwire.com/blog/posts/processwire-3.0.62-and-more-on-markup-regions/
       
      I hope this could help others starting out with markup regions.
      Just take it slow and read it a couple a times and soon you will see the greatness of markup regions.

      /EyeDentify 
    • By louisstephens
      So I am making a front-end dashboard for an internal project, and was curious if it is possible to match 2 pages together based on their title and retrieve the data.
      To make it more clear:
      I have two pages "Section A"  and "Section B" that each has child pages with names (the specific person) with subpages for their services offered. 
      Section A - John Doe (name=john-doe) - Service 1 - Service 2 - Service 3 - Jane Doe (name=jane-doe) - Service 1 - Service 2 - Service 3 Section B - John Doe (name=john-doe) - Service 4 - Service 5 - Service 6 - Jane Doe (name=jane-doe) - Service 4 - Service 5 - Service 6 What I would like to achieve on the front-end (if it is even possibile), is an output like:
      John Doe - Service 1 - Service 2 - Service 3 - Service 4 - Service 5 - Service 6 Jane Doe - Service 1 - Service 2 - Service 3 - Service 4 - Service 5 - Service 6 Thinking aloud: use a find to get all pages using the template "service user" and then foreaching through their children (and possibly getting a few field values as well) to output in the front end. The service templates used in the sections vary slightly in which fields they use.
      <?php $findUsers = $pages->find("template=service-a (and or) service-b,(some how match based on names)"); //if no match exists, output what is available ?> <ul> <?php foreach ($findUsers as $groupedUser): ?> <li><?php echo $groupedUser->title; ?></li> <li><?php echo $groupedUser->service_a_location; ?></li> <li><?php echo $groupedUser->service_a_body; ?></li> <li><?php echo $groupedUser->service_b_location; ?></li> <li><?php echo $groupedUser->service_b_phone; ?></li> <?php endforeach; ?> </ul> Sorry, it is just me thinking aloud, but I was not even sure if it is possible to group them together like that. I was going to have a page for each "section" only displaying that data, but I thought it would be nice to display them all together as an overview for the user.
    • By louisstephens
      Just a quick question, I have a function in my ready.php file that creates a new js file inside the "scripts" folder (under templates). Everything is working as expected, however, is it possible for the url to look like www.domain.com/scripts/generated.js ?
       
      Currently, the only way I can access it is via www.domain.com/site/templates/scripts/generated.js. Or is there a better place I should be putting these scripts ? I I am trying to use the scripts elsewhere (not on the processwire install).
    • By Guy Incognito
      I'm trying to make an AJAX call from within a template to a php script within my templates folder, but I'm getting a 404 from all URLs. Is there a proper way to directly address scripts within PW templates? I've read it will work in the site root, but I'd rather keep all the code together if possible.
    • By Jim Bailie
      Hello,
      After doing a deep dive into Processwire over the weekend (better late than never), I'm starting to layout an app to receive a migration of an existing app. I'm sure I'll have several questions, but here's my first:
      Question: I'm using the _main.php/regions template approach. However, I need to do some ajax calls for some html. How would I tell a page just to include only the needed template/fields?
      I can probably figure this out, but in the interest of time...Thanks!