What are the real advantages of templating engines over just using PHP?

前端 未结 10 1619
青春惊慌失措
青春惊慌失措 2021-02-05 02:33

I develop my web applications using only PHP for the view files and I don\'t feel limited in any way, but I hear there\'s a consistent number of developers advocating \"external

相关标签:
10条回答
  • 2021-02-05 03:26

    I find myself using templating engines when I need sandboxing. for example, letting users of a hosted CMS edit templates.

    0 讨论(0)
  • 2021-02-05 03:30

    Some templating engines can compile templates leading to highly optimized transforms. Take for example the XslCompiledTransform in .NET.

    0 讨论(0)
  • 2021-02-05 03:34

    New Syntax


    Some people wont agree, but since I've been using Twig the "for ... else" feels right. It might not be a lot, but it keeps my templates that little bit cleaner.

    {% for row in articles %}
     Display articles ...
    {% else %}
     No articles.
    {% endfor %}
    

    Automatic Escaping


    You can have the template engine automatically escape any output. This is great as you no longer have to repeat htmlspecialchars ... everywhere. Twig does this nicely.

    {% autoescape on %}
      Everything will be automatically escaped in this block
    {% endautoescape %}
    

    Template Inheritance


    Another feature I like is the ability to extend base templates. Here's a basic example

    base.html template

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      {% block head %}
        <link rel="stylesheet" href="style.css" />
        <title>{% block title %}{% endblock %} - My Webpage</title>
      {% endblock %}
    </head>
    <body>
      <div id="content">{% block content %}{% endblock %}</div>
      <div id="footer">
        {% block footer %}
          &copy; Copyright 2009 by <a href="http://domain.invalid/">you</a>.
        {% endblock %}
      </div>
    </body>
    

    child.html template

    {% extends "base.html" %}
    
    {% block title %}Index{% endblock %}
    {% block head %}
      {% parent %}
      <style type="text/css">
        .important { color: #336699; }
      </style>
    {% endblock %}
    {% block content %}
      <h1>Index</h1>
      <p class="important">
        Welcome on my awesome homepage.
      </p>
    {% endblock %}
    

    A child template can override blocks for page specific styles, content, etc ... You can also notice the use of {% parent %} which grabs the parents content so you don't lose it all while overriding.

    I recommend you give Twig a go. Extremely useful.

    0 讨论(0)
  • 2021-02-05 03:35

    Your non-answers look like real answers but phrased in a very condescending manner. For example:

    babysitting bad developers (i.e. use a template engine because it forces you to not mix code into presentation)

    I would call this an application of the Rule of Least Power. It makes your template much more useful for all users, not just "bad developers".

    Restrictions are what make programming languages. PHP doesn't have an inline-assembly feature, and it's not because Rasmus thought you are all "babies".

    0 讨论(0)
提交回复
热议问题