What are the benefits of Smarty when used with PHP, or is it better to use pure PHP?

I decide whether to use any template engine in php code or not. Now I will look carefully at Smarty.

Honestly, for me it is not clear what are the benefits.

My impressions are not very good from the very beginning. I tried to go through the demo application: http://www.smarty.net/sampleapp1 . And from the very beginning I received notifications. I fixed this by passing values โ€‹โ€‹to the forms. I am changing the code from

// adding a guestbook entry
$guestbook->displayForm();

to

// adding a guestbook entry
$guestbook->displayForm(array('Name' => '', 'Comment' => ''));

Guessing this, I understand that this complicates code debugging. And it also makes me learn another language - itโ€™s easy, but the easiest way is not to learn something new at all, if there is no need. An alternative is to use PHP itself.

http://www.smarty.net/syntax_comparison : .... PHP , Smarty . , - php-, Smarty . , - .

Smarty , .

. ? ? - ?

Smarty, . !

UPDATE:

: Smarty PHP?. , Smarty. 1,5 . , - ?

+3
4

, , , Smarty, . :

1: , PHP, HTML . , Smarty? .

2: : PHP , Smarty, . :

<div>Product list</div>
<?php foreach($products as $product): ?>

<div>Product: <?php echo $product->name; ?> </div>
<div>Price: <?php echo $product->price; ?> </div>
<?php if($product->discountPercentage): ?>
<div>
Discount: <?php echo $product->price * $product->discountPercentage / 100; ?> off! </div>
<?php endif; ?>

<?php endforeach; ?>
+4

, : .

(, ), smarty :

<?php echo $some_var; ?>

:.

{$some_var}

.

. ( smarty)

( , ), .

, , , .

? , :

-, , ..

- smarty - , .

- javascript- - , :

<script type='text/javascript'>
    $(document).ready(function(){
        //foo
    });
</script>

ok, js , , js. smarty , , :

{literal}{\literal}

, ....

, , , .

+2

Smarty.

,    :

1. Parsing of smarty templates to PHP which make things slow
2. Your learning ablity to cope-up with smarty syntax.

:

  • - .
  • , DRY. ( erb - ROR)

, - PHP, Smarty.

0

, Smarty , , , , . , . -

, , , () . - HTML PHP . Smarty , Smarty Template {extends} . , ( doctype, SO ) -

`

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Description" content="{block name=meta_description}{/block}" />
    <title>My Smarty Site - {block name=pageTitle}{/block}</title>
    {block name=additionalHeaders}{/block}
 </head>
    <body>
        <div id="headerDIV">
            <div id="nav-box">
                <!-- Nav would go here -->
            </div>
        </div><!-- end #headerDIV -->
        <div id="mainContainer">
            <div id="generalContent">
                    <div id="pageTitle">
                        {block name=pageTitle}{/block}
                    </div>
                 {block name=pageText1}{/block}
                <div id="page-bottom">
                    {block name=pageText2}{/block}
                </div>
            </div> <!-- end #generalContent -->
        </div> <!-- end #mainContainer -->
    </body>

` :

{extends file="myTemplate.tpl"}
{block name=packageTitle}Simple Template{/block}
{block name=meta_description}This is a simple Smarty Template test{/block}
{block name=additionalHeaders}
    <link rel="stylesheet" type="text/css" href="/css/smarty_test.css"/>
{/block}

{block name=pageText1}
    <p>Hello, {$name}! This is a Smarty Test.</p>
{/block}
{block name=pageText2}
<p>This is some more text....</p>
{/block}

, , , Smarty . - - ; , (, PHP).

, Smarty , , . , PHP, , . , - , , .. Smarty, , PHP.

Smarty, , , , , , .

0
source

Source: https://habr.com/ru/post/1787066/


All Articles