Youbility Posted November 3, 2016 Share Posted November 3, 2016 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 More sharing options...
adrian Posted November 3, 2016 Share Posted November 3, 2016 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 1 Link to comment Share on other sites More sharing options...
BitPoet Posted November 3, 2016 Share Posted November 3, 2016 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 6 Link to comment Share on other sites More sharing options...
adrian Posted November 3, 2016 Share Posted November 3, 2016 Just to say that I agree that @BitPoet's approach makes much more sense than switching templates. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now