|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jun 24, 2004 7:40 pm Post subject: split_array |
|
|
This is a plugin for splitting an array in to proportional parts.
The need came about when I had an array, such as:
Code: | $smarty->assign('colors', array('red','blue','green','yellow','purple','black','brown','violet')); |
and I needed to display this data in the template in three columns, such as:
Code: | red yellow brown
blue purple violet
green black |
It also takes care to put the remainder in the leftmost columns, even though it is going from top to bottom in each column. It also makes empty arrays for any extra columns, so you don' t have to do special tests for that in the section loops. {html_table} didn't work because I need to wrap each value in an HREF tag. So this little plugin will split an array up in to chunks so you can loop over them in a {section} loop and display the values in nice looking columns.
You might name it "columnize" or something more understandable to a template designer rather than "split_array", so take your choice.
Full Template Example:
Code: | <table border="0" width="100%">
<tr>
{split_array var=$colors num=3 assign="sp_colors"}
{section name=outer loop=$sp_colors}
<td>
{section name=inner loop=$sp_colors[outer]}
<a href="...">{$sp_colors[outer][inner]}</a>
{if not $smarty.section.inner.last}<br />{/if}
{/section}
</td>
{/section}
</tr>
</table> |
and here is the plugin source:
Code: | <?php
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: split_array
* Version: 1.0
* Date: July 24, 2004
* Author: Monte Ohrt <monte@ispi.net>
* Purpose: split an array into equal proportioned arrays
* Input: var = array to split
* num = number of ways (columns) to split
* assign = template var to assign result to
*
* Examples: {split_array var=$foobar num=4 assign="foobar"}
* -------------------------------------------------------------
*/
function smarty_function_split_array($params, &$smarty)
{
if(!isset($params['var'])) {
$smarty->trigger_error("split_array: missing 'var' parameter");
return;
}
if(!isset($params['num'])) {
$smarty->trigger_error("split_array: missing 'num' parameter");
return;
}
if(!isset($params['assign'])) {
$smarty->trigger_error("split_array: missing 'assign' parameter");
return;
}
if(!is_array($params['var'])) {
$smarty->trigger_error("split_array: expecting 'var' as an array");
return;
}
$_num = (int) $params['num'];
$_count = count($params['var']);
if($_count <= $_num ) {
for($_x = 0; $_x < $_count; $_x++)
$_split_array[$_x] = isset($params['var'][$_x]) ? array($params['var'][$_x]) : array('');
} else {
$_mod = $_count % $_num;
$_div = floor($_count / $_num);
for($_x = 0; $_x < $_num; $_x++) {
if($_x+1 <= $_mod)
$_length = $_div + 1;
else
$_length = $_div;
$_split_array[$_x] = !empty($params['var']) ? array_splice($params['var'],0,$_length) : array('');
}
}
$smarty->assign($params['assign'],$_split_array);
}
?>
|
Last edited by mohrt on Mon Mar 31, 2008 7:06 pm; edited 2 times in total |
|
Back to top |
|
t-rob Smarty Rookie
Joined: 22 Feb 2006 Posts: 9 Location: Amsterdam, the Netherlands
|
Posted: Wed Feb 22, 2006 12:13 pm Post subject: |
|
|
Could you give me more examples on how to use this function? I can get it working but not exactly the way it should, example:
x[0][0]='hp'
x[0][1]='notebook'
x[0][2]='on stock'
x[1][0]='ibm'
x[1][1]='server'
x[1][2]='no stock'
x[2][0]='acer'
x[2][1]='desktop'
x[2][2]='on stock'
x[3][0]='lenovo'
x[3][1]='tft'
x[3][2]='no stock'
what I need is a table like this:
<table border="1">
<tr>
<td>hp</td><td>ibm</td><td>acer</td>
</tr>
<tr>
<td>lenovo</td><td></td><td></td>
</tr>
</table>
I need to configure the amount of colums and the rows should follow untill there is nothing in the array x anymore. How do I do this? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Feb 22, 2006 5:42 pm Post subject: |
|
|
what does the original array look like? maybe you need something like html_table (comes with Smarty) instead of split_array. |
|
Back to top |
|
t-rob Smarty Rookie
Joined: 22 Feb 2006 Posts: 9 Location: Amsterdam, the Netherlands
|
Posted: Thu Feb 23, 2006 1:13 pm Post subject: |
|
|
Never mind, somebody gave a very handy solution:
Code: | <table>
<tr>
{section name=nr loop=$array}
<td>{$array[nr]}</td>
{if $smarty.section.nr.iteration % 3 == 0}
</tr>
<tr>
{/if}
{/section}
</tr>
</table> |
Do not really understand what happens here but it works perfectly. I have to admit, I've been using Smarty for three days now and I'm absolutely in love with the tool! |
|
Back to top |
|
k2merlinsix Smarty n00b
Joined: 12 Apr 2006 Posts: 3
|
Posted: Wed Apr 12, 2006 5:21 pm Post subject: What about tab like appearance |
|
|
I am querying a mysql database and getting a list of datatypes from different tables. I display set_1 by default. If someone clicks on the tab/hyperlink for set_4 I want it to display that.
I am using a {foreach} loop to display the original set of data but when someone clicks on a different set the data that was in the assigned var is now gone. Does the foreach erase an assigned variable after it runs on it?
Example
callQuery.php ...
if($queryResults != null)
array_push($results, $queryResults);
$Smarty->assign("dataResults", $results);
$Smarty->assign("dataType", "type1");
$Smarty->display("T_QUERY_DISPLAY");
queryDisp.tpl ...
{include file="headerHyperlinks.tpl"}
{foreach item=list from=$dataResults}
{foreach item=field from=$list}
{if $dataType eq $field.type}
$field.type &
$field.location<br>
{/if}
{/foreach}
{/foreach}
[EVERYTHING WORKS GREAT TO THIS POINT]
User clicks on different data set and if I try to print any part of the $dataResults at any point it is always null.
Maybe I am missing something?
Any help would be great!! |
|
Back to top |
|
solty Smarty n00b
Joined: 11 Mar 2010 Posts: 4
|
Posted: Wed Mar 17, 2010 7:53 am Post subject: Where is the code? |
|
|
My journal archive like this.
2009
Vol 7, No 4 (2009)
Vol 7, No 3 (2009)
Vol 7, No 2 (2009)
Vol 7, No 1 (2009)
2008
Vol 6, No 4 (2008)
Vol 6, No 3 (2008)
Vol 6, No 2 (2008)
Vol 6, No 1 (2008)
2007
Vol 5, No 4 (2007)
Vol 5, No 3 (2007)
Vol 5, No 2 (2007)
Vol 5, No 1 (2007)
and my code is
{**
* archive.tpl
*
* Copyright (c) 2003-2006 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* Issue Archive.
*
* $Id: archive.tpl,v 1.15 2006/06/12 23:26:28 alec Exp $
*}
[code]{assign var="pageTitle" value="archive.archives"}
{include file="common/header.tpl"}
{iterate from=issues item=issue}
{if $issue->getYear() != $lastYear}
{if !$notFirstYear}
{assign var=notFirstYear value=1}
{else}
</div>
<br />
<div class="separator"></div>
{/if}
<div>
<h2>{$issue->getYear()|escape}</h3>
{assign var=lastYear value=$issue->getYear()}
{/if}
<h2><a href="{url op="view" path=$issue->getBestIssueId($currentJournal)}">{$issue->getIssueIdentification()|escape}</a></h2>
{/iterate}
{if $notFirstYear}</div>{/if}
{if !$issues->wasEmpty()}
{page_info iterator=$issues}
{page_links name="issues" iterator=$issues}
{else}
{translate key="current.noCurrentIssueDesc"}
{/if}
{include file="common/footer.tpl"}[/code]
I want to change to 2-3 column (without dots :-))
2009.................................... 2008
Vol 7, No 4 (2009)................. Vol 6, No 4 (2008)
Vol 7, No 3 (2009).................. Vol 6, No 4 (2008)
Vol 7, No 2 (2009)...................Vol 6, No 3 (2008)
Vol 7, No 1 (2009)...................Vol 6, No 2 (2008)
I was not find appopriate code location in the my .tpl.
I am new...
Any suggestion...?? |
|
Back to top |
|
solty Smarty n00b
Joined: 11 Mar 2010 Posts: 4
|
Posted: Mon Mar 22, 2010 8:16 am Post subject: I was not solved |
|
|
Any suggestion...?? |
|
Back to top |
|
|
|
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
|
|