繁体   English   中英

当php变量/ not /在包含文件的作用域中可用时?

[英]When php variables /not/ available from an included file's scope?

我正在为Drupal 7开发一个自定义主题,但有些卡住了。 我没有将其发布到Drupal stackexchange,因为我不认为此问题与Drupal有关-我认为这是php语言问题。

基本上,Drupal会查找诸如page.tpl.php,page--front.tpl.php等命名文件。实际上,这是我目前仅有的两个文件。

在我的html.tpl.php文件中,我发现定义页面的整体结构很方便。 当访问其他页面(例如首页,首页)时,将运行以下代码:

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

漂亮又简单。

现在让我们看一下page.tpl.php中的整体HTML结构:

<?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"; ?>

也很简单。 $ content_column_class由主题提供,$ x只是一个测试字符串。

现在在我们的子模板文件pages / test.tpl.php中:

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

您期望输出如下:

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

实际上,输出是这样的:

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

我只是不明白这一点。 实际上,作为一个php开发人员,这些年来我从未见过类似的东西。 每次我使用include()语句或其族的require,require_once等命令时,脚本执行都会在所包含的文件中停下来的地方继续执行,并且变量/ always /可用于所包含的脚本。 实际上,我不认为改变这样的include()行为是/ possible /。

现在,我可以解决这个问题:

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

现在输出是预期的:

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

不宜使用global关键字,通常不建议使用。 有没有更简单的方法? 我担心如果走这条路,不可避免地会有一些忘记全球化的变量。

我唯一能找到的可能是相关事件是Wordpress的以下论坛帖子: https ://wordpress.org/support/topic/passing-php-variable-between-template-files:

WordPress不会像在非WP网站上一样允许您包含变量。 一种解决方法是使用页面上的自定义字段,然后使用该字段中的值来确定要加载的样式表。

但是,他们实际上并没有说/ how /可以这样做。

调试回溯并不是特别有用:

 #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] => ...

可能吗? 谁能帮助我了解这里可能发生什么以及如何克服?

主题是Drupal Bootstrap的子主题,并不是我期望的那样重要。

当您使用已经编码的东西时,可能很难集成一些东西。 您是否在标准的Drupal主题模块内部进行了浏览,并了解了它们的进行方式?

当它切换到版本6时,我已经停止使用Drupal,所以我不知道它现在如何工作。

从Drupal 7开始,有关主题的官方文档:

我认为您应该按照变量建议进行注册,尤其是在第三个链接中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM