View previous topic :: View next topic |
Author |
Message |
danson Smarty Rookie
Joined: 03 Dec 2004 Posts: 5
|
Posted: Fri Dec 03, 2004 7:45 pm Post subject: Simulating print_r |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Dec 03, 2004 7:57 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Dec 03, 2004 9:01 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Dec 03, 2004 9:14 pm Post subject: |
|
|
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 |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Dec 03, 2004 10:34 pm Post subject: |
|
|
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 |
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 |
|
danson Smarty Rookie
Joined: 03 Dec 2004 Posts: 5
|
Posted: Sat Dec 04, 2004 12:31 am Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Dec 05, 2004 5:03 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Dec 06, 2004 1:57 am Post subject: |
|
|
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 |
|
|