• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / var_dump or print_r inside a Smarty template

var_dump or print_r inside a Smarty template

While trying to debug why something wasn’t working, I needed to see what data had been assigned to a Smarty template but from within the template itself (I didn’t have time to try and work out where in the PHP code the stuff was assigned and debug from there). This post shows how to do a var_dump or print_r from within the Smarty template itself.

Using {$var|print_r}

It is possible to do this in the template, where $var is the name of the variable:

{$var|print_r}

but the downside is that it only outputs the values and not the keynames, and there are no spaces between the values, so it’s not ideal for debugging.

Using a {php} block

So you can cheat instead and throw in a {php} block and simply execute some PHP code. The following code again gets the variable assigned as $var:

{php}
  $myvar = $this->get_template_vars('var'); var_dump($myvar);
{/php}

or

{php}
  $myvar = $this->get_template_vars('var'); print_r($myvar);
{/php}

You might want to enclose it with a <pre> block to make it more readable in the browser.

Dumping the values from PHP code

I’ve shown in the past how to dump the variables assigned to a Smarty template with PHP. See the related posts below for more details.

Check Out These Related posts:

  1. Bash For Loop
  2. Validating numbers with PHP
  3. Return information from PHP print_r instead of displaying it
  4. Get all variables assigned to Smarty – Part 1

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap