翻訳:PHPのテンプレートエンジン

, Fabien Potencier, Symfony. PHP Twig — .

-----------------------------------------------------------------------
, PHP ? … . . PHP , .

-, . , Symfony, Sensio MVC . , . ( ) - () .

. , .

PHP , , , « PHP». , .

PHP () ?

PHP ? - , PHP -, . PHP , , ? .

1995 , PHP/FI:
<!--include /text/header.html-->
 
<!--getenv HTTP_USER_AGENT-->
<!--ifsubstr $exec_result Mozilla-->
  Hey, you are using Netscape!<p>
<!--endif-->
 
<!--sql database select * from table where user='$username'-->
<!--ifless $numentries 1-->
  Sorry, that record does not exist<p>
<!--endif exit-->
  Welcome <!--$user-->!<p>
  You have <!--$index:0--> credits left in your account.<p>
 
<!--include /text/footer.html-->


, PHP .

Django .


:


PHP . 14 (, <?= ):
<?php echo $var ?>


PHP (, )
<?php  echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>


Django:
{{ var }}
{{ var|escape }}



, . , , . , PHP :
<?php if ($items): ?>
   <?php foreach ($items as $item): ?>
    * <?php echo $item ?>
  <?php endforeach; ?>
<?php else: ?>
    No item has been found.
<?php endif; ?>


Django — else for:
{% for item in items %}
  * {{ item }}
{% else %}
  No item has been found.
{% endfor %}



PHP . 5- , , . .. , — .

Django , :
<!-- base.html -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    {% block head %}
      <link rel="stylesheet" href="main.css" />

    {% endblock %}
  </head>
  <body>
    {% block content %}{% endblock %}
  </body>
</html>
 
<!-- index.html -->
{% extends "base.html" %}

 
{% block head %}
  {{ block.super }}
  <link rel="stylesheet" href="main.css" />
{% endblock %}
 
{% block content %}
  Index content
{% endblock %}


, . , «-».


PHP . , :
<?php echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>


, , :
<?php echo e($var) ?>


, , , XSS CSRF .

, Symfony (2006); : Django 1.0, Ruby on Rails  3.

(?) . :
{% autoescape off %}
  {{ object.as_html }}
{% endautoescape %}
 
{{ object.as_html|safe }}
 


, . JavaScript, .


«»

(, ). , . «» : / , . .

PHP, Django , .

PHP


. , . , .

PHP , «». . . .


Smarty Smarty 3

. Django «» Smarty. Smarty - PHP.

Hello {$name|escape}
{section name=item loop=$items}
  {$items[item]|escape}
{/section}


Smarty :
, Smarty 3 - :
, ( )

PHPTAL

PHPTAL , Zope. , , -HTML , , RSS .
<?xml version="1.0"?>
<html>
  <body>
    Hello <span tal:content="name" />
    <ul tal:repeat="item items">
      <li tal:content="item"></li>
    </ul>
  </body>
</html>


-, , :
<html metal:use-macro="layout.xml/main">
 <body metal:fill-slot="content">
   Hello <span tal:content="name" />
   <ul tal:repeat="a array">
     <li tal:content="a"></li>
   </ul>
 </body>
</html>

<html metal:define-macro="main">
  <metal:block define-slot="content"/>
</html>
 

« » HTML .

eZ Components Templates

eZ Components . … , :
{use $name}
{use $items}
Hello {$name}
{foreach $items as $item}
 * {$item}
{/foreach}

, . , .

Dwoo

Dwoo — , , . Smarty. :
<html>
  <body>
    {block "content"}{/block}
  </body>
</html>


{extends "layout.tpl"}
 
{block "content"}
  {include("basic.tpl")}
{/block}


Dwoo Smarty, , , , Smarty.

, Dwoo «» .

Calypso

Calypso Django PHP. .. Django . .

Twig

PHP , Django. «» Twig. Armin Ronacher, of Jinja ( Python). Jinja. Twig Jinja, Django, .

Twig 2008 Chypr. Python.

, . Calypso Twig PHP-. . , . , «» «» .

, . . : , PHPDoc; :
Twig , .

()()
Twig31 1903 333
PHPTAL3.82 1002 632
Dwoo6.91 8701 449
Smarty 212.92 350775
Smarty 314.93 230671
Calypso34.3620292
eZ Templates535 850189

1 , . — 10 ; 1 10 000 (). , (header) (footer) , , .


, , , Twig .

()
Twig383
PHPTAL598
Dwoo1 645
Smarty 21 634
Smarty 31 790
Calypso614
eZ Templates2 783


, , .

-----------------------------------------------------------------------
. , . Twig, .

: .


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


All Articles