Jump to content

SMARTY foreach tableau associatif


Recommended Posts

Bonjour à tous,

 

Je débute avec SMARTY et je voudrais afficher une variables assignée à mon template product-list.tpl.

je sais qu'on affiche une variable contenue dans un tableau associatif de la manière suivante :

{$products[0].features[1].value}

En revanche,

J'ai tenté de faire un foreach mais je ne m'en sors pas !

 

Voici ce que j'ai tenté :

{foreach from=$products item=features name=LocPriceproducts}
{$LocPriceproducts}
{/foreach}

Malheureusement, cela ne fonctionne pas...

 

Si quelqu'un pouvait m'aiguiller, j'en serais très reconnaissant !!!

 

PS : Voir Attach Files pour consulter le tableau de ma variable ! la valeur entourée en rouge est celle que je voudrais pouvoir obtenir à l'aide de la fonction foreach !

 

Merci d'avance

 

Olivier

 

post-774244-0-61107600-1396439982_thumb.png

Link to comment
Share on other sites

Informations supplémentaires...

 

voici mon product-list.tpl dans lequel il y a déjà un foreach et je sais qu'on ne peut pas imbriquer un foreach dans un autre...

{if isset($products)}
	<!-- Products list -->
	<ul id="product_list" class="clear">
	{foreach from=$products item=product name=products}
		<li class="ajax_block_product {if $smarty.foreach.products.first}first_item{elseif $smarty.foreach.products.last}last_item{/if} {if $smarty.foreach.products.index % 2}alternate_item{else}item{/if} clearfix">		  
        <div class="product_container">
			<div class="center_block">        
				<a href="{$product.link|escape:'htmlall':'UTF-8'}" class="product_img_link product_image" title="{$product.name|escape:'htmlall':'UTF-8'}">
					<img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'large_default')}" alt="{$product.legend|escape:'htmlall':'UTF-8'}" class="image_of_prod" />     
				</a>
				<h3>
            <a href="{$product.link|escape:'htmlall':'UTF-8'}">{$product.name|escape:'htmlall':'UTF-8'|truncate:35:'...'}</a>
            {if isset($product.new) && $product.new == 1}
            <span class="new">{l s='New'}</span>
            {/if}
        </h3>
				<p class="product_desc">
        	{$product.description_short|strip_tags:'UTF-8'|truncate:360:'...'}
        </p>
        <div class="clear"></div>
        {if isset($comparator_max_item) && $comparator_max_item}
          <p class="compare">
            <input type="checkbox" class="comparator" id="comparator_item_{$product.id_product}" value="comparator_item_{$product.id_product}" {if isset($compareProducts) && in_array($product.id_product, $compareProducts)}checked="checked"{/if} /> 
            <label for="comparator_item_{$product.id_product}">{l s='Select to compare'}</label>
          </p>
        {/if}
			</div><!-- .center_block -->
            
			<div class="right_block">        
				{if isset($product.on_sale) && $product.on_sale && isset($product.show_price) && $product.show_price && !$PS_CATALOG_MODE}
          <span class="on_sale">{l s='On sale!'}</span>
				{elseif isset($product.reduction) && $product.reduction && isset($product.show_price) && $product.show_price && !$PS_CATALOG_MODE}
          <div class="discount_block">
              <span class="discount">{l s='Reduced price!'}</span>
          </div>
          <div class="clear"></div>
          {/if}
				{if (!$PS_CATALOG_MODE AND ((isset($product.show_price) && $product.show_price) || (isset($product.available_for_order) && $product.available_for_order)))}
				<div class="content_price">
					{if isset($product.show_price) && $product.show_price && !isset($restricted_country_mode)}
              <span class="price" style="display: inline;">
                {if !$priceDisplay}{convertPrice price=$product.price}{else}{convertPrice price=$product.price_tax_exc}{/if}
              </span><br />
                prix à la journée
              {/if}
					     {if isset($product.available_for_order) && $product.available_for_order && !isset($restricted_country_mode)}
              <span class="availability">
              	{if ($product.allow_oosp || $product.quantity > 0)}
                  	{l s='Available'}
                  {elseif (isset($product.quantity_all_versions) && $product.quantity_all_versions > 0)}
                  	{l s='Product available with different options'}
                      {else}
                      {l s='Out of stock'}
                  {/if}
              </span>
          {/if}
				</div>
				{if isset($product.online_only) && $product.online_only}
            <span class="online_only">{l s='Online only!'}</span>
            {/if}
				{/if}
				{if ($product.id_product_attribute == 0 || (isset($add_prod_display) && ($add_prod_display == 1))) && $product.available_for_order && !isset($restricted_country_mode) && $product.minimal_quantity <= 1 && $product.customizable != 2 && !$PS_CATALOG_MODE}
                
      					{if ($product.allow_oosp || $product.quantity > 0)}
      						{if isset($static_token)}
                    <a class="button" href="http://www.studio54.be/index.php?controller=contact"><span></span>{l s='Add to cart'}</a>
                  {/if} 		
      					{else}
      						<span class="exclusive">{l s='Out of stock'}</span><br />
      					{/if}
                  
				        {/if}
            </div>
        </div>
		</li>
	{/foreach}
</ul>
<script>
$(document).ready(function(){
  $.ajax({
    type: 'POST',
    url: baseDir+'modules/themesettings/ajax.php',
    data: 'id={foreach from=$products item=p name=prd}{$p['id_product']}{if $smarty.foreach.prd.last}{else},{/if}{/foreach}&customer={$cookie->id_customer}',
    success: function(result){
      if (result == '0')
      {
        console.log("no data")
      } else {                          
        var pData = JSON.parse ( result );        
        $.each(pData, function (id) {
          if (pData[id].isFavorite == 1) {
          $("#favprod_{$product['id_product']} #favprod_block_extra_add").removeClass("display").addClass("hide");
            $("#favprod_{$product['id_product']} #favprod_block_extra_remove").removeClass("hide").addClass("display");
          }
          if (pData[id].grade > 0) {
            $(".rating_{$product['id_product']}").addClass("active_state").find("i").text(pData[id].grade);
          }
          if (pData[id].nbr > 0) {
            $(".comment_number_{$product['id_product']}").addClass("active_state").find("span").text(pData[id].nbr);
          }
        });        
      }
    }
  });
});
</script>
<div class="clear"></div><!-- /Products list -->
{/if}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...