1

Is there a way to use .tpl.php templates instead of Twig templates in Drupal 8, or is there a way to write PHP in a Twig file instead of that Twig language?

I am a beginner for Drupal 8, but I have worked with Drupal 7.

7
  • 1
    No you can’t write php in a Twig file, it’s a different language. The “annoying” language is Twig itself, nothing to do with Symfony other than Symfony happens to prefer it (not least because it’s miles ahead of old php-only templates). If you need php logic to prepare variables for a template file, that’s what preprocess functions were made for, same as D7
    – Clive
    Commented Oct 23, 2017 at 11:52
  • So can I call php functions from the twig language? @Clive Commented Oct 23, 2017 at 12:39
  • You could plug-in the phptemplate engine as service. Have a look at hook_extensions() and hook_theme(). But I would not recommend this at all. You should do all logic in preprocess functions and template should solely for displaying the information without any data manipulation. The old phptemplateengine-way was really bad.
    – user21641
    Commented Oct 23, 2017 at 12:43
  • Not directly, but you can write extensions to expose them, yes
    – Clive
    Commented Oct 23, 2017 at 12:48
  • Is there a bot or something that rakes this site and downvotes everything? Seeing a ton of downvotes lately on questions and answers.
    – Kevin
    Commented Oct 23, 2017 at 13:20

2 Answers 2

3

You don't need to write PHP in twig files. Twig is fairly expressive to the degree in which you need to template, for everything else, it should go into the .theme file, theme hook, and/or preprocess function.

There is virtually no reason to have PHP code in a template.

Spend a week with Twig and you won't go back.

Example:

<?php if ($page['content']): ?>
  <?php print render($page['content']); ?>
<?php endif; ?>

vs:

{% if page.content %}
  {{ page.content }}
{% endif %}

Much more readable.

I'll expand with a more advanced example.

Lets say you have list of things you want to output, but every 7 items, you want to make a new unordered list. The end goal is the CSS inlines these lists, think of a superfish menu where menu links are ordered top to bottom, then left to right.

  <div class="global-top__submenu">
    {% for chunk in items|batch(7) %}
      <ul class="global-top__submenu-col">
        {% for item in chunk %}
          <li>
            {{ link(item.title, item.url) }}
          </li>
        {% endfor %}
      </ul>
    {% endfor %}
  </div>

Where items is passed to the template. This small example will output a UL with 7 items until there is nothing left to print.

enter image description here

I have yet to run into an instance where I cannot accomplish something with Twig, or, because it's Twig.

Give it a shot and dive in, forget phptemplate.

3
  • Is there a reason this is downvoted?
    – Kevin
    Commented Oct 23, 2017 at 13:17
  • Seems like a fair answer to me, maybe downvoted because someone was not happy with this answer?
    – JurgenR
    Commented Oct 23, 2017 at 13:26
  • Hi @Kevin this explains a lot. Thanks ! Could you please elaborate how to get the details of a child page from a parent page node in twig. I have done that with tpl in D7. But don't know how to get it with twig. Commented Oct 24, 2017 at 3:54
2

To use custom PHP and display the result in a template, you must go through preprocessing functions (like hook_preprocess_node()) to override the display or add new variables to display in your template. You can create a custom module or use the MYTEHEME.theme file of your current theme to call preprocessing function.

If before you used a lot of PHP code in TPL files, it's bad practice because the files are used to display the data not to make hard particular/specific treatments inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.