1

I'm trying to include a template section into another template under conditions based on the themes settings.

page.tpl.php

<header>
    <?php
    $theme_header_style = theme_get_setting('header_style');//the value is 1
    $header_path = "header_tpl_".$theme_header_style.".php";
    include_once $header_path;//includes header_tpl_1.php
    ?>
    <h1><?php print $site_name; ?></h1><!--this works-->
</header>

header_tpl_1.php

<h1>this will show up</h1>
<h1><?php print $site_name; ?></h1><!--this will not work just an empty h1--> 
<p>this will show up on page.tpl.php as well</p>

I'm pulling in header_style from my themes settings and for this example the value is 1. The file it will include is being added, as the html from the file is displaying but any php in the file is being ignored. anything on page.tpl.php is working as expected its only the php content on included files like header_tpl_1.php that is not working.

Update

Now that I know using a PHP include doesn't work I would like to know how I could include the contents of the header_tpl_1.php into the template prior to Drupal rendering the page, as this clearly would be the solution. The end objective is being able to load different PHP files into page.tpl.php based on a value set in the themes settings. The code in my header_tmp_1.php was removed from page.tpl.php as working code and put into header_tpl_1.php so I know there are no errors in it, as it works if it's added directly into page.tpl.php. The problem is including content into a template. I need the content to be included prior to Drupal rendering the variables.

  • what are you trying to do? – Adrian Cid Almaguer Dec 3 '15 at 17:23
  • i have files in my template directory "header_tpl_0.php", "header_top_1.php" and so on and depending on the selection made in the theme settings it will include that header section. each header section pulls in normal header data like site name, logo, breadcrumb, and so on. It works if its inside the page.tpl.php but not if its included. – Patrick W. McMahon Dec 3 '15 at 17:34
  • It includes the file and shows the html but the php is not executed. – Patrick W. McMahon Dec 3 '15 at 17:36
  • Templates are evald, maybe a scope issue? This sort of decision should ideally be made in preprocess functions, then you wouldn't have an issue – Clive Dec 3 '15 at 18:32
  • Is there a tutorial for including sections of template into a tmp file. – Patrick W. McMahon Dec 3 '15 at 18:35
0

Did you cleared cache ? I usually include header.inc, but I try your code and it worked on my site.

header code

  • the file that you included did it try to print out drupal variables? I have cleared the cash multiple times. Its including the file. The html is showing up. the php drupal methods are not being executed. – Patrick W. McMahon Dec 3 '15 at 19:10
  • for example if you put <?php print $site_name; ?> into your header_tpl_1.php file will it print out the site name? – Patrick W. McMahon Dec 3 '15 at 19:11
  • If you need print variable on page template file, you should use hook Hook preprocess page link. I print <?php print $logo; ?> on this header_tpl_1.php and it worked nomarly. - Code : Imgur image link - Demo : Imgur Image link – Trungthanh.hust Dec 3 '15 at 19:27
  • are you including header_tpl_1.php into page.tmp.php using a php include – Patrick W. McMahon Dec 3 '15 at 19:35
  • Yes, see First Image on my answer. i use your code include_once $header_path; to include header_tpl_1.php to page.tpl.php. If HTML is showing up, it mean you include header_tpl_1.php success. But your variable maybe don't have value. You should test by print this vars on page.tpl.php – Trungthanh.hust Dec 3 '15 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.