Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:04:48+00:00 2026-05-28T05:04:48+00:00

I use .twig templates on a website however I need to get pretty crafty

  • 0

I use .twig templates on a website however I need to get pretty crafty with the logic on a few of the views. I dont think twig can handle this and its a piece of cake to do in PHP. (I’m not going to explain the details of what that is, just know its easy in PHP and I actually already have the code). The question is, can I include a .PHP template in base or parent templates that are of type .twig?

Example below (which does not work) – blah.html.php is a template which extends the base.html.twig template…

/some/web/directory/bundlename/Resources/views/blah/blah.html.php

<?php $view->extend('::base.html.twig') ?>

<?php $view['slots']->set('title', 'Welcome to nowhere!') ?>

<?php $view['slots']->start('body') ?>

    <div id="body">
        html stuff here, blah blah blah...
        <?php .... ?>
            complex php and html stuff, and so on...
        </php end....?> 
        you get the idea...
    </div>

<?php $view['slots']->stop() ?>

On the browser the code above renders all the .twig code in the base template as text in the browser (I.E. it does not get executed by the templating engine).

PS – yes I have both .twig and .php set in app/config/config.yml

templating:      { engines: ['twig', 'php'] }

Ok, here are the details of the “complex” stuff…
This is taken from a version of the site which was coded in Symfony 1.4 – Its being upgraded to Symfony 2 and we want to use all .twig

<?php foreach ($all_items as $item): ?>
<tr>
    <td valign="top">
        //<?php include_partial('global/item', array('item' => $item)) ?>

 <<< some code snipped out here >>>

    <td valign="top">
        <table border="1" width="100%">
        <?php echo $form->renderFormTag('memberitems/additem') ?>
        <?php echo $form['item_id']->render(array('value' => $item->getIid())) ?>
        <?php echo $form['user_id']->render(array('value' => 1)) ?>
        <?php echo $form['_csrf_token'] ?>
            <tr>
                <td width="50" colspan="2" align="center">Quantity<br><?php echo $form['quantity']->render() ?></td>
                <td width="50" colspan="2" align="center">Amount<br><?php echo $form['value']->render() ?></td>
                <td width="50" colspan="2" align="center"><input type="submit" value="Add"></td>
            </tr>
        </form>

    <<< here is a complex portion, we're declaring a variable with a value >>>
    <<< cant do this with twig - from what I know... >>>

        <?php // determine if item is in registry - if so display "Currently Asking For:" ?>
        <?php $is_in_registry = false; ?>
        <?php foreach($member_items as $m_item): ?>
        <?php   if($m_item->getIid() == $item->getIid()): ?>
        <?php       if($is_in_registry == false): ?>
        <?php           $is_in_registry = true; ?>
                        <tr>
                            <td width="50" colspan="6" align="center"><br>Currently Asking For:</td>
                        </tr>
        <?php       endif ?>

        <?php echo $form->renderFormTag('memberitems/removeitem') ?>
        <?php echo $form['item_id']->render(array('value' => $item->getIid())) ?>
        <?php echo $form['user_id']->render(array('value' => 1)) ?>
        <?php echo $form['id']->render(array('value' => $m_item->getMIid())) ?>
        <?php echo $form['_csrf_token']->render() ?>

                        <tr>
                            <td width="50" colspan="4" align="center">
                                <?php echo $m_item->getMIqty()." @ ".$m_item->getMIval() ?>
                            </td>
                            <td width="50" colspan="2" align="center"><input type="submit" value="Remove"></td>
                        </tr>
                    </form>
        <?php   endif ?>
        <?php endforeach ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:04:49+00:00Added an answer on May 28, 2026 at 5:04 am

    Complex logic should really be put in the service layer in Symfony2. The templates should look clean and tidy for all the devs not familiar with PHP (e.g front-end developers).

    However, I don’t see anything in that template that couldn’t be done in Twig. Here’s the tricky part written in Twig to prove it:

    {% set is_in_registry = false %}
    {% for m_item in member_items %}
    
        {% if m_item.getIid == item.getIid %}
            {% if is_in_registry is false %}
                {% set is_in_registry = true %}
                <tr>
                    {# you should use css for styling the td #}
                    <td width="50" colspan="6" align="center"><br />Currently asking for:</td>
                </tr>
            {% endif %}
    
            {{ form.renderFormTag('memberitems/removeitem') }}
            {{ form.item_id.render({ value: item.getIid }) }}
            {{ form.user_id.render({ value: 1 }) }}
            {{ form.id.render({ value: m_item.getMIid }) }}
            {{ form._csrf_token.render }}
    
            <tr>
                <td width="50" colspan="4" align="center">
                    {{ m_item.getMIqty ~ ' @ ' ~ m_item.getMIval }}
                </td>
                <td width="50" colspan="2" align="center"><input type="submit" value="Remove"></td>
            </tr>
            {# didn't notice your form opening tag. I'm assuming you're dynamically generating it #}
            </form>
        {% endif %}
    {% endfor %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question: How do I permit use of debug in Twig templates within Silex?
In Symfony 2 templates (using Twig), how can I effectively check whether a user
Using symfony2 with twig templates, I normally use js embedded inside the *html.twig file
In PHP templates I can use php functions, for example: foreach ($users as $user){
I'm trying to use translation in a custom twig filter like this public function
I use embedded controller to render form that's used on multiple pages: Twig {%
I have a form I need to use on multiple pages: Controller $emailForm =
I can't really understand the difference between macros and includes in Twig: both seems
I'm wondering the best way to separate my plugin's templates from the actual logic.
Why do I get my string two times in the output? #!/usr/bin/perl use warnings;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.