View previous topic :: View next topic |
Author |
Message |
forgo Smarty n00b
Joined: 06 Aug 2003 Posts: 1
|
Posted: Wed Aug 06, 2003 3:54 am Post subject: Modifying an Array |
|
|
I was messing around with Smarty, trying to become familiar for an upcoming project, when I came across something that confused me. Now, after reading around more, it seems as though this may not be possible. But I really don't know . So I ask you...
I wanted to assign an array to a variable and then have Smarty use a modifier to display it with print_r. I can of course use a foreach or such to display the elements of the array, but that's not really what I'm looking for.
This is what I have and what doesn't work. It appears to never call the modifier, as it outputs only "Array" with no pre tags.
PHP
Code: | <?php
function printr($str) {
return "<pre>" . print_r($str, TRUE) . "</pre>";
}
require('./libs/Smarty.class.php');
$smarty = new Smarty;
$arr = array(1,2,3,4);
$smarty->assign('arrdata',$arr);
$smarty->register_modifier("printr","printr");
$smarty->display('file:./index.tpl');
?> |
Template
Code: | {* Smarty *}
{$arrdata|printr} |
Output
Any help would be appreciated.
Thanks,
Mark |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Aug 06, 2003 5:11 am Post subject: |
|
|
Well Smarty takes the variable when sent to a modifier as a string so PHP will say Array a simple solution for what you wrote is to just make it a function[php:1:aebf81e7b5]$smarty->assign("arr", array(1, 2, 3, 4, 5));
$smarty->register_function("print_r", "printr");
$smarty->display("test.tpl");
function printr($params, &$smarty) {
return "<pre>" . print_r($params['var'], true) . "</pre>";
}[/php:1:aebf81e7b5]and in test.tpl _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Aug 06, 2003 6:03 pm Post subject: |
|
|
@forgo: read about the @-operator on http://smarty.php.net/manual/en/language.modifiers.php
{$foo|@print_r} or maybe {$foo|@print_r|escape} should do.
[edit: sorry, forget the escape-trick. i forgot that print_r *prints* and not returns the data. but since php-4.3.0 you can do {$foo|@print_r:1|escape}] |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Aug 06, 2003 8:04 pm Post subject: |
|
|
Wow I dont know how many times I have read through the manual and have never seen that _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Aug 06, 2003 9:31 pm Post subject: |
|
|
hmmm. I've used the array (@) syntax before but I am now a bit confused--Messju, does this mean that PHP functions are auto-registered as modifiers? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Aug 06, 2003 9:42 pm Post subject: |
|
|
@boots: yes they are for ages.
only with security turned on, you have to supply a whitelist of allowed modifiers in (OTOH) $smarty->security_settings['MODIFIER_FUNCS'], which contains only "count" by default. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Aug 06, 2003 9:43 pm Post subject: |
|
|
Thanks Messju. I can't velieve I didn't know that . That explains a lot, though! |
|
Back to top |
|
|