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

Optional section Element, sonst nur eins

 
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: German
View previous topic :: View next topic  
Author Message
kid
Smarty Rookie


Joined: 04 Jul 2004
Posts: 13

PostPosted: Mon Aug 23, 2004 2:33 pm    Post subject: Optional section Element, sonst nur eins Reply with quote

Hallo,

anstatt ein Template x mal einzubinden in einer Schleife
hab ich die Schleife ins include-Template ausgelagert.
Also folgender Teil ist ein Template, was includet wird:

Code:

{if $produktArray}
  {section name=element loop=$produkte}
  {assign var="produkt" value=$produkte[element]}
{/if}
{* Einzelnes Produkt *}
{if $produktTabelle}
  {/section}
{/if}


Der Hintergedanke ist, dass ich sowohl eine Schleife
ausgeben kann mit vielen von {* Einzelnes Produkt *},
als auch nur ein {* Einzelnes Produkt *}.

Leider mäckert Smarty, weil section nicht nur zur Hälfte
in einem if-Teil sein darf. Gibt es einen eleganten Weg
das anders zu lösen, ohne 2 Templates zu machen?

Ich hoffe ich habs brauchbar erklärt.

Viele Grüße,

Alexander
Back to top
View user's profile Send private message AIM Address MSN Messenger
Ranger
Smarty n00b


Joined: 25 Aug 2004
Posts: 1

PostPosted: Wed Aug 25, 2004 2:09 pm    Post subject: Re: Optional section Element, sonst nur eins Reply with quote

Der 2. Teil ist doch völlig überflüssig, m.E. müsste dein Code so aussehen:

Code:

{if $produktArray}
  {section name=element loop=$produkte}
  {assign var="produkt" value=$produkte[element]}
  {/section}
{else}
{* Einzelnes Produkt *}
{/if}
Back to top
View user's profile Send private message
kid
Smarty Rookie


Joined: 04 Jul 2004
Posts: 13

PostPosted: Wed Aug 25, 2004 6:15 pm    Post subject: Reply with quote

Hmm, ne. Ich hatte das so gesplittet, um nicht alles
Doppelt zu designen. Wenn ich das so wie vorgeschlagen
machen würde, wär es so:

Code:
{if $produktArray}
  {section name=element loop=$produkte}
   <table ...>
   <tr><td>
  {assign var="produkt" value=$produkte[element]}
   {* Einzelnes Produkt *}
   <table ...>
   <tr><td>{$produkt.id}</td><td>{$produkt.name}</td></tr>
   </table>
  {/section}
   ...
   </td></tr>
   </table>
{else}
   {* Einzelnes Produkt *}
   <table ...>
   <tr><td>{$produkt.id}</td><td>{$produkt.name}</td></tr>
   </table>
{/if}


Also die beiden Template Teile sind in dem Fall identisch.
Also ist keine grosse Sache, aber wäre halt schön, wenns
zusammenfassbar wär.
Back to top
View user's profile Send private message AIM Address MSN Messenger
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Wed Aug 25, 2004 6:22 pm    Post subject: Reply with quote

dann extrahiere den doppelten teil doch in ein 2. TPL:

Code:

{if $produktArray}
  {section name=element loop=$produkte}
   <table ...>
   <tr><td>
  {assign var="produkt" value=$produkte[element]}
   {* Einzelnes Produkt *}
   {include file="produkt.tpl"}
  {/section}
   ...
   </td></tr>
   </table>
{else}
   {* Einzelnes Produkt *}
   {include file="produkt.tpl"}
{/if}


Code:

   <table ...>
   <tr><td>{$produkt.id}</td><td>{$produkt.name}</td></tr>
   </table>
Back to top
View user's profile Send private message
kid
Smarty Rookie


Joined: 04 Jul 2004
Posts: 13

PostPosted: Thu Aug 26, 2004 4:13 pm    Post subject: Reply with quote

Hallo,

dann hab ich wieder warum ich überhaupt die Schleife ausgelagert hab - das extra Templete wird mehrfach eingebunden und bremst.

Naja, dann lkass ich das wohl mit 2 x Template im Template.

Gruß, Alex
Back to top
View user's profile Send private message AIM Address MSN Messenger
daniel987
Smarty Rookie


Joined: 26 Aug 2004
Posts: 20

PostPosted: Sun Aug 29, 2004 7:08 pm    Post subject: Reply with quote

Ich habe zurzeit dasselbe Probelm allerdings möcht ich mein tpl aus eine Datenbank holen wie gehe ich vor?
_________________
It's better to burn out than to fade away.
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Mon Aug 30, 2004 3:06 pm    Post subject: Reply with quote

man hätte es auch ganz einfach so machen können:

TPL:
Code:

{section name=element loop=$produkte}
  { $produkte[element] }
{/section}


Wenn dein Array nur ein Produkt enhält, wird deine schleife nur einmal durchlaufen, ansonsten so oft wie elemente da sind...
Back to top
View user's profile Send private message
kid
Smarty Rookie


Joined: 04 Jul 2004
Posts: 13

PostPosted: Wed Sep 01, 2004 6:29 pm    Post subject: Reply with quote

Ich brauch aber bei nur einem Element nicht die Tabelle drumherum. Wenn ich die noch Elemtabhängig mach, hab ich nen Salat, den kein Template Designer versteht Smile
Back to top
View user's profile Send private message AIM Address MSN Messenger
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Thu Sep 02, 2004 6:11 am    Post subject: Reply with quote

hm könnte auch so gehen:
siehe
http://smarty.php.net/manual/en/language.function.section.php

Code:

{ if ( $smarty.section.element.loop == 1) }
<table>
{ /if }

{section name=element loop=$produkte}
  { $produkte[element] }
{/section}

{ if ( $smarty.section.element.loop == 1) }
</table>
{ /if }
Back to top
View user's profile Send private message
kid
Smarty Rookie


Joined: 04 Jul 2004
Posts: 13

PostPosted: Thu Sep 02, 2004 6:48 pm    Post subject: Reply with quote

Jep, da hatte ich auch dran gedacht - das meinte ich mit Salat, denn die if's
muss man dann auch noch bei den td's dazu packen und auch noch bei den
tr's - dann lieber einmal copy & paste.
Back to top
View user's profile Send private message AIM Address MSN Messenger
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Fri Sep 03, 2004 7:49 am    Post subject: Reply with quote

Code:

{ foreach name="produkt_loop" from=$produkt item="akt_produkt"}
   { if $smarty.foreach.produkt_loop.first }
      <table>
   { /if }
         <tr>
            <td>
              { include file="produkt.tpl" produkt=$akt_produkt }
            </td>
         </tr>
   { if $smarty.foreach.produkt_loop.first }
      </table>
   { /if }
{ foreachelse }
   { include file="produkt.tpl" produkt=$produkt }
{ /foreach }


produkt.tpl
Code:

   {* Einzelnes Produkt *}
   <table>
      <tr>
         <td>{$produkt.id}</td>
         <td>{$produkt.name}</td>
      </tr>
   </table>


so?
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: German 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