Prestashop PHP 包含在 .tpl 中不起作用

发布于 2024-12-02 12:43:13 字数 763 浏览 0 评论 0原文

我正在使用 Prestashop 为客户创建一个店面。 Prestashop 使用 Smarty .TPL 文件。我通读了 smarty 文档并浏览了网络,但所有建议都不起作用。

我首先使用常规 .php 页面创建了一个网站,并在每个页面上包含 header.php。

然后我为 prestashop 创建了一个目录并进行了设置。我编辑了 header.tpl 文件并能够在 header.php 代码中进行硬编码。问题是;当我想编辑标题(导航栏、图像、社交媒体)时,我必须在两个不同的地方进行编辑。所以我尝试“包含”我的 header.php 文件。

不过,当我尝试使用 smarty 的 {include_PHP "file.php"} 和/或 {PHP}include...{PHP} 时,Prestashop 出错并给我一个空白的白页 - 没有给出错误 - (在 chrome 中它给出我是“服务器错误”),直到我取出包含的内容。

我尝试用 smarty include 和另一段具有 header hook 的代码替换整个 header.tpl 代码,但这些都不起作用。有什么建议吗?我只想要一个标题,只需编辑一次即可进行更改。

使用 Prestashop v 1.4.4.0

编辑:我将 allowed_php 从 false 更改为 true。现在它正在尝试添加该文件,但它说找不到该文件。我将它放在 header.tpl 旁边并使用:

{php}
            include('navBar.php'); 
       {/php} 

I am creating a storefront for a client using Prestashop. Prestashop uses Smarty .TPL files. I read through smarty documentation and scoured the web but all of the suggestions are not working.

I first made a site using regular .php pages and I am including the header.php on every page.

I then made a directory for prestashop and got it set up. I edited the header.tpl file and was able to hard code in the header.php code. The problem with this is; when I want to edit the header (nav bar, images, social media), I will have to edit it in two different places. So I tried to "Include" my header.php file.

Though, when I try using smarty's {include_PHP "file.php"} and/or {PHP}include...{PHP}, Prestashop errors out and gives me a blank white page - no errors are given - (in chrome it gives me a "server error") until I take out the includes.

I tried replacing the entire header.tpl code with a smarty include and another piece of code that had a header hook, but none of these worked. Any suggestions? I just want one header where I only have to edit it once to make changes.

Using Prestashop v 1.4.4.0

Edit: I changed allow_php to true from false. Now it's trying to add the file, though it says it can't find the file. I placed it next to header.tpl and just used:

{php}
            include('navBar.php'); 
       {/php} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-12-09 12:43:13

回答了!

使用 Smarty .TPL 文件时,当您包含某些内容时,您不会从正在处理的文件的路径中包含内容。您包括索引所在的位置。

示例:

我正在处理 header.tpl,它位于:
siteroot/smartyinstall/themes/themename/header.tpl

当 include 查找该文件时,它实际上是在 smarty 根文件夹中查找它,因为 header.tpl被拉入 smartyinstall 文件夹中的 index.html 页面。

所以,你必须从那里开始。就我而言,我试图包含的标头位于:
siteroot/includes/navBar.php

所以,我必须编写 include('../includes/navBar.php'); ,只向上一个目录,而不是四个目录。

我希望这对遇到此类问题的每个人都有帮助!

ANSWERED!

When using Smarty .TPL files, when you include something, you are not including from the path of the file you are working on. You are including from where the index is.

Example:

I am working on header.tpl, this is located in:
siteroot/smartyinstall/themes/themename/header.tpl

When the include is looking for the file, it is actually looking for it in the smarty root folder because the header.tpl is being pulled into the index.html page that is in the smartyinstall folder.

So, you have to go from there. In my case, the header that I was trying to include was in:
siteroot/includes/navBar.php

so, I had to write include('../includes/navBar.php');, only going up one directory, instead of four.

I hope this helps everyone that has a problem like this!

仅一夜美梦 2024-12-09 12:43:13

在 smarty .tpl 文件中包含 php 被认为是非常糟糕的做法,因此我强烈建议您不要以这种方式添加代码。禁用 {php} 标记的主要原因之一是帮助防止代码注入攻击。电子商务网站本质上是被利用的自然目标。

更好的方法是重写 FrontController 类,将自定义代码分配给 smarty 变量 - 然后可以将其插入到 header.tpl 中,而无需使用 php include()< /代码>。

由于 Prestashop 1.4.x 中提供了类和控制器覆盖,因此您没有理由需要对核心发行版进行黑客攻击和/或修改。

保罗

It's considered very bad practice to include php in smarty .tpl files so I would strongly recommend you don't add code this way. One of the main reasons for disabling the {php} tag is to help prevent code injection attacks. eCommerce sites are by their very nature natural targets for exploits.

A better approach would be to override the FrontController class to assign your custom code to a smarty variable - this could then be inserted into the header.tpl without resorting to using a php include().

With the class and controller overrides available in Prestashop 1.4.x there's no real reason you should need to resort to hacks and/or modifications to the core distribution.

Paul

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文