YesNoOk
avatar

Change logs week 25, 2012 (Read 2199 times)

Started by Nightwish, June 17, 2012, 18:54:02

0 Members and 1 Guest are viewing this.
New #1
Change logs week 25, 2012 |
June 17, 2012, 18:54:02
After a short while of inactivity, I'm back with some major new stuff.

The main new thing I've been working on the last couple of days was the integration of Twig. Twig is an advanced PHP template engine and is part of the Symfony2 PHP framework, a stand alone version is, however, available.

Eos Alpha can now use Twig templates but compatibility with the old (SMF - style) templates is kept as long as I need it and I will most likely need it for quite some time. Right now, only a very few templates have been converted to Twig (one of them being the board index) and because of the sheer number of templates in SMF, it will probably take a long time to convert them all.

I started to experiment with Twig a while ago and quickly realized that there are a number of benefits of using such a modern template engine.
  • Twig has an easy and very readable syntax. Twig templates look A LOT less messy than SMF's pure PHP templates with all their echo statements and lots of punctuation needed (thus, lots of mistakes to make when authoring a new template).
  • Twig has some really powerful features, like macros, inheritance and complex expressions.
  • Twig can easily be extended with own tags, functions, filters and more. This might come in handy at a later stage.
My biggest concern was a possibly huge performance impact when using a template engine, but to my surprise, there is almost none. Twig compiles templates into pure PHP code, which means they can benefit from PHP accelerators like APC or XCache just like any other piece of PHP code can. Switching from the default to the Twig template for the board index did not change the execution time at all - not even by a single millisecond.

Here is a small comparison, first the template that displays the software credits in its old form (pure PHP code)
Code: (php)
function template_credits()
{
global $context, $txt;

// The most important part - the credits :P.
echo '
<div class="main_section" id="credits">
<div class="cat_bar2">
<h3 class="catbg">', $txt['credits'], '</h3>
</div>
<div class="blue_container cleantop">
<div class="content">
  ',$context['credits_intro'],'
</div>
</div>';

foreach ($context['credits'] as $section)
{
if (isset($section['pretext']))
echo '
<br>
<div class="blue_container">
<div class="content">
<p>', $section['pretext'], '</p>
</div>
</div>';

if (isset($section['title']))
echo '
<br>
<div class="cat_bar">
<h3 class="catbg">', $section['title'], '</h3>
</div>';

echo '
<div class="blue_container cleantop">
<div class="content">
<dl>';

foreach ($section['groups'] as $group)
{
if (isset($group['title']))
echo '
<dt>
<strong>', $group['title'], '</strong>
</dt>
<dd>';

// Try to make this read nicely.
if (count($group['members']) <= 2)
echo implode(' ' . $txt['credits_and'] . ' ', $group['members']);
else
{
$last_peep = array_pop($group['members']);
echo implode(', ', $group['members']), ' ', $txt['credits_and'], ' ', $last_peep;
}

echo '
</dd>';
}

echo '
</dl>';

if (isset($section['posttext']))
echo '
<p class="posttext">', $section['posttext'], '</p>';

echo '
</div>
</div>';
}

echo '
<br>
<div class="cat_bar2">
<h3 class="catbg">', $txt['credits_copyright'], '</h3>
</div>
<div class="blue_container cleantop">
<div class="content">
<dl>
<dt><strong>', $txt['credits_forum'], '</strong></dt>', '
<dd>', $context['copyrights']['smf'];

echo '
</dd>
</dl>';

if (!empty($context['copyrights']['mods']))
{
echo '
<dl>
<dt><strong>', $txt['credits_modifications'], '</strong></dt>
<dd>', implode('</dd><dd>', $context['copyrights']['mods']), '</dd>
</dl>';
}

echo '
</div>
</div>
</div>';
}

And here is the Twigified version:
Code: (html)
{% extends "base.twig" %}
{% block content %}
  <div class="main_section" id="credits">
    <div class="cat_bar2">
      <h3 class="catbg">{{ T.credits }}</h3>
    </div>
    <div class="blue_container cleantop">
     <div class="content">
      {{ C.credits_intro }}
     </div>
    </div>
    {% for section in C.credits %}
      {% if section.pretext is defined %}
        <br>
        <div class="blue_container">
          <div class="content">
            <p>{{ section.pretext }}</p>
          </div>
        </div>
      {% endif %}
      {% if section.title is defined %}
        <br>
        <div class="cat_bar">
          <h3 class="catbg">{{ section.title }}</h3>
        </div>
      {% endif %}
      <div class="blue_container cleantop">
        <div class="content">
          <dl>
            {% for group in section.groups %}
              {% if group.title is defined %}
                <dt>
                  <strong>{{ group.title }}</strong>
                </dt>
                <dd>
              {% endif %}
              {% if group.members|length <= 2 %}
                {{ implode(' ' ~ T.credits_and ~ ' ', group.members) }}
              {% else %}
                {{ implode(', ', group.members) }} {{ T.credits_and }} {{ group.last_peep }}
              {% endif %}
                </dd>
            {% endfor %}
          </dl>
          {% if section.posttext is defined %}
            <p class="posttext">{{ section.posttext }}</p>
          {% endif %}
        </div>
      </div>
  {% endfor %}
  <br>
  <div class="cat_bar2">
    <h3 class="catbg">{{ T.credits_copyright }}</h3>
  </div>
  <div class="blue_container cleantop">
    <div class="content">
      <dl>
        <dt><strong>{{ T.credits_forum }}</strong></dt>
        <dd>{{ C.copyrights.smf }}</dd>
      </dl>
      {% if C.copyrights.mods is not empty %}
        <dl>
          <dt><strong>{{ T.credits_modifications }}</strong></dt>
          <dd>{{ implode('</dd><dd>', C.copyrights.mods) }}</dd>
        </dl>
      {% endif %}
    </div>
  </div>
</div>
{% endblock content %}
__
Every program has at least one bug and can be shortened by at least one instruction -- from which, by induction, one can deduce that every program can be reduced to a single instruction that doesn't work.
My SMF-based forum fork
Last Edit: July 14, 2012, 03:12:04 by Nightwish