greenboxgoolu
greenboxgoolu

Reputation: 139

Converting TWIG to PHP Syntax

<td class="text-left">
      <select name="product[{{ list_row }}][status]" id="input-status" class="form-control">
          {% if i.status %}
            <option value="1" selected="selected">{{ text_enabled }}</option>
        <option value="0">{{ text_disabled }}</option>
             {% else %}
             <option value="1">{{ text_enabled }}</option>
               <option value="0" selected="selected">{{ text_disabled }}</option>
             {% endif %}
      </select>
</td>
    <td class="text-left">
           <button id="button_cancel" type="button" onclick="$('#product_row_{{ list_row }}').remove()" data-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger">
      <i class="fa fa-minus-circle"></i>
        </button>
          </td> 
             </tr>
       {% set list_row = list_row + 1 %}
        {% endfor %}
        {% endif %}

For above code "Select" how should i convert it to php syntax?

Same goes to set list_row = list_row +1 ? what is %} < means ?

Upvotes: 1

Views: 2569

Answers (1)

Will B.
Will B.

Reputation: 18416

There is not much different from the syntax of PHP and Twig. Twig offers a pure templating perspective from PHP, in order to help keep your templates free of business logic and provides capabilities like auto-escaping of HTML entities and Javascript, and silently ignoring undefined variable, array indexes or object methods/properties, similar to the ?? (null coalesce) operator in PHP .

I highly recommend that you take a day and learn the very limited syntax that Twig has https://twig.symfony.com/doc/2.x/ there are subtle differences between versions 1, 2 and 3. With a great explanation of how twig works in https://twig.symfony.com/doc/2.x/templates.html

Twig Output

{{ var|e }}

Is the equivalent to

<?php echo htmlspecialchars((isset($var) ? $var : null), ENT_QUOTES, 'UTF-8'); ?>

Twig Variable Declarations

{% set var = 'value' %}

Is the equivalent to

<?php $var = 'value'; ?>

So {{ }} can be thought of as a shortcut for printing output <?php echo ?> or rather <?= ?>.
Where {% %} are open and close twig statement delimiters and can be thought of similarly to the open and close PHP statement delimiters <?php ?>.

It is important to note, that twig does not have access to the PHP globals or namespace. So something like <?php foreach ($vars as $i => $var) { ?> is handled differently in twig, for this instance {% for i, var in vars %}.

Twig Variable Access

Twig is also able to access both associative array keys and object properties with the same syntax var.property, with the ability to automatically call get, is and has methods of an object.

If var is an array, {{ var.property }} is the equivalent to

$var = ['property' => 'value'];

echo htmlspecialchars(($var['property'] ?? null), ENT_QUOTES, 'UTF-8'); //PHP 7.0+
echo htmlspecialchars((array_key_exists('property', $var) ? $var['property'] : null), ENT_QUOTES, 'UTF-8'); //PHP < 7.0

if var is an object, {{ var.property }} is the equivalent to

class Var
{
    private $property = 'value';

    public function getProperty()
    {
       return $property;
    }
}
$var = new Var();

if (isset($var->property)) {
    echo htmlspecialchars($var->property, ENT_QUOTES, 'UTF-8');
} elseif (is_callable([$var, 'property'])) {
    echo htmlspecialchars($var->property(), ENT_QUOTES, 'UTF-8');
} elseif (is_callable([$var, 'getProperty'])) {
    echo htmlspecialchars($var->getProperty(), ENT_QUOTES, 'UTF-8');
} elseif (is_callable([$var, 'isProperty'])) {
    echo htmlspecialchars($var->isProperty(), ENT_QUOTES, 'UTF-8');
} elseif (is_callable([$var, 'hasProperty'])) {
    echo htmlspecialchars($var->hasProperty(), ENT_QUOTES, 'UTF-8');
} else {
    echo null;
}



There are portions of your template missing. Such as the template variables and the beginning of your if and for ... in blocks. To provide a more complete example, I included some assertions of what the variables and the beginning if and foreach statements would look like.

To convert the Twig template you supplied to PHP, it would look similar to the below.

<?php
/* START ASSERTIONS */

$iterable = [
    ['status' => true],
];
$test_enabled = 'Enabled';
$text_disabled = 'Disabled';
$button_remove = 'Remove';

if (!empty($iterable)) {
    $list_row = 0;
    foreach ($iterable as $i) { 
?>
<tr>
<?php /* END ASSERTIONS */ ?>

<td class="text-left">
    <select name="product[<?php echo htmlspecialchars($list_row, ENT_QUOTE, 'UTF-8'); ?>][status]" id="input-status" class="form-control">
    <?php if (array_key_exists('status', $i) ? $i['status'] : null) { ?>
        <option value="1" selected="selected"><?php echo htmlspecialchars($text_enabled, ENT_QUOTE, 'UTF-8'); ?></option>
        <option value="0"><?php echo htmlspecialchars($text_disabled, ENT_QUOTE, 'UTF-8'); ?></option>
    <?php } else { ?>
        <option value="1"><?php echo htmlspecialchars($text_enabled, ENT_QUOTE, 'UTF-8'); ?></option>
        <option value="0" selected="selected"><?php echo htmlspecialchars($text_disabled, ENT_QUOTE, 'UTF-8'); ?></option>
    <?php } ?>
    </select>
</td>
<td class="text-left">
    <button id="button_cancel" type="button" onclick="$('#product_row_<?php echo htmlspecialchars($list_row, ENT_QUOTE, 'UTF-8'); ?>').remove()" data-toggle="tooltip" title="<?php echo htmlspecialchars($button_remove, ENT_QUOTE, 'UTF-8'); ?>" class="btn btn-danger">
        <i class="fa fa-minus-circle"></i>
    </button>
</td> 
</tr>
<?php 
    $list_row = $list_row + 1;
    } /* endfor */
} /* endif */
?> 

Upvotes: 2

Related Questions