翻訳: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 , .



() ()
Twig 3 1 190 3 333
PHPTAL 3.8 2 100 2 632
Dwoo 6.9 1 870 1 449
Smarty 2 12.9 2 350 775
Smarty 3 14.9 3 230 671
Calypso 34.3 620 292
eZ Templates 53 5 850 189


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




, , , Twig .



()
Twig 383
PHPTAL 598
Dwoo 1 645
Smarty 2 1 634
Smarty 3 1 790
Calypso 614
eZ Templates 2 783




, , .



-----------------------------------------------------------------------





. , . Twig, .



: .



All Articles