Jump to content

include external php in template file?


Thomas108
 Share

Recommended Posts

Hello everyone,

I am new to processwire and this is my first post. I am coming from the modx world, and I am unfortunately really bad in php.
Nonetheless after silently reading here for a few weeks I think that processwire might be just the right cms for my needs.

At the moment I am doing a website for a musician and I will probably have quite a few beginner questions.
Thanks for your help and patience in advance :)

Thomas

*********************************************************

Here is my first question:

My pages have basically the same layout, so I would like to have only one template file. But there will be a few extras on certain pages, like one page with a calendar and one with an audio player.

I thought it would be wise to put the code for the audio player or the calendar in an external file or page to keep my template file simple to read. To do this I tried to "echo" or "include" an external php file, but the php didn' t get processed.

<?php
  if ($page->id == 1032)//home
  {echo "<h2>Welcome...</h2>";}
    elseif ($page->id == 1017)//dates
      {echo "how to include external code here?";}
?>

Is there a processwire way to do this and is this in general a good approach?

Thomas

PS: I read the different modx threads already.

Link to comment
Share on other sites

Welcome!

You just include(/path/to/file.php) http://php.net/manual/de/function.include.php

You could also use a php switch() instead of if else

switch($page->id){
    case 1001: 
        echo "...";
    break;
    case 1002:
        include(...);
    break;
    case 1004:
        echo someFunction($page); // maybe a function
    default:
        echo "..";
}

About the approach I'm not sure and don't see the whole picture, but experiment with different ones. There's no general "this is the best". Whatever makes sense and works for you.

I think outputting content by id might be ok in one case, but maybe not so convenient regarding reading/maintenance and code compatibility. I usually try to avoid it.

There's so many ways to build templates and output your code in templates, and it goes far beyond this question. And there's as many different opinions on what is good and what not. :) But some principals may apply always. 

Usually I find it more convenient to use different templates for different pages as much as possible.

  • Like 1
Link to comment
Share on other sites

I've used different templates on occasion where there were only one or two minor differences.

It's okay to do this as long as you have your header and footer in separate files (like they are by default) so if you change those you don't have to change them in your templates (from memory in MODx the default template had everything in it, header and all).

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

×
×
  • Create New...