Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Modifying an Array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
forgo
Smarty n00b


Joined: 06 Aug 2003
Posts: 1

PostPosted: Wed Aug 06, 2003 3:54 am    Post subject: Modifying an Array Reply with quote

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 Confused . 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
Code:
Array


Any help would be appreciated.

Thanks,
Mark
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Wed Aug 06, 2003 5:11 am    Post subject: Reply with quote

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
Code:
{print_r var=$arr}

_________________
"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
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 06, 2003 6:03 pm    Post subject: Reply with quote

@forgo: read about the @-operator on http://smarty.php.net/manual/en/language.modifiers.php

{$foo|@print_r} or maybe {$foo|@print_r|escape} Smile 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
View user's profile Send private message Send e-mail Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Wed Aug 06, 2003 8:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Aug 06, 2003 9:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Aug 06, 2003 9:42 pm    Post subject: Reply with quote

@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. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Aug 06, 2003 9:43 pm    Post subject: Reply with quote

Thanks Messju. I can't velieve I didn't know that Embarassed. That explains a lot, though!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP