Page 1 of 1

twig to php syntax

Posted: Mon Jan 13, 2020 4:13 pm
by randompeople10

Code: Select all

                            {% if categories %}
                                {% for i in categories %}
                                    <option value="{{ i.category_id }}">{{ i.name }}</option>
                                {% endfor %}
                            {% endif %}

Code: Select all

                                            <td class="text-left">
                                                {% if sort == 'pd.name' %}
                                                    <a class="{{ order|lower }}">{{ column_name }}</a>
                                                {% else %}
                                                    <a>{{ column_name }}</a>
                                                {% endif %}
                                            </td>
Any idea that how do i convert the above code to php syntax ? have trouble on converting this code and can't find any explaintion on the web :choke:

Re: twig to php syntax

Posted: Mon Jan 13, 2020 4:29 pm
by uksitebuilder

Code: Select all

<?php if($categories){ ?>
    <?php foreach ($categories as $category){ ?>
        <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
    <?php } ?>
<?php } ?>

Code: Select all

<td class="text-left">
    <?php if($sort == 'pd.name'){ ?>
        <a class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
    <?php } else { ?>
        <a><?php echo $column_name; ?></a>
    <?php } ?>
</td>

Re: twig to php syntax

Posted: Mon Jan 13, 2020 4:50 pm
by randompeople10
uksitebuilder wrote:
Mon Jan 13, 2020 4:29 pm

Code: Select all

<?php if($categories){ ?>
    <?php foreach ($categories as $category){ ?>
        <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
    <?php } ?>
<?php } ?>

Code: Select all

<td class="text-left">
    <?php if($sort == 'pd.name'){ ?>
        <a class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
    <?php } else { ?>
        <a><?php echo $column_name; ?></a>
    <?php } ?>
</td>
Thanks

for this

Code: Select all

                                                        {% if i.image %}
                                                            <img src="{{ i.image }}" alt="{{ i.image }}" class="img-thumbnail" />
                                                        {% else %}
                                                            <span class="img-thumbnail list"><i class="fa fa-camera fa-2x"></i></span>
                                                        {% endif %}
after conversion it should turn look like this ?

Code: Select all

<?php if($special){ ?>
  <?php foreach ($special as $specials){ ?>
	<?php if($specials['image']){ ?>
            <img src="<?php $specials['image']?>" alt="<?php echo $specials['image']?>" class="img-thumbnail" />
	else {
	<span class="img-thumbnail list"><i class="fa fa-camera fa-2x"></i></span>
}
<?php } ?>
and how about this ?

Code: Select all

                                                    <td class="text-left">
                                                        {{ i.product_name }}
                                                        <input type="hidden" name="product[{{ list_row }}][product_id]" value="{{ i.product_id }}" data-type="product_id" class="form-control" />
                                                    </td>
everything look weird

Re: twig to php syntax

Posted: Mon Jan 13, 2020 4:58 pm
by xxvirusxx
randompeople10 wrote:
Mon Jan 13, 2020 4:50 pm
and how about this ?

Code: Select all

                                                    <td class="text-left">
                                                        {{ i.product_name }}
                                                        <input type="hidden" name="product[{{ list_row }}][product_id]" value="{{ i.product_id }}" data-type="product_id" class="form-control" />
                                                    </td>
everything look weird

Code: Select all

 <td class="text-left"><?php echo $i['product_name']; ?>
    <input type="hidden" name="product[<?php echo $list_row ; ?>][product_id]" value="<?php echo $i['product_id']; ?>" data-type="product_id" class="form-control" />
   </td>
randompeople10 wrote:
Mon Jan 13, 2020 4:50 pm

Code: Select all

                                                        {% if i.image %}
                                                            <img src="{{ i.image }}" alt="{{ i.image }}" class="img-thumbnail" />
                                                        {% else %}
                                                            <span class="img-thumbnail list"><i class="fa fa-camera fa-2x"></i></span>
                                                        {% endif %}

Code: Select all

<?php if ($i['image']) { ?>
      <img src="<?php echo $i['image']; ?>" alt="<?php echo $i['image']; ?>" class="img-thumbnail" />
<?php } else { ?>
    <span class="img-thumbnail list"><i class="fa fa-camera fa-2x"></i></span>
<?php } ?>

What you want to convert?

Re: twig to php syntax

Posted: Tue Jan 14, 2020 2:08 am
by letxobnav
Why do you guys keep on opening and closing php with multiple echo statements?

Code: Select all

<?php if ($i['image']) { ?>
      <img src="<?php echo $i['image']; ?>" alt="<?php echo $i['image']; ?>" class="img-thumbnail" />
<?php } else { ?>
    <span class="img-thumbnail list"><i class="fa fa-camera fa-2x"></i></span>
<?php } ?>
Just echo once.

Code: Select all

<?php 
if ($i['image']) {
   echo '<img src="'.$i['image'].'" alt="'.$i['image'].'" class="img-thumbnail" />';
} else {
   echo '<span class="img-thumbnail list"><i class="fa fa-camera fa-2x"></i></span>';
}
?>

Re: twig to php syntax

Posted: Tue Jan 14, 2020 9:50 am
by randompeople10
Thanks , the page is working right now after some modify

Code: Select all

    
 <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 %}
Can anyone explain on the the last 2 code ? (SET and OPTION VALUE)
the rest able to modify myself.

Re: twig to php syntax

Posted: Tue Jan 14, 2020 10:05 am
by by mona
option value is the value of the option stored in the database as an integer
and set is setting the next row value as + 1 more

https://www.w3schools.com/tags/tag_option.asp
https://twig.symfony.com/doc/3.x/templates.html

Re: twig to php syntax

Posted: Tue Jan 14, 2020 5:57 pm
by paulfeakins
randompeople10 wrote:
Mon Jan 13, 2020 4:13 pm
Any idea that how do i convert the above code to php syntax ? have trouble on converting this code and can't find any explaintion on the web :choke:
Why not just use Twig?

Re: twig to php syntax

Posted: Wed Jan 15, 2020 1:34 am
by xxvirusxx
letxobnav wrote:
Tue Jan 14, 2020 2:08 am
Why do you guys keep on opening and closing php with multiple echo statements?
We respect opencart code structure :D
randompeople10 wrote:
Tue Jan 14, 2020 9:50 am
Can anyone explain on the the last 2 code ? (SET and OPTION VALUE)

Code: Select all

<?php $list_row  = 0; ?>
<?php $list_row++; ?>

Re: twig to php syntax

Posted: Wed Jan 15, 2020 3:09 am
by letxobnav
"opencart" and "structure", those two words don't mix.

More difficult to read and creating a lot of white-space.

Re: twig to php syntax

Posted: Wed Jan 15, 2020 5:16 pm
by paulfeakins
letxobnav wrote:
Wed Jan 15, 2020 3:09 am
"opencart" and "structure", those two words don't mix.

More difficult to read and creating a lot of white-space.
I'd strongly disagree, one of the reasons we chose OpenCart originally was that the code is well-written and clean.