Function - When php variables not available from an included file39s scope

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I am developing a custom theme for Drupal 7 and I m a bit stuck. I ve not posted this to the Drupal stackexchange, because I don t believe this problem relates to Drupal -- I think this is a php language issue.

Basically, Drupal looks for named files such as page.tpl.php, page--front.tpl.php, etc. Actually, those are the only two I have right now.

In my html.tpl.php file, I found it convenient to define my page s overall structure. When a different page is accessed (such as page-front, the front page), the following code is run:

<?php
$subtemplate = "front/content";
include("page.tpl.php");
?>

Nice and simple.

Now let s look at the overall HTML structure in page.tpl.php:

<?php
if(!isset($subtemplate)) {
   $subtemplate = "test";
}?>
<html> (etc)
...
<?php print "from main template file:" . $content_column_class; ?>
<?php $x="hello"; ?>
<?php include "pages/$subtemplate.tpl.php"; ?>

Also quite simple. $content_column_class is provided by the theme, $x is just a test string.

Now in our subtemplate file, pages/test.tpl.php:

<?php echo $x; ?>
<?php print "from sub template file:" . $content_column_class; ?>

You would expect the output to be this:

from main template file: class="col-sm-12" 
from sub template file: class="col-sm-12" 
hello

In fact, the output is this:

from main template file: class="col-sm-12" 
Notice: Undefined variable: x in include() (line 1 of /buildkit/build/CiviCRM/sites/all/themes/common/templates/pages/test.tpl.php).   
Notice: Undefined variable: content_column_class in include() (line  1 of /buildkit/build/CiviCRM/sites/all/themes/common/templates/pages/test.tpl.php).

And this I just don t understand. In fact, I have never seen anything like in all my years as a php developer. Every time I have used the include() statement or its family require, require_once, etc., script execution has continued where it left off inside the included file and variables are /always/ available to the included script. In fact, I didn t think it was /possible/ to change include() s behaviour like this.

Now, I can solve this problem:

<?php global $x; ?>
<?php global $content_column_class; ?>
<?php echo $x; ?>
<?php print "from subtemplate:" . $content_column_class; ?>

Now the output is as expected:

from main template file: class="col-sm-12" 
from sub template file: class="col-sm-12" 
hello

Using the global keyword is inelegant and generally not recommended. Is there an easier, simpler way to do this? I am worried that there will inevitably be variables I have forgotten to globalize if I take this route.

https://wordpress.org/support/topic/passing-php-variable-between-template-files https://wordpress.org/support/topic/passing-php-variable-between-template-files

WordPress won t let you include variables in the same way that you might do on a non-WP site. One way to approach this might to use a custom field on your pages and use the value in that field to determine which stylesheet to load.

However, they dont actually say /how/ it is possible to do this.

The debug backtrace is not especially helpful:

 #0  include() called at [/buildkit/build/CiviCRM/sites/all/themes/common/templates/page.tpl.php:100]  
 #1  include(/buildkit/build/CiviCRM/sites/all/themes/common/templates /page.tpl.php) called at [/buildkit/build/CiviCRM/includes/theme.inc:1525]
 #2  theme_render_template(sites/all/themes/common/templates/page.tpl.php, Array ([page] => Array ([#show_messages] => 1,[#theme] => page,[#theme_wrappers] => Array ([0] => html),[#type] => page,[#icon] => ,[#icon_position] => before,[#process] => Array ([0] => bootstrap_form_process),[#pre_render] => Array ([0] => bootstrap_pre_render),[content] => Array ([workbench_block] => Array ([#markup] => ...

Is it possible? Can anyone help me understand what might be going on here and how it might be overcome?

Theme is a child theme of Drupal Bootstrap, not that I expect that would matter.

Answers

When you are working with something already coded, it could be hard to integrate some stuff. Do you have a look inside the standard Drupal theming module and have you seen how they proceed?

I ve stopped to use Drupal when it switches to version 6, so I don t know how it works now.

From Drupal 7, official documentation about theming :

I think you should register your variables as they suggest especially in the third link.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/36414304/when-php-variables-not-available-from-an-included-files-scope

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils