MyBB Community Forums

Full Version: Template Engine
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
MyBB 2.0 is using the Twig templating engine rather than the current system of basic PHP templates. Twig offers a lot more functionality, such as built in support for conditional "if" statements, loops, including other templates and more. Twig was chosen for its clean syntax, readability and high level of usage (it is used in hundreds of other projects, meaning it's fairly easy to learn and many people already understand it).

Some examples

To give a brief introduction to how Twig works, here are a few samples of templates from the 2.0 core. This should give you a taste of the syntax, but we highly recommend reading the "Twig for template designers" guide if you are a theme designer.

<div class="likes">
    <span class="likes__prefix">
        <i class="fa fa-caret-{{ langDir.right }}"></i>
    </span>
    {% for key, like in likes %}
        <span class="likes__like" data-like-id="{{ like.id }}">
            {% if like.user.name != trans('likes.current_user') %}
                <a href="{{ url_route('user.profile', {'slug': like.user.name, 'id': like.user.id}) }}">{{ like.user.name }}</a>
            {% else %}
                {{ like.user.name }}
            {% endif %}
        </span>
        {% if key < likes|length - 2 or (key == likes|length - 2 and numOtherLikes > 0) %} {# don't show a comma at the end of the list. Key is zero-based, so minus 1. #}
            <span class="likes__separator likes__separator--comma">
                {{ trans('likes.sep_comma') }}
            </span>
        {% elseif key == likes|length - 2 %}
            <span class="likes__separator likes__separator--and">
                {{ trans('likes.sep_and') }}
            </span>
        {% endif %}
    {% endfor %}
    {% if numOtherLikes > 0 %}
        <span class="likes__separator likes__separator--comma">
                {{ trans('likes.sep_and') }}
        </span>
        <span class="likes__others">
            <a href="{{ viewAllLikesLink }}">{{ trans_choice('likes.others', numOtherLikes, {'numOthers': numOtherLikes}) }}</a>
        </span>
    {% endif %}
    {{ trans('likes.like_this') }}
</div>

This template is used to render the usernames of users who have liked a post. It makes use of loops, condditional statements and functions (such as "trans", which translates a language variable).

Providing feedback

Due to how much work has been done, it is highly unlikely that we will be changing template engine away from Twig. We made the choice to use Twig after evaluating several other options (such as Blade, Smarty, and pure PHP).
What made you decide Twig over Smarty?

From the speed comparisons I saw when I made this decision for myself, Smarty was significantly faster, especially with caching. The main supporters of Twig seemed to be supporting it because its a Symfony project.

Aside from that, I may have ended up choosing Smarty because I prefer it's simpler syntax.
We chose Twig due to a lot of the team having experience with it, it being an easy syntax to learn, there being a lot of tutorials and resources and the easy extensibility. Both Smarty and Twig are fairly similar to each other, but Twig tends to be better maintained in my experience.

Regarding speed comparisons, the speed isn't a major issue once the templates are cached. The initial compilation for both is fairly similar and is affected by the number of defined filters and functions.
I hope when this is released there will be a bunch of tutorials somewhere on here this looks very different. If your trying to change templates in a skin> Php would be just fine.
(2016-05-22, 01:40 PM)DeMoN100 Wrote: [ -> ]I hope when this is released there will be a bunch of tutorials somewhere on here this looks very different. If your trying to change templates in a skin> Php would be just fine.

http://twig.sensiolabs.org/documentation

It's very simple. As far as I'm aware Twig doesn't allow you to use raw PHP in templates anyway.
(2016-05-22, 01:49 PM)Nathan Malcolm Wrote: [ -> ]
(2016-05-22, 01:40 PM)DeMoN100 Wrote: [ -> ]I hope when this is released there will be a bunch of tutorials somewhere on here this looks very different. If your trying to change templates in a skin> Php would be just fine.

http://twig.sensiolabs.org/documentation

It's very simple. As far as I'm aware Twig doesn't allow you to use raw PHP in templates anyway.

Yep, which is one of the reasons it was chosen over alternatives such as Blade. It's more powerful than the current system, has an easy to read syntax, and is fairly secure.
but the html will saved in Database? like actually work mybb 1.8.7 because it has been very practical and good
(2016-07-21, 01:12 PM)Augustino7 Wrote: [ -> ]but the html will saved in Database? like actually work mybb 1.8.7 because it has been very practical and good

No, templates are served as files. This allows them to be edited via both the ACP and via a standard text editor (or IDE), giving designers, developers and administrators much more power.