在正文内容区域内使用 PHP 变量
我在页面的正文内容区域内使用 PHP 代码时遇到问题。我在这个网站、Drupal 网站和其他网站上不知疲倦地搜索过,所以如果答案就在这个网站上的某个地方,我深表歉意,但我找不到它,我需要帮助。
我们在整个网站中重复使用了很多信息,我想将它们作为变量存储在 PHP 文件中。我们现在在我们的网站上这样做,但我正在重写我们的整个网站以使用 Drupal。例如,我们销售软件,我希望为网站上的各种“添加到购物车”按钮的每个产品 URL 提供一个变量。我不想将产品 URL 硬编码到每个链接中,而是设置一个可以在任何 drupal 页面上调用的 PHP 变量。
我无法让任何事情发挥作用;我读过一些建议,但没有一个有效。我尝试将变量设置为它们自己的块,然后在创建新页面时从页面内调用它们。我可以回显页面上的变量,但只能在它们所在的块内回显,我无法调用它们并让它们从其他块或内容区域回显。我尝试过使用 global 关键字(根据一项建议),但这对我不起作用。
我希望这是有道理的。其他信息?我正在使用 Drupal 6.x,我在创建页面时启用了 PHP 代码,我确实启用了 PHP 过滤器模块,我可以获取 PHP 代码进行渲染,所以我知道它正在工作,只是在我需要它的地方不起作用是。
我应该说(如果仅仅阅读这篇文章还不够明显!)我是 Drupal 新手,所以如果有人可以帮助并尝试尽可能简单地向我解释他们的建议,我真的很感激!
提前致谢。
编辑 2011 年 3 月 15 日
为了进一步解释,我将发布一些示例代码。我还没有这样做,因为还没有太多东西可以展示,而且我认为这可能会让问题更加混乱。
因此,我制作了一个 Drupal“页面”,用于下载我们的软件试用版。我要设置的 PHP 变量是用于我们的下载链接;我想将它们设置在一个地方,这样如果将来需要更改下载链接,我只需在一处进行即可。您会看到,我们在各个网站页面上都有下载链接。我们的“立即购买”链接也是如此。这是页面代码:
<p>Try [product] free for 30 days.</p>
<!--<p>[token_custom_ewintry]</p>-->
<p><?php global $ewintry; ?><a href="<?php print $ewintry; ?>">Download for Windows PC</a></p>
<p><?php global $emactry; ?><a href="<?php print $emactry; ?>">Download for Mac OS X</a></p>
<p><?php global $ebbtry; ?><a href="<?php print $ebbtry; ?>">Download for BlackBerry</a></p>
<?php
$ebbtryprint = variable_get("ebbtry", "default");
print $ebbtryprint;
?>
<p><a href="<?php print $ewmtry; ?>">Download for Windows Mobile</a></p>
<p><?php global $ipewlstorelink; ?>iPhone, iPad, iPod touch owners: <a href="<?php print $ipewlstorelink; ?>">Download [product] on the iTunes App Store</a>. You'll be able to create 10 cards for free to try [product] before you buy!</p>
对于此示例,我保留了我尝试过的所有内容。您会看到我对全局变量的调用,但它从未起作用。我在创建并放置在“内容顶部”区域中的自定义块中定义了全局变量。我了解到,显然该区域的任何内容实际上都无法访问我的页面正文内容,因为这些调用从未起作用。
我昨天使用令牌模块制作了一个自定义令牌;它从来没有起作用,但后来我在另一篇文章中读到,默认情况下,令牌在正文内容区域中可用,并且我需要一个特殊的过滤器。我还没有找到过滤器,所以我不确定这个解决方案是否有效。
我调用了variable_get。现在,这确实起作用了。我在 template.php 页面中定义了variable_set。我的值确实使用代码示例中上面的打印调用进行打印。然而,我今天早上查看了此页面,我认为这不是我需要的答案。因为现在我必须在所有页面上调用variable_get,然后才能打印任何内容,对吗?这并不能解决我只想将所有内容设置在一个地方即可在任何地方调用的问题。我尝试将variable_get调用放入我的自定义块中,但我再次无法从我的正文内容区域访问“内容顶部”中的任何内容。 Variable_get 调用打印“内容顶部”中的值,但随后不会在内容区域中的该值下方重新打印。
所以也许该代码可以帮助别人帮助我。我现在将详细了解 CCK,因为这是我尚未尝试过的唯一其他建议。如果有人可以提供帮助,请提前致谢。
I'm having trouble with using PHP code on my pages, within my body content area. I've searched tirelessly on this site, Drupal's site, and other sites in general, so I apologize if the answer is on this site somewhere, but I can't find it and I need help.
We have a lot of info we reuse throughout our site that I'd like to store in a PHP file as variables. We do this on our site now, but I'm rewriting our whole site to use Drupal. So, for example, we sell software, and I'd like a variable for each of our product URLs for various 'add to cart' buttons on the site. I don't want to have to hardcode the product URL into each link, but rather to seta PHP variable that I can call on any drupal page.
I cannot get anything to work; I've read about several suggestions but none work. I've tried setting the variables as their own block, then calling them from within a page when I create a new page. I can echo the variables on the pages but only within the block they are inside, I cannot call them and get them to echo from other blocks or content areas. I've tried using the global keyword (as per one suggestion) but that didn't work for me.
I hope this makes sense. Other info? I'm using Drupal 6.x, I do have PHP code enabled when creating pages, I do have the PHP filter module enabled, I can get PHP code to render so I know it's working, it's just not working where I need it to be.
I should say (if it's not obvious just from reading this!) I am a Drupal newbie so if anyone can help and try to explain their suggestion as plainly as possible for me, I'd really appreciate it!
Thanks in advance.
EDIT 3/15/11
To try to explain further, I'll post some sample code. I haven't done this yet because there isn't much to show yet, and I thought it might confuse the issue even more.
So, I've made a Drupal 'page' which is for our software trial downloads. The PHP variables that I want to set are for our download links; I want to set them in one place so that if, in the future, the download link needs to change, I only have to do so in one spot. You see, we have download links on various site pages. The same is true of our 'buy now' links. Here is the page code:
<p>Try [product] free for 30 days.</p>
<!--<p>[token_custom_ewintry]</p>-->
<p><?php global $ewintry; ?><a href="<?php print $ewintry; ?>">Download for Windows PC</a></p>
<p><?php global $emactry; ?><a href="<?php print $emactry; ?>">Download for Mac OS X</a></p>
<p><?php global $ebbtry; ?><a href="<?php print $ebbtry; ?>">Download for BlackBerry</a></p>
<?php
$ebbtryprint = variable_get("ebbtry", "default");
print $ebbtryprint;
?>
<p><a href="<?php print $ewmtry; ?>">Download for Windows Mobile</a></p>
<p><?php global $ipewlstorelink; ?>iPhone, iPad, iPod touch owners: <a href="<?php print $ipewlstorelink; ?>">Download [product] on the iTunes App Store</a>. You'll be able to create 10 cards for free to try [product] before you buy!</p>
For this sample I've left in everything I've tried. You'll see my calls to global variables, which never worked. I have the global variables defined in a custom block that I created and placed in my 'content top' region. I learned that apparently nothing from that region is actually accesible to my page's body content, because the calls never worked.
I have a custom token that I made yesterday with the Tokens module; it never worked, but then I read on a different post that by default, tokens are available in the body content area, and I need a special filter. I've yet to find a filter, and so I am not sure this solution will ever work.
I have my call to variable_get. Now, this did work. I have variable_set defined within my template.php page. My value does print using the print call above in my code sample. However, I looked at this page this morning and I don't think that's the answer I need. Because now I'll have to call variable_get on all my pages before I can print anything, right? And that doesn't solve the problem where I wanted to only have to set everything in one place to call anywhere. I tried putting the variable_get call in my custom block, but again I can't access anything in 'content top' from my body content area. The variable_get call prints the value in 'content top' but then it will not re-print below that in the content area.
So maybe that code will help someone to help me. I am going to look in detail at CCK now, as that's the only other suggestion I haven't tried. Thanks in advance if anyone can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试设置全局变量,然后使用它在函数/方法块中,您需要在导入时使用 global 关键字:
实际操作: http://jfcoder .com/test/globals.php
打印:
我一直习惯使用 $GLOBALS 设置全局变量,这样做从来没有遇到任何问题。
不过,我要提醒大家的是,设置全局范围的变量被认为是有害的(或者至少是不必要的),因为它们很容易意外地覆盖代码中的其他位置(由您和/或其他人)。
你在描述中所说的方法听起来很混乱;您应该使用数据库并让 Drupal 抽象您如何组织、设置和从数据存储中获取数据,而不是编辑文件并将某些链接和数据硬编码到 PHP 文件中。这就是我读你的描述时的想法,这可能不公平,但我认为我需要提及它。
编辑
在Drupal中,您可以使用
variable_set()
在default/settings.php页面中设置全局变量,然后使用variable_get()
通过名称获取变量。http://api.drupal.org/api /drupal/sites--default--default.settings.php/6
http://api.drupal.org/api/drupal/includes--theme.inc/function/template_preprocess/6
编辑 2
请注意 <
variable_set() 的 href="http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_set/6" rel="nofollow">源
功能:编辑
好的,这是您可以执行的操作:
/drupal-root/htdocs/sites/settings.php
打开 settings.php 文件并在在底部,使用 $GLOBALS 全局变量设置 PHP 变量,如下所示:
然后在模板中(选择 PHP 输入格式):
或者...
您应该看到模板代码中的变量打印在页面上。请注意 $GLOBALS 变量周围的大括号。
If you're trying to set a global variable, and then use it within a function/method block, you need to use the global keyword on import:
In action: http://jfcoder.com/test/globals.php
Prints:
I have always gotten in the habit of setting a global variable using $GLOBALS, I never have any issues doing it this way.
I would caution, though, that setting globally scoped variables is considered harmful (or at least unnecessary), since they are so easy to accidentally overwrite somewhere else in your code (by you and/or someone else).
Your stated approach in the description sounds quite messy; you should be using a database and let Drupal abstract how you organize, set and get the data from the datastore, instead of editing your files and hardcoding some links and data into a PHP file. This is what I'm thinking reading your description, which may not be fair, but I thought I needed to mention it.
EDIT
In Drupal, you can set global variables in the default/settings.php page using
variable_set()
, and then usevariable_get()
to get the variable by name.http://api.drupal.org/api/drupal/sites--default--default.settings.php/6
http://api.drupal.org/api/drupal/includes--theme.inc/function/template_preprocess/6
EDIT 2
Note the source for the
variable_set()
function:EDIT
Ok, here is what you can do:
/drupal-root/htdocs/sites/settings.php
Open the settings.php file and at the bottom, set your PHP variables using the $GLOBALS global variables, as so:
And then in your template (with the PHP Input Format selected):
Or...
And you should see your variable printed out on the page from the template code. Note the curly braces surrounding the $GLOBALS variable.
如果您想提供一些节点附带的附加信息,您应该使用 CCK 创建内容类型,其中包含所有附加信息。通过模板内的样式,您几乎可以归档任何内容。
如果 CCK 不可用(在大多数情况下,这正是您想要的),您需要实现一个
_preprocess_
函数。这看起来像现在您的页面模板文件中有一个
$my_custom_var
可用。请务必熟悉 Drupal 的模板系统(如果您还没有)。
If you want to provide additional info that should go with some nodes, you should use CCK to create a content type, that has all the additional infos. With styling inside a template, you can archive almost anything.
If CCK is not siutable (in most cases, it is exactly what you want), you need to implement a
_preprocess_
function. This would look likeNow you have a
$my_custom_var
in your page-template file available.Be sure to make yourself familiar with the template system of Drupal (if you haven't already).
听起来您正在寻找 token_filter 模块。
我的问题是它还没有为 D7 做好准备(在令牌模块中)。
It sounds like you are looking for the token_filter module.
My problem is that it isn't ready (in token module) for D7 yet.