代码之家  ›  专栏  ›  技术社区  ›  MrSmile cashews

smarty include_php异常

  •  0
  • MrSmile cashews  · 技术社区  · 7 年前

    我想在我的模板中包含一个PHP文件。但是有时候php文件不存在。 我试着通过函数“文件存在”来检查它,但它不起作用。

    {if file_exists("`$plugin`/button.php")}
        {include_php "`$plugin`/button.php"}
    {/if}
    

    致命错误:未捕获-->Smarty编译器:模板中的语法错误 第456行的“file:/home/user/www/site/template/template.tpl” “包括\u php” $plugin /button.php“”include_php文件 '/button.php'不可读<--引发 /home/user/www/site/smarty/sysplugins/smarty_internal_templatecompilerbase.php 在线456

    有没有办法检查PHP文件是否存在?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Andreas    7 年前

    在尝试使用变量之前,一定要检查它们。

    在本例中,$plugin变量为空,导致字符串连接失败。

    如果您首先确保设置了变量,那么如果设置为真,请检查文件是否存在,它应该按预期工作。

    {if isset($plugin) && file_exists("`$plugin`/button.php")}
        {include_php "$plugin/button.php"}
    {/if}
    

    在纯PHP中,这应该可以工作,因为如果ISset失败,它甚至不会尝试文件存在。
    但不确定是否聪明。
    如果它不能这样工作,你需要使它成为一个单独的如果。