Jump to content

Change page template on the fly in init.php


Youbility
 Share

Recommended Posts

Hello everyone,

I was wondering if it could be possible to change the page template on the fly in init.php (the file that gets prepended to my actual template), e.g. using

$page->template = 'my-other-template'; // does not work for me

$page->set('template', 'my-other-template'); // does not work either

 

My scenario is the following:

I have a calculator using the calculator template. If all required GET parameters are set it should show the result of the calculator. If not it should show the page content, which is an explanation how the calculator works and some input fields. That information should not be shown when the results are show. Now I could put everything in one template and use a couple of conditions to achieve that, but "redirecting" the template seems cleaner to me.

Is that possible somehow?

Best regards
Alex

Link to comment
Share on other sites

Another possible approach would be to add the condition in the head of the calculator template file, include the result template when met and return, otherwise proceed as normal. calculator.php:

<?php if($input->get->firstvalue && $input->get->secondvalue ) { include('_result.php'); return; } ?>
/* The regular calculator template follows here */

This way, you don't have any "strange magic" that swaps templates and that you'll have to search for when you re-visit the code in three years time ;)

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

  • Recently Browsing   0 members

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