PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Structures » Structures_DataGrid_Renderer_Smarty » Bug #8237

Can't use multiple grids on the same page

Details

Request #8237Can't use multiple grids on the same page
Submitted2006-07-18 12:44 UTC
Fromalexkunin at gmail dot com
Assignedolivierg
StatusClosed
PackageStructures_DataGrid_Renderer_Smarty
PHP VersionIrrelevant
Roadmaps(Not assigned)

Comments

[2006-07-18 12:44 UTC] alexkunin at gmail dot com

Description:
------------
There is no possibility to render several grids within same Smarty template - their variables will overlap. Of course, we can create another instance of Smarty, render grid inside it, and then pass ready HTML code to main template, but this does not look right (actually, this looks like a workaround).

Test script:
---------------
Here is a patch that solves the problem:

--- old\Smarty.php Tue Jul 18 15:11:37 2006
+++ Smarty.php Tue Jul 18 15:15:51 2006
@@ -180,17 +180,17 @@
$id = __CLASS__ . '::' . __FUNCTION__;
return PEAR::raiseError("$id: no Smarty container loaded");
}
- $this->_smarty->assign('currentPage', $this->_page);
- $this->_smarty->assign('recordLimit', $this->_pageLimit);
- $this->_smarty->assign('columnsNum', $this->_columnsNum);
- $this->_smarty->assign('recordsNum', $this->_recordsNum);
- $this->_smarty->assign('totalRecordsNum', $this->_totalRecordsNum);
- $this->_smarty->assign('pagesNum', $this->_pagesNum);
- $this->_smarty->assign('firstRecord', $this->_firstRecord);
- $this->_smarty->assign('lastRecord', $this->_lastRecord);
- $this->_smarty->assign('currentSort', $this->_currentSort);
+ $this->_smarty->assign($this->_requestPrefix . 'currentPage', $this->_page);
+ $this->_smarty->assign($this->_requestPrefix . 'recordLimit', $this->_pageLimit);
+ $this->_smarty->assign($this->_requestPrefix . 'columnsNum', $this->_columnsNum);
+ $this->_smarty->assign($this->_requestPrefix . 'recordsNum', $this->_recordsNum);
+ $this->_smarty->assign($this->_requestPrefix . 'totalRecordsNum', $this->_totalRecordsNum);
+ $this->_smarty->assign($this->_requestPrefix . 'pagesNum', $this->_pagesNum);
+ $this->_smarty->assign($this->_requestPrefix . 'firstRecord', $this->_firstRecord);
+ $this->_smarty->assign($this->_requestPrefix . 'lastRecord', $this->_lastRecord);
+ $this->_smarty->assign($this->_requestPrefix . 'currentSort', $this->_currentSort);

- $this->_smarty->register_function('getPaging',
+ $this->_smarty->register_function($this->_requestPrefix . 'getPaging',
array(&$this, '_smartyGetPaging'));
}

@@ -236,7 +236,7 @@
$prepared[$index]['label'] = $spec['label'];
}

- $this->_smarty->assign('columnSet', $prepared);
+ $this->_smarty->assign($this->_requestPrefix . 'columnSet', $prepared);
}

/**
@@ -247,7 +247,7 @@
*/
function buildBody()
{
- $this->_smarty->assign('recordSet', $this->_records);
+ $this->_smarty->assign($this->_requestPrefix . 'recordSet', $this->_records);
}

/**

Bad news:

1. It breaks compatibiliy (however mostly theoretically since probably nobody was able to use nultiple grids before).
2. It works not so nice when grids are being generated dynamically, and Smarty code within template tries to enumerate and render several grids. And this is a real headache for me.

Better way. We could pass array instead of prefixed variables:

$this->_smarty->assign(
$this->_requestPrefix,
array(
'currentPage' => ...,
'recordLimit' => ...,
...
)
);

Grid instantination (assuming requestPrefix is 'myGrid'):

{include file=_typical_grid_template.html data=$myGrid}

Now any variable can be accessed like this: $data.currentPage etc.

[2006-07-29 13:13 UTC] olivierg at php dot net

Your patch and your idea to prefix Smarty variables is interesting. But please try to put links to patches instead of pasting them in here. See: http://www.phpfi.com

> 2. It works not so nice when grids are being generated
> dynamically, and Smarty code within template tries to
> enumerate and render several grids.

What's this? What's failing?

> Better way. We could pass array instead of prefixed
> variables.

Yes, but that won't work with the getSmarty() function. We'll still need to prefix it.

[2006-07-29 13:17 UTC] olivierg at php dot net

I meant: "it won't work with the getPaging()" function

[2006-07-31 16:48 UTC] alexkunin at gmail dot com

> Your patch and your idea to prefix Smarty variables is
> interesting. But please try to put links to patches
> instead of pasting them in here. See:
> http://www.phpfi.com

OK, thank you for the info. I was just following the
instructions. And patch was looking a bit more compact
until wrapping was applied.

> > 2. It works not so nice when grids are being generated
> > dynamically, and Smarty code within template tries to
> > enumerate and render several grids.
>
> What's this? What's failing?

getPaging() function, as you've said. Also, let's look
at the following scenario (actually implemented in my
current project).

There are page with few grids on them:
* we have some page which shows two grids (e.g. it
contains user account's info and shows list of
sessions, list of groups - two grids)
* and we have another page with one grid (e.g. it
contains user group's info and shows list of users
that belong to this group)
* and we have other pages, with three or four grids

I could create separate template for every page, but
isn't that doubling of code? So, I'm creating one
generic template which expects list of grids that will
be displayed one by one.

If I will pass grids as usual, then all variables will
overwritten by the last grid.

I can instantiate separate Smarty template for each
grid, render it, and then pass array of strings (each
string = complete HTML code for particular grid).
I don't like this solution. I just want to do all my
tasks within one Smarty instance.

Also I can pass prefixed variables and list of grid
prefixes. But how can I get particular variable value
within the Smarty template? By constructing its name and
{eval}'ing it. (Or maybe I just can't see easier way.)
Looks like a workaround.

Finally, I can pass something like this:

$smarty->assign(
'grids',
array(
$firstGrid->getVariables(),
$secondGrid->getVariables(),
$thirdGrid->getVariables()
)
);

And then {section var=i loop=grids} etc. - code is
trivial (except that getPaging function).

BTW, it looks like in this case I don't need anything
Smarty specific - just a generic renderer which does not
render anything, but only prepares all needed data as
one compact hash-based structure. Maybe, something like
Structures_DataGrid_Renderer_Array?

> > Better way. We could pass array instead of prefixed
> > variables.
>
> Yes, but that won't work with the getSmarty()
> function. We'll still need to prefix it.

Yes, exactly. And we again have to {eval}'uate composed
function name.

What about having one global associative array of
'grid_name' => &$ref pairs? Then we can pass exact grid
name to static version of _smartyGetPaging.

Sorry for the long post. Wasn't possible for me to express all this in a shorter manner.