Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Afficher du texte codé dans un foreach en php coté template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French
View previous topic :: View next topic  
Author Message
emulienfou
Smarty Rookie


Joined: 28 May 2010
Posts: 18

PostPosted: Fri May 28, 2010 3:15 pm    Post subject: Afficher du texte codé dans un foreach en php coté template Reply with quote

Bonjour, j'ai un foreach dans mon fichier php qui va lister des dossiers et vérifier s'ils sont en chmod 777 et j'affiche le résultat dans mon fichier TPL. le probème et que sa m'affiche uniquement le dernier dossier scanner.

Dois-je recréer mon foreach dans le TPl et si oui comment ?

Ou sinon comment dois-je faire ?

Voici mon code du foreach en PHP :
Code:
$important_files = array(
      '../backup/',
      '../engine/data/',
      '../engine/cache/',
      '../engine/cache/system/',
      '../uploads/',
      '../uploads/files/',
      '../uploads/fotos/',
      '../uploads/posts/',
      '../uploads/posts/thumbs/',
      '../uploads/thumbs/',
      '../templates/',
      '../templates/Default/',
   );

foreach ($important_files as $dir) {
       $is_writable = $is_dir = true;
       //If the file exists and is a directory
       if (file_exists($dir) && is_dir($dir)) {
          //Si il n'est pas inscriptible, on demande à Apache de le rendre inscriptible en espérant qu'il soit configurer pour accepter de telles requêtes
          if (!is_writable($dir)) {
             $is_writable = (@chmod($dir, 0777)) ? true : false;
          }
       }
       else {
          $is_dir = $is_writable = ($fp = @mkdir($dir, 0777)) ? true : false;
       }
             
       $tpl->assign('chmod', array(
          'TITLE'   => str_replace('..' , '', $dir),
          'C_EXISTING_DIR' => $is_dir,
          'C_WRITIBLE_DIR' => $is_writable
       ));
          
       if ($all_dirs_ok && (!$is_dir || !$is_writable)) {
          $all_dirs_ok = false;
      }
    }



Code du fichier TPL :
Code:
{foreach from=$important_files item=dir}
                           <dl>
                              <dt><label>{$chmod.TITLE}</label></dt>
                              <dd>
                                 {if $chmod.C_EXISTING_DIR}
                                    <div class="success_block">{$L_EXISTING}</div>
                                 {else}
                                    <div class="failure_block">{$L_NOT_EXISTING}</div>
                                 {/if}
                                 {if $chmod.C_WRITIBLE_DIR}
                                    <div class="success_block">{$L_WRITABLE}</div>
                                 {else}
                                    <div class="failure_block">{$L_NOT_WRITABLE}</div>
                                 {/if}
                              </dd>                        
                           </dl>
                           {/foreach}



Cordialement
Back to top
View user's profile Send private message
Gowser
Smarty Pro


Joined: 19 Feb 2008
Posts: 104
Location: Nantes (France)

PostPosted: Wed Jun 02, 2010 2:57 pm    Post subject: Reply with quote

Bonjour,

A chaque fois que vous effectué un

Code:
 $tpl->assign('chmod', array(
          'TITLE'   => str_replace('..' , '', $dir),
          'C_EXISTING_DIR' => $is_dir,
          'C_WRITIBLE_DIR' => $is_writable
       ));


vous écrasez les anciennes valeurs donc c'est normal que seule les dernière soient affichées

Vous devriez créer d'abord un tableau contenant tous les résultats puis exploiter ce tableau dans smarty :

Code:
foreach ($important_files as $dir) {
   ...
   ...
   /*
   // ancienne façon
    $tpl->assign('chmod', array(
      'TITLE'   => str_replace('..' , '', $dir),
      'C_EXISTING_DIR' => $is_dir,
      'C_WRITIBLE_DIR' => $is_writable
   ));
   */
   // nouvelle façon
   $chmodListe[] = array(
      'TITLE'   => str_replace('..' , '', $dir),
      'C_EXISTING_DIR' => $is_dir,
      'C_WRITIBLE_DIR' => $is_writable
   );
   ...
   ...
}
$tpl->assign('chmodListe', $chmodListe)


puis dans le tpl exlpoiter la variable 'chmodListe'

Code:
{foreach from=$chmodListe item=dir}
{/foreach}
Back to top
View user's profile Send private message Send e-mail
emulienfou
Smarty Rookie


Joined: 28 May 2010
Posts: 18

PostPosted: Thu Jun 03, 2010 8:12 am    Post subject: Reply with quote

Merci, cela fonctionne à la perfection !!!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP