View previous topic :: View next topic |
Author |
Message |
kid Smarty Rookie
Joined: 04 Jul 2004 Posts: 13
|
Posted: Mon Aug 23, 2004 2:33 pm Post subject: Optional section Element, sonst nur eins |
|
|
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 |
|
Ranger Smarty n00b
Joined: 25 Aug 2004 Posts: 1
|
Posted: Wed Aug 25, 2004 2:09 pm Post subject: Re: Optional section Element, sonst nur eins |
|
|
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 |
|
kid Smarty Rookie
Joined: 04 Jul 2004 Posts: 13
|
Posted: Wed Aug 25, 2004 6:15 pm Post subject: |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Wed Aug 25, 2004 6:22 pm Post subject: |
|
|
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 |
|
kid Smarty Rookie
Joined: 04 Jul 2004 Posts: 13
|
Posted: Thu Aug 26, 2004 4:13 pm Post subject: |
|
|
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 |
|
daniel987 Smarty Rookie
Joined: 26 Aug 2004 Posts: 20
|
Posted: Sun Aug 29, 2004 7:08 pm Post subject: |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Mon Aug 30, 2004 3:06 pm Post subject: |
|
|
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 |
|
kid Smarty Rookie
Joined: 04 Jul 2004 Posts: 13
|
Posted: Wed Sep 01, 2004 6:29 pm Post subject: |
|
|
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 |
|
Back to top |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Thu Sep 02, 2004 6:11 am Post subject: |
|
|
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 |
|
kid Smarty Rookie
Joined: 04 Jul 2004 Posts: 13
|
Posted: Thu Sep 02, 2004 6:48 pm Post subject: |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Fri Sep 03, 2004 7:49 am Post subject: |
|
|
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 |
|
|