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

Simulating print_r

 
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
danson
Smarty Rookie


Joined: 03 Dec 2004
Posts: 5

PostPosted: Fri Dec 03, 2004 7:45 pm    Post subject: Simulating print_r Reply with quote

Actually a function that i have written called print_r_html that does the necessary replacements of spaces with nbsp's and /n with <br>.

I'm a smarty newb (born just a few days ago) so please take it easy!

I'd just like a nice way to get smarty to print out an array structure. I spent quite some time searching and found lots of threads showing sections and foreach's which work for displaying the table in html tables.

I was about to see how to just call the php function (and found the {php} tags) but then read this is really unadvised. I'm not sure why exactly as long as you restrict your php use to DISPLAY FUNCTIONS only...?

Anyway, could someone give me some advice on how to achieve what I am looking for? I am liking smarty overall!

Much thanks,
Daniel
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 03, 2004 7:57 pm    Post subject: Reply with quote

There are several ways to do this. One way is to just {$var|@print_r} although that might not be the best formatting for HTML.

Here is how I do it. Put this in your plugin directory (modifier.dump.php)

[php:1:484d65147b]<?php

/*
* Smarty plugin
* -------------------------------------------------------------
* Type: modifier
* Name: dump
* Version: 1.0
* Date: July 28, 2004
* Author: Monte Ohrt <monte@ispi.net>
* Purpose: dump the contents of a var
* Input: var to dump
* Example: {$var|@dump}
* -------------------------------------------------------------
*/
function smarty_modifier_dump($var)
{
$_ret = "<PRE>\n";
$_ret .= print_r($var,true);
$_ret .= "</PRE>\n";
return $_ret;
}

/* vim: set expandtab: */

?>[/php:1:484d65147b]

Usage: {$var|@dump}

Be careful to include the "@" symbol so the modifier acts on the array.
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: Fri Dec 03, 2004 9:01 pm    Post subject: Reply with quote

Either {debug} (which opens a separate debug console) or the undocumented debug_print_var modifier are useful:

{$foo|@debug_print_var}

But I often prefer:

{$foo|@var_export:true|nl2br}
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 03, 2004 9:14 pm    Post subject: Reply with quote

boots wrote:
{$foo|@var_export:true|nl2br}


That works well too, I like the fixed-width font format of the <pre> tags, pick your poison Wink
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: Fri Dec 03, 2004 10:34 pm    Post subject: Reply with quote

mohrt wrote:
boots wrote:
{$foo|@var_export:true|nl2br}


That works well too, I like the fixed-width font format of the <pre> tags, pick your poison Wink


Sure. Then I'd do:

<pre>{$foo|@var_export:true|nl2br}</pre>

If you wanted to do that often your modifier is nice and to the point--I just am not a big fan of canning HTML in plugins. On the other-hand, debug_print_var already gives nicely formatted results. That's what's lovely about Smarty--there is no right way, just style baby!

Oh, and by-the-way, danson, this works because under the default settings, Smarty allows you to use global PHP functions as modifiers.

Cheers to all.
Back to top
View user's profile Send private message
danson
Smarty Rookie


Joined: 03 Dec 2004
Posts: 5

PostPosted: Sat Dec 04, 2004 12:31 am    Post subject: Reply with quote

Hi guys,

What a fantastic board - a reply within about 10 mins!

Just thought I'd give a summary:

I tried them all and ended up using:

<pre>{$var|@print_r}</pre>

This seems a bit easier to understand than some of the other ones:

{$var|@dump}
Here dump is a modifier which just saves you having to add the <pre> tags - a good idea if you are going to be doing a lot of array dumps.

{$var|@debug_print_var}
This gives a different format - the key's are all bolded. Quite a nice effect although the spacing isn't as nice.

{$var|@var_export:true|nl2br}
Actually prefer:
<pre>{$var|@var_export:true}</pre> as it gives slightly better formatting. Not quite as good as print_r though. The ":true" didn't actually seem to do anything...?

I didn't get the {debug} tag to work to pop up a window. The debug modifier file doesn't have an example of use but TFM probably has it documented.

Thanks guys!
Dan
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Dec 05, 2004 5:03 pm    Post subject: Reply with quote

danson wrote:
":true" didn't actually seem to do anything...?


The true parameter tells print_var to return its value instead of echoing it. In this case the output ends up being the same, although it is more proper to return it in general.
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: Mon Dec 06, 2004 1:57 am    Post subject: Reply with quote

Just to further Monte's comment, it is actually necessary to use "true" if you want to continue the modifier chain, eg:

{$var|@var_export:true|nl2br} would work

but if you do this:
{$var|@var_export|nl2br}

nl2br will not be applied since it will not have recieved any input from var_export.
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