I have a question regarding smarty templates, I'm using PHP and Smarty in order to generate pages. I would like to somehow count or determine first and last row of table. So I can set different CSS for first and last TR or leave it as it is, if it's only 1 TR.
Hope it makes sense what im after. Thanks in advance.
<table class="table-paddings-2" cellspacing="0" cellpadding="0">
<tr>
<th class="table-gray-th-noborder">Search Type</th>
<th class="table-gray-th-noborder">Average Turnaround Time</th>
</tr>
{foreach key=obk item=row from=$report}
<tr>
<td class="td-border-top-only text-align-left">{$row.SearchName}</td>
<td class="td-border-top-only text-align-left">{$row.tt}</td>
</tr>
{/foreach}
</table>
Documentation is your friend: http://www.smarty.net/docsv2/en/language.function.foreach.tpl
Examples 7.12, 7.13:
{* show LATEST on the first item, otherwise the id *}
<table>
{foreach from=$items key=myId item=i name=foo}
<tr>
<td>{if $smarty.foreach.foo.first}LATEST{else}{$myId}{/if}</td>
<td>{$i.label}</td>
</tr>
{/foreach}
</table>
Related
I have table generated by mustache loop that looks like this:
names.mustache
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>
#
</th>
<th>
Name
</th>
</tr>
</thead>
<tbody>
{{#allnames}}
{{#active}}
<tr>
<td>
{{count}}
</td>
<td>
{{name}}
</td>
</tr>
{{/active}}
{{/allnames}}
</tbody>
</table>
And I want to iterate count so that my table can have row numbers. basically, i initialized $count=1. How can I implement that with clean efficient code? or is there a better simpler way?
UPDATED
For Mustache, you need to create a function
var data = {
items: allnames,
, index: function() {
return ++window['index']||(window['index']=0);
}
}
and then use {{index}} inside the loop
originally
Sorry, I was thinking of handlebars which works as below (see correct answer above)
Use {{#index}}
{{#allnames}}
{{name}} is {{#index}}
{{/allnames}}
Note: index starts from zero
I've been working on this all day and have tried every possible solution I could find but nothing seems to be working. Here's a copy of my code (using Smarty template system):
<tr><td style="border-top: none;">
<table>
<tr><th colspan="7">TITLE OF THE TABLE</th></tr>
{foreach from=$data.history item=tmp name=item}
<tr><td colspan="1" class='{cycle name=color values="odd,even"}'>{$tmp->history}</td></tr>
{/foreach}
</table>
</td><tr>
If you looks in the foreach statement you'll see that I'm trying to create a new td for each piece of information. This is actually working. However, the problem is that it's not spanning them in the table like I want it to do.
I want each td to have its own column and for there to be a maximum of 7 columns. As of right now, there's only one column showing and every is just split between rows.
Ideally, I want there to be 7 columns per row, even if some of them have to be empty because there's not enough data to pull. Is there any way to do this?
I think you are missing a final </tr>
<tr><td style="border-top: none;">
<table>
<tr><th colspan="7">TITLE OF THE TABLE</th></tr>
{foreach from=$data.history item=tmp name=item}
<tr><td colspan="1" class='{cycle name=color values="odd,even"}'>{$tmp->history}</td></tr>
{/foreach}
</table>
</td><tr>
^^^ ------------> should be </tr>
Hi I'm making this HTML email and thanks to the good old Outlook 07 that adds a really nice page break I kinda have to change my logic. Currently my html looks something like this:
<table>
<tr> //I want for every 6th element to create a new <tr>
<td>
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{/foreach}
</table>
</td>
</tr>
</table>
Right now after the 6th element it creates this page break so I want is when I have let's say 12 elements each six to have their own table row with table inside.
The following code should work for you:
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration mod 6 eq 1}
<tr>
<td>
<table>
{/if}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{if $smarty.foreach.elements.iteration mod 6 eq 0 or $smarty.foreach.elements.last}
</table>
</td>
</tr>
{/if}
{/foreach}
</table>
but because of many tables I cannot check it for sure. You could also look at Break UL tag after 'n' times in while-loop where I answered how to do the same in PHP.
In my Wordpress site (Twenty Thirteen theme), I tried to get content from outside and output the data to HTML table (so I can use the jQuery tablesorter for arrangement). However, I found that I would lose the HTML tag in Wordpress?!
Here is my testing code in php :
<?php
$result = '<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Due</th>
<th>Web Site</th>
</tr>
</thead>
<tbody>
<tr class="even">
<td>Smith</td>
<td>John</td>
<td>jsmith#gmail.com</td>
<td>$50.00</td>
<td>http://www.jsmith.com</td>
</tr>
<tr class="odd">
<td>Bach</td>
<td>Frank</td>
<td>fbach#yahoo.com</td>
<td>$50.00</td>
<td>http://www.frank.com</td>
</tr>
<tr class="even">
<td>Doe</td>
<td>Jason</td>
<td>jdoe#hotmail.com</td>
<td>$100.00</td>
<td>http://www.jdoe.com</td>
</tr>
<tr class="odd">
<td>Conway</td>
<td>Tim</td>
<td>tconway#earthlink.net</td>
<td>$50.00</td>
<td>http://www.timconway.com</td>
</tr>
</tbody>
</table> ';
echo $result;
?>
This code is fine in other place. In Wordpress, what I got is
Last Name
First Name
Email
Due
Web Site
Smith
John
jsmith#gmail.com
$50.00
http://www.jsmith.com
Bach
Frank
fbach#yahoo.com
$50.00
http://www.frank.com
Doe
Jason
jdoe#hotmail.com
$100.00
http://www.jdoe.com
Conway
Tim
tconway#earthlink.net
$50.00
http://www.timconway.com
It seems like I lost all the tags. I tried to use htmlentities but the result is the same. Can someone tell me how to generate table correctly? Thanks!!
I'm is using PHP smarty templater. I need to create even odd row highlighting.
Please, send me example how to do that.
also I have variable:
$smarty.foreach.product.index
For this kind of situation smarty has method called {cycle}
<table>
{foreach $products as $product}
<tr class="{cycle values="odd,even"}">
<td>{$product.name}</td>
</tr>
{/foreach}
</table>
The result for this will be:
<table>
<tr class="odd">
<td>1st product</td>
</tr>
<tr class="even">
<td>2nd product</td>
</tr>
<tr class="odd">
<td>3rd product</td>
</tr>
</table>
In you stylesheet file define properties for odd and even lines like this:
tr.even td{background: #CCCCCC;}
tr.odd td{background: #EFEFEF;}
<table>
{foreach key=i item=row from=$items}
<tr{if $i%2==1} bgcolor=#e4e4e4{/if}><td>{$i}</td></tr>
{/foreach}
</table>
{section name=myloop start=0 loop=10 step=1}
<tr class="{if $smarty.section.myloop.index is even}tr_even{else}tr_odd{/if}"><td>{$smarty.section.myloop.index}</td></tr>
{/section}