Jump to content

Recommended Posts

Posted

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

Posted

Yep, you can do that, just not in init.php. You need to do it in ready.php because the page is not available yet in init.php

  • Like 1
Posted

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

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