Jump to content
matsn0w

[SOLVED] Including a template in a template

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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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 louisstephens
      So I ran into a very strange issue today. I have a template with a pagetable and I went to add an item to it, when I went to select an image (for an image field) the page instantly threw up an error
      "ProcessPageSearchLive: No search specified"
      The page's content also switched to the image attached. This all worked perfectly last week (local mamp box). Has anyone experienced this before, and how did you solve it?
       

    • By Brian Peat
      Hello! I've got a single client on Processwire and it's completely foreign to me. I've been able to find most things, but they've asked for a custom designed page with a full width header image/section, and then a body block and a right side bar below the header. They're using a landing page template, so I duplicated that, figured out how to add it in the admin, and assigned it to the page. Nothing. It doesn't change a thing.
      I feel like I'm missing something obvious. I've made sure all the fields are the same, I tried to set up the parent/child stuff though this page doesn't have a parent that I can tell (though it's under Landing Pages). If I can get the template to actually kick in, I think I can use a bit of code to check for the hero section and load it at the top. I also made a copy of the widget template and set that to load instead of the original. But again, since my main page template isn't kicking in, neither is the new widget template.
      I'd just love some tips on what to check or change to get a landing page to actually render what's in the assigned page template.
      Hopefully I've used the correct terminology here to make sense.
      Thanks!
    • By rookie
      hi there,
      Is there a module or other solution to select a template with a preview image? Sometimes it is not that easy to describe a template.
      e.g. Template with 3 columns (image, title, content, button) or template for a slider with max. 4 pictures.
      Even if it is nice to have a description field for a template, a picture says more than thousand words could describe.
      And no, I don't think I could develop something like that. I don't even know where to start.🙄
    • By muzzer
      I have just upgraded a site from 2.7 to latest v3.
      I have run into a weird issue where a page template setting for slashUrls changes from No to Yes immediately after an ajax call, causing the second ajax call on the page to use the wrong setting.
      There is no reference to the $template->slashUrls() in any of my code.
      This happens every time I load the page, it's 100% consistent.
      I have this running on a second machine under v2.7 and no such weirdness.
      I'm lost as to why this is happening, what would cause a PW setting change for slashUrls? Anyone have any ideas?
       
       
       
    • By Pip
      Hi everyone, 
      Is there a way for us to replicate the "http://mydomain.com/processwire/page/edit/?id=xxx" and change the template to match my site template? 
      I'm terribly happy with the admin / backend page edit. It covers literaly everything I want to empower my non super admin user in updating pages such as validation, repeater management, file upload. 
      I wish not to allow the users to see the backend for both security and aesthetic reasons. 
      Thanks and hope to hear from you soon. 
       
×
×
  • Create New...