Jump to content

A different way of using templates / delegate approach


Soma

Recommended Posts

Just wanted to chime in here and clarify what I have found on the PHP Manual.
Following the short discussion on PHP shorthands.
 

Ya, I know!
But if the server doesn't support <?, it won't support <?= neither...
So, what I'm saying is: if you are going to use <?= in one statement, why not use <? in all the others also?
Or, if you are concerned that the server doesn't support them, don't use neither.

EDIT: Or am I wrong here? I thought they where both shorthands :)

 
I searched the manual and found the following:

From PSR-1 (Basic coding standards)
 

2.1. PHP Tags

PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations.

 
From PHP Manual
 

Note:

Starting with PHP 5.4, short echo tag <?= is always recognized and valid, regardless of the short_open_tag setting.

 
So even after PHP 5.4 becomes the norm, only the short-echo tag should be used.

Hope that clarifies the current situation (and probably explains Ryans preference).
 
Please note, the manual also has this to say on use of tags in production code.

Note:

Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

Sorry if this is an old subject, and clarified somewere else. Just wanted to bring it to the attention of others, following my own confusion on this matter.

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...

I am new here in pw and I think a template approach with output buffering is very  useful. I use yii for other projects and want to use a cms too for some projects. Templating with output buffer is done in yii and other frameworks. The big advantage is that you can write clear template files with html and php echos since php 5.4 always with the short echo tags. The performance of output buffering in php is also very good.

The simplest way to do this: _init.php (The prepended config file)

<?php
/**
 * The prepended file for all template files
 *
 * It buffers the output and after rendering all outputs and view Files the buffer will keep in the $content variable
 * and this content variable will decorated by the main layout file.
 */

ob_start();
ob_implicit_flush(false);

/**
 * Renders a view file as a PHP script.
 *
 * This method treats the view file as a PHP script and includes the file.
 * It extracts the given parameters and makes them available in the view file.
 *
 * @params string $viewFile the view file.
 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
 * @return void
 */
function render($viewFile, $params = [])
{
    extract($params, EXTR_OVERWRITE);
    require('views/' . $viewFile . '.php');
}

_out.php (the appended template file)

<?php
/* 
 * Now the content is complete and we can output the content 
 * with the decorated layout file.
 */

$content = ob_get_clean();

include('layouts/main.php');
Link to comment
Share on other sites

  • 4 weeks later...

I think I agree with Ryan on this thread. My approach would be to encourage newcomers (to PHP) to start by breaking thing up in the simpler default way. The need for more complex approaches comes later and is more interesting for those with a more solid foundation in programming.

  • Like 1
Link to comment
Share on other sites

I also agree with onjegolders and ryan. As I started with the examples out of this topic as well as skyscrapers, at one point I could not see what certain things were doing. It took me some time to get used to the API and template system. I then started from the DEV branch, and I now build day by day to make it more dynamic. Later on I will manage smoothness.

  • Like 1
Link to comment
Share on other sites

I just start explorin different templating approaches.

Ryan's main.php method appealed to me so I converted my template structure from the good old head.inc, foot.inc to the main.php structure.

Everything is working fine. Just one thing I cannot get my head around:

I have basic-page.php:

<? 
// Template basic-page
if($page->numChildren) {
	foreach ($page->children as $child) {
		$page->outMain .= $child->render(); // list the children
	}
}

include("./main.php");
?>

This renders all children, all of which have basic-sub-page.php as template:

<article class="article row">
	<h1 class="col-md-12"><?= $page->title?></h1>
	<figure class="figure col-md-4">
	<?= $images; ?>
	</figure>
	<div class="text col-md-8">
	<?= $page->body; ?>
	</div>
</article>
<? include("./main.php"); ?>

$images is irrelevant for my question, it just outputs img code.

You see that I include main.php also in the basic-sub-page.php.

This is fine when I call the page directly.

But when calling it through $child->render() in basic-page.php, then I don't want to include main.php.

How can I conditionally  include main.php depending on whether the page gets rendered directly or through $child->render() ?

Any ideas would be much appreciated.

Link to comment
Share on other sites

Hello Horst,

thank you for that link. Very helpful, indeed.

Actually, I found the solution to my problem through a link in that post to Ryan's post here.

I can pass my own variable in the options array of $page->render(). On the page that gets rendered I check for the variable. If it is there, I don't include main.php.

Love PW :-)

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Hi,

I found this topic very interesting, in particular I like the idea of the delegate approach (having only on main.php file and a subfolder with the template files).

However there's one thing I don't like about it, it's the fact that everytime I create a new template I have to give it an alternative name "main".

This is not big problem in my projects but if for example I want to share this approach with a colleague, which has never used PW, and I tell him "Hey everytime you create a new template remember to give it the alternate name" I'm sure he would say "But.. why do I have to do it everytime? Isn't there a way to do it automatically?"

So, my question is: do you think there is a way to tell PW that if there isn't a template file for a certain page, it have to call a default template file (for example the above mentioned main.php)?

Link to comment
Share on other sites

@enricob: 1) You do not have to add an alternate template file on a per page basis, you only have to add it once for a newly created template. Don't know how many templates one need for a average site, - I seldom have used more than 10 or 15.

2) I think it isn't a good idea. For example, it is different if a page has a template that has no template file compared to pages that have a template which is assigned a template file. Those pages without a template file cannot be shown on the frontend (because there is no template file that specifies how to generate the output). Such pages have many usecases like for example toolpages etc., or trees and childpages that you need to define categories or tags or whatever. You don't want them accessible through HTTP.

  • Like 2
Link to comment
Share on other sites

Thank you horst for your answer, I know it's not a big work to edit the alternate name for each template, it's just a little bit "unnatural", and I prefer to keep things simple when it is possible, it's just my personal preferences.

It would have been great to have a simple solution for having a default "main.php" file but I think as you said this is not possible... Anyway it's really not a big deal, the fact is that in the last days I am experimenting with PW and I think I am only trying hard to push it to its limits :D

Link to comment
Share on other sites

I have a question regarding Soma's original approach that kicked off this thread. If I want to include jquery or special css only on specific pages, is there a way to accomplish that using Soma's method? It doesn't seem like there is a point where I could pass that kind of information before the main.php file loads like I could using Ryan or Apeisa's methods. Am I missing something? Thanks.

Link to comment
Share on other sites

@thistimj,

Yes. Something like this in the head of your main.php should do it:

if ($page->template == "special") {
   echo  '<script src="' . $config->urls->templates . 'js/modernizr.foundation.js"></script>';
   echo '<link rel="stylesheet" href="' . $config->urls->templates . 'css/style.css" type="text/css" media="all">';
}
  • Like 2
Link to comment
Share on other sites

Another approach is to put this in your main.php

<html>
<head>
<?php echo $head; ?>
</head>

And then in each template file you can define $head differently, eg:

$head = '<script src="' . $config->urls->templates . 'js/modernizr.foundation.js"></script>
<link rel="stylesheet" href="' . $config->urls->templates. 'css/style.css" type="text/css" media="all">';

I do the same thing with a variable called $endOfBody

  • Like 1
Link to comment
Share on other sites

@kongondo cool. I hadn't considered that. That's a good option.

@adrian, does your example mean that I would have a template.php file and also set the alternate template to main.php in the PW admin? I hope that makes sense.

Having that stored in a variable is also a good option.

Link to comment
Share on other sites

@Adrian,

Thanks for this. Not sure your solution would work in Soma's approach? Remember, there is only one template file in his approach (main.php) which has most of the html. The .inc come after <body>. Hence $head  in main.php would remain undefined. Your solution would work with Ryan's approach though. I could be wrong :-)

Link to comment
Share on other sites

@Adrian,

Thanks for this. Not sure your solution would work in Soma's approach? Remember, there is only one template file in his approach (main.php) which has most of the html. The .inc come after <body>. Hence $head  in main.php would remain undefined. Your solution would work with Ryan's approach though. I could be wrong :-)

You're right of course :) I was forgetting the scenario we were talking about here. I guess I just don't like having lots of template specific conditions in the main.php file which is why I use that method along with Ryan's approach.

Too many options with PW :)

  • Like 2
Link to comment
Share on other sites

Yeah, that is a great tutorial. I can't make my brain work with Ryan's delayed output method, and he hasn't finished the "More Strategies" section yet. I have one site where I used Apeisa's method (found on page 2 of this thread), but it seems like overkill for what I typically need to accomplish. Soma's way seems to be a good balance between the good old "header/footer" method and Apeisa's way.

  • Like 1
Link to comment
Share on other sites

Thank you horst for your answer, I know it's not a big work to edit the alternate name for each template, it's just a little bit "unnatural", and I prefer to keep things simple when it is possible, it's just my personal preferences.

It would have been great to have a simple solution for having a default "main.php" file but I think as you said this is not possible... Anyway it's really not a big deal, the fact is that in the last days I am experimenting with PW and I think I am only trying hard to push it to its limits :D

You didn't even scratch on the surface yet... :) It's possible, just if it makes sense or there's isn't any drawbacks is a different question.

If you want you can overwrite and change almost everything. And if not, there's maybe just a hook needed to add to core. There's many different approaches as always in PW.

Fo example you could set all templates to have "main" as alternative template 

foreach($this->templates as $tpl){
     if($tpl->name != "admin") $tpl->altFilename = "main";
}

Depends really where you put this code. Most sense would make inside a autoload module like the HelloWorld.module. But then you have ALL templates always use main.php. You can't change it unless you add more logic to this script.

After all I've come away from the "delegate" approach I've posted here, and use the one Ryan proposed. Including the main.php at the end of a template file makes things easier when you want to add stuff per template. Some more flexibility after all. 

  • Like 2
Link to comment
Share on other sites

Thank you @Soma, in fact I have still a lot to learn about PW!

You're right your solution would work, I haven't thought about it...

Anyway, even if I like the idea of having a single main.php template file, I have to say that I too have come to the conclusion that ryan's approach feels better, it's just more "ProcessWire" style (and there's good reason for this! :D)

Link to comment
Share on other sites

  • 1 month later...

Hello to all

I'm still starting to understand all that was said about creating a template in processwire, but there's something i still couldn't: if you have multiple templates, all of them will have a matching php file in the site/templates directory, and include all the other files from the css, script and inc directories, under templates. correct?

But that means all the css, from the different templates will be mixed in the same css directory? If it is, that means a very different philosophy of drupal or wordpress (my previous choice CMS's)

thanks for your answers. Bye

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
  • Recently Browsing   0 members

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