5

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. 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". I tried setting new smarty, I also tried using template function (plugin) but I still get white page. Any suggestions would be appreciated! My table.php code: ($items function reads from the file)

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

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

Edit: I used $smarty = new SmartyBC(); and changed to {php} tags. It no longer shows white screen but the table.php code doesn't work -the table doesn't show.

Is there a smarter way to do this? Other than including php file? Edit: I got it working by using foreach loop inside the tpl but I wonder if it's the right way to do this?

010101010
  • 65
  • 1
  • 1
  • 8
  • A white page usually means that you have an internal server error. Have you checked your error log? A good idea is also to turn `display_errors` on in your local PHP environment. Read more here: [How do I get PHP errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). You simply can't execute PHP code in smarty templates (even if you do include it). – M. Eriksson Apr 29 '18 at 02:37
  • @MagnusEriksson I used display_errors to fix some other problems, thank you for that. Do you have any other idea about how I should handle this issue? Thank you for your input – 010101010 Apr 29 '18 at 12:18

2 Answers2

2

You should not inject php code in any kind of template (not only Smarty). Load your data and perform your logic in php and render in the templating. engine. There is no need for template functions or to include php in your case.

PHP file

// 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

<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:

jirig
  • 551
  • 6
  • 21
1

Use {php} tag then include the php file path inside it

{php}
  include('table.php');
{/php}
Vinay
  • 7,442
  • 6
  • 25
  • 48
  • 1
    Please avoid including raw php code in templates. It doesn't belong there. If your really need some complex logic in your template, use `block functions`: https://www.smarty.net/docsv2/en/plugins.block.functions.tpl – jirig Apr 29 '18 at 13:31
  • 1
    Agree but nothing is perfect sometimes you do need to include php in view - say you have a huge calculation/rendering in php file and porting it to controller + smarty tpl will take much more time than your project manager can allot.We all know best practises yet we bound to stray away from them on those 'desparate' times. – Vinay Apr 29 '18 at 16:53
  • Well that's a problem with legacy code or bad decisions. If you have a big calculation-rendering thing in your php code it is already a big pain to work with, so converting it to template based rendering would save a plenty of time later on. There should be no need to inject raw php code to template in this particular case since this is a small school project. I am not arguing that manager would allocate enough time to rewrite it, that really depends (sadly I know how it usually goes). – jirig Apr 29 '18 at 17:19
  • Yes that's true. – Vinay Apr 30 '18 at 02:58