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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Mithlesh
      Changing it to null
    • By Mithlesh
      Hi, I have one URL - writerrelocations.com/contact-now/

      I have one issue where my header Image is appearing again after the contact form, for your reference:



      I have checked my processwire template section where have not added any Image field besides Header Image, attaching the same for the reference:




      PLEASE help me to get rid of the image below contact form

      Waiting for the swift response!

      Thanks
       
    • By iipa
      Hi fellow developers!
      I want to implement following action:
      Admin-priviledged user uploads a spreadsheet file (with many sheets) When page is saved, back end generates repeater items from the sheets We can render spreadsheet data from the repeater field. Profit! Currently I'm trying to find a way to read the uploaded spreadsheet file. ProcessWire doesn't seem to have modules suitable for my needs, but external PHP Library PhpSpreadsheet sounds like it could do the job. However, I can't even try it, because I simply don't understand how I can refer to an external library!
      Folder structure:
      templates _func.php > where I want to refer to the library libraries PhpSpreadsheet bunch of *.php > what I want to refer to I have tried different things varying from their documentation to forum posts and PW API with no luck. I would prefer not to use Composer, since it seems kind of overkill for one library.
      Thanks in advance and have a great day!
    • By EyeDentify
      I have been experimenting with the new $page->meta() method and find it useful.

      Once i figured out that the data i "save" with it is tied to the page where i called the method from.

      So this is not obvious at least not for me in the documentation:
      https://processwire.com/api/ref/page/meta/
       
      So i just wanted to share that revelation with the community so you don´t get as confused as i was.

      Happy Coding Everyone.
    • By stanoliver
      In the new page-meta-method (https://processwire.com/blog/posts/pw-3.0.133/#comments) there is an example how to ouput "world". 
      Could may someone give me a snippet of code so that the output is one of the colors (red, green, blue).
×
×
  • Create New...