简体   繁体   English

如何在.tpl文件中包含php代码

[英]How to include php code in .tpl file

I have a small school project which I've almost finished. 我有一个小学校项目,我差不多完成了。 But now I have to change my working code and use template instead. 但现在我必须改变我的工作代码并改用模板。 I chose Smarty. 我选择了Smarty。 Table displays data from a form. 表显示表单中的数据。 Data is stored in the text file and each element is on new line. 数据存储在文本文件中,每个元素都在新行上。 Everything worked before but now that I can't figure out how to how to display my table. 一切都工作,但现在我无法弄清楚如何显示我的表。 With my current code, my page turns white. 使用我当前的代码,我的页面变为白色。 I debugged it and got an error "is deprecated, use SmartyBC class to enable". 我调试它并得到一个错误“已弃用,使用SmartyBC类启用”。 I tried setting new smarty, I also tried using template function (plugin) but I still get white page. 我尝试设置新的smarty,我也尝试使用模板功能(插件),但我仍然得到白页。 Any suggestions would be appreciated! 任何建议,将不胜感激! My table.php code: ($items function reads from the file) 我的table.php代码:($ items函数从文件中读取)

<?php
$count = 0;
if (isset($Items)){
    foreach ($Items as $item) {
        if($count == 0){
            print "<tr><td>$item</td>";
            $count += 1;
        } else if($count == 1) {
            print "<td>$item</td>";
            $count +=1;
        } else if($count == 2) {
            print"<td>$item</td></tr>";
            $count = 0;
        }

    }
}

tpl file tpl文件

    <table>
    <tr>
        <th>Name</th>
        <th>Lastname</th>
        <th>Phone</th>
    </tr>
    {include_php file='table.php'}
</table>

Edit: I used $smarty = new SmartyBC(); 编辑:我使用了$ smarty = new SmartyBC(); and changed to {php} tags. 并更改为{php}标签。 It no longer shows white screen but the table.php code doesn't work -the table doesn't show. 它不再显示白屏,但table.php代码不起作用 - 表不显示。

Is there a smarter way to do this? 有更聪明的方法吗? Other than including php file? 除了包括php文件? Edit: I got it working by using foreach loop inside the tpl but I wonder if it's the right way to do this? 编辑:我通过在tpl中使用foreach循环来实现它,但我想知道这是否是正确的方法呢?

Use {php} tag then include the php file path inside it 使用{php}标签然后在其中包含php文件路径

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

You should not inject php code in any kind of template (not only Smarty). 你不应该在任何模板中注入PHP代码(不仅仅是Smarty)。 Load your data and perform your logic in php and render in the templating. 加载数据并在php中执行逻辑并在模板中渲染。 engine. 发动机。 There is no need for template functions or to include php in your case. 在您的情况下,不需要模板函数或包含php。

PHP file PHP文件

// Initiate smarty
$smarty = new Smarty ...;
...

// Somehow load your data from file
$itemsFromFile = somehow_load_data_from_file( ... );
...

// PAss your data to Smarty
$smarty->assign('items', $itemsFromFile);
...

// Render your template
$smarty->display( ... );

TPL file TPL文件

<table>
    <tr>
        <th>Name</th>
        <th>Lastname</th>
        <th>Phone</th>
    </tr>

    {foreach $items as $key => $item}
        {if $key % 3 == 0}
            <tr>
        {/if}
                <td>$item</td>
        {if $key % 3 == 2}
            </tr>
        {/if}
    {/foreach}
</table>

Use the advantages of the templating engine. 利用模板引擎的优势。 You can use modulus of three instead of counting to two and then reseting to zero. 您可以使用三个模数而不是计数到两个,然后重置为零。

Sources: 资料来源:

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

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