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

Display on array

 
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: Russian
View previous topic :: View next topic  
Author Message
ggyyvv
Smarty n00b


Joined: 02 Feb 2015
Posts: 3

PostPosted: Mon Feb 02, 2015 10:48 am    Post subject: Display on array Reply with quote

Привет всем!

Столкнулся с проблемой вывода массива через запятую
вот такой код
Code:
{foreach name=fields key=key2 item=item2 from=$item1}
   {$item2}
      {if $smarty.foreach.fields.last}
      ,
      {else}
      {/if}
{/foreach}


на выходе получается следующее
Code:
1 , 2 , 3 ...


а хотелось бы без пробелов перед запятой, вот так
Code:
1, 2, 3 ...


смотрел разные функции, кажется foreach для вывода самая лучшая, учитывая, что нужно еще и iteration использовать, но нормальный вывод с запятыми никак не получается...
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Feb 02, 2015 4:07 pm    Post subject: Reply with quote

У тебя не пробелы, а переводы строки.
Убери их в шаблоне, и получишь то, что хочешь получить.
Back to top
View user's profile Send private message
ggyyvv
Smarty n00b


Joined: 02 Feb 2015
Posts: 3

PostPosted: Mon Feb 02, 2015 4:56 pm    Post subject: Reply with quote

AnrDaemon wrote:
У тебя не пробелы, а переводы строки.
Убери их в шаблоне, и получишь то, что хочешь получить.


сразу не понял, где они у меня, спасибо за подсказку
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Feb 02, 2015 8:38 pm    Post subject: Reply with quote

Поэтому сначала всегда смотрят исходный код сгенерённого вывода.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Feb 03, 2015 6:05 pm    Post subject: Reply with quote

Ещё добавлю, что если вы ВОТ ТАК СИЛЬНО хотите сделать перенос в шаблоне, который не будет видно на выводе, есть минимум два способа.
1. Сделать перенос внутри тега.
Code:
$template = <<<'SMARTY_TEMPLATE'
{foreach $item1 as $key2 => $item2}{$item2}{if
  $item2@last},{/if} {/foreach}
SMARTY_TEMPLATE;

$tpl = new Smarty();
$tpl->assign('item1', array(1, 2, 3, 4, 5, 6));
$tpl->display("string:$template");

2. Сделать перенос внутри комментария.
Code:
$template = <<<'SMARTY_TEMPLATE'
{foreach $item1 as $key2 => $item2}{*
  *}{$item2}{if $item2@last},{/if} {/foreach}
SMARTY_TEMPLATE;

$tpl = new Smarty();
$tpl->assign('item1', array(1, 2, 3, 4, 5, 6));
$tpl->display("string:$template");


P.S.
И кончайте уже юзать Smarty2 синтаксис. Голову сломать можно, пока разберёшься, откуда что берётся.
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: Russian 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