Jump to content

[SOLVED] Including a template in a template


matsn0w
 Share

Recommended Posts

Hey everyone, hope you're all doing well!

I'm building my first ProcessWire website at the moment, which is of course a very good way to understand all of the possibilities ProcessWire has to offer. But that also means that I'm facing some troubles now and then, and so is the case right now.

I made a template for a couple of pages, like the homepage, the contact page, etc. I am using Bootstrap 4.0 for the layout. 

So here's the deal: I want to show an alert box on top of every page, warning the user that the website is still under construction. Of course, I can just include the code and add the fields I made for the alert into the template, but that means I have to do that for every page, which I obviously don't want. So I made a template with the code and fields for the alert. When I did this, I realised that I can create multiple alerts this way, so I made two pages, containing different alerts. (And converted the code into a foreach-loop in the template).

But now my question; how can I include these pages in a different template? Can I make a field or something to insert a page or is this simply not possible? Or do I have to approach this in a different way?

I hope I explained my problem clear!

Thank you in advance,

matsn0w

Link to comment
Share on other sites

Hi and welcome to the forum!

Your approach is good and flexible, so that's a good start! :)

To output the alerts on all pages in the front end, you have some options. I'll show 2 of them:

1 - Do the logic in the layout file that's shared by all pages

In a default PW project, all pages can share a main layout file, usually _main.php. And on this file, you can output your Alerts pages' content, like so:

//_main.php file

<body>
  <?php
  	$alerts = $pages->find('template=alerts'); // considering your template is called "alerts"
  	foreach ($alerts as $a):
  ?>
  <div class="alert">
    <h1><?= $a->title ?></h1>
    <h1><?= $a->body ?></h1>
  </div>
  <?php endforeach; ?>
  
  ... rest of file
    

2 - Using the render function to output the content of you ALERT template in all, so the logic (the foreach etc.) is done in that file, not in _main.php

 

Edited by Sergio
  • Like 5
Link to comment
Share on other sites

hi and welcome,

did you see the docs here: https://processwire.com/docs/tutorials/how-to-structure-your-template-files/ ?

There's also a lot to read, if you like ;) Old but still good i think.

 

Also, as sergio already mentioned, have a look at the "default" site profile (choose it during installation and see what is different to your setup). You can adopt your setup to use it like the default profile easily (the keywords are append/prependTemplatefile): https://processwire.com/docs/tutorials/how-to-structure-your-template-files/page4

  • Like 2
Link to comment
Share on other sites

Thank you both for your reactions! I like the way to render() a page, so that's what I do now.

Thanks a lot!

PS: this is my code now (I know there will be better ways, but I like it this way):

<!-- Alert(s) -->
<?php 
	$alerts = $pages->find('template=bootstrap_alert');
	foreach ($alerts as $alert) {
		echo $alert->render();
	}
?>

 

  • Like 1
Link to comment
Share on other sites

  • matsn0w changed the title to [SOLVED] Including a template in a template

that looks totally fine to me :) just make sure that the alert pages itself are not accessible from the frontend. maybe I'm wrong, but I guess you have some "bootstrap_alert" pages with a related bootstrap_alert.php file in the templates folder? then those pages will be accessible as /whatsoever/youralertpage1 from the frontend and that might not be what you want?

  • Like 1
Link to comment
Share on other sites

52 minutes ago, matsn0w said:

You are totally right, I can disable that by setting those pages to Hidden, right?

No, hidden is just hidden for find operations, but the page can still be accessed directly. You need to set it to "unpublished" (then a request leads to a 404 error not found). Then you need to add "include=all" to your $pages->find() selector otherwise it will not find your alerts.

  • Thanks 1
Link to comment
Share on other sites

Wait, never mind...

I've now unpublished the Alert page, and that works fine for as long as I am logged in, but when I'm logged out (so in the 'guest' view) PW throws me the following error:

Error: Exception: Page '/alerts/under-construction/' is not currently viewable.

Any ideas?

Link to comment
Share on other sites

6 hours ago, matsn0w said:

Wait, never mind...

I've now unpublished the Alert page, and that works fine for as long as I am logged in, but when I'm logged out (so in the 'guest' view) PW throws me the following error:


Error: Exception: Page '/alerts/under-construction/' is not currently viewable.

Any ideas?

Are you looking at this from a Browser Cached Page or are you accessing the site via Browser Incognito Mode?  If you logged out of ProcessWire and then went back to that page from the same Browser tab, you may get this result because the Browser Cache needs to be properly cleared.

Link to comment
Share on other sites

Negative, I'm always developing with the Chrome Dev Console open (so cache is disabled) and it also happens in Incognito mode etc. 

Even on other devices!

PS: it shows me the Internal Server Error when I disable Debug Mode

Edited by matsn0w
small addition
Link to comment
Share on other sites

Fixed it.

To me, it looks like the render() function is not working when you unpublish a page. So I now manually render the alert, like this:

<?php $alerts = $pages->find('template=bootstrap_alert, include=all'); ?>

<?php foreach ($alerts as $alert): ?>
    <div class="alert alert-warning alert-dismissible fade show" role="alert">
        <h4 class="alert-heading"><?= $alert->alert_heading ?></h4>
        <?= $alert->alert_body ?>
        <?php if ($alert->alert_dismissable == 1): ?>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">×</span>
            </button>
        <?php endif ?>
    </div>
<?php endforeach ?>

I put this code in a seperate file, so I only have to include it on the pages where I want to show it. This works fine.

Thank you all for the help!

  • Like 1
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

×
×
  • Create New...