Jump to content

Use the JS console for quick tests


diogo
 Share

Recommended Posts

Guys, here's a small trick to test things out on your sites:

foreach($page->children as $p) {
    echo "<script>console.log('" . $p->title . "')</script>";
}
 
Now open the console and enjoy  ;)
 
 
For more power make sure to check Soma's Chrome PHP Logger
 
 
---
 
Edit:
Or put this simple function or your _init.php
function JSlog($str) {
    echo "<script>console.log('" . $str . "')</script>";
}

and on your templates

JSlog($page->title);
Edited by diogo
added the function
  • Like 12
Link to comment
Share on other sites

Thank you, diogo!

As we are in tutorial mode here, it is worth mentioning the benefits of this approach versus the common echo / print_r / var_dump. I can think of two:

  1. You can check variables on the live site, where outputting things in the document html may scare site visitors.
  2. In some cases, for example in Reno theme, html output is hard to find and read on the screen. So console log is simply clearer.

I am not sure (as I have not tested it), but this technique may help debugging PW admin with its redirects.

Link to comment
Share on other sites

I thought of this only for the frontend, really. Your first point is the main advantage for me. The second advantage is that, even if the site is not live, you don't have to decide a place on the page to output your tests, and you don't have to format the markup (for instance with <pre>) because everything will be output nicely in the console.

Link to comment
Share on other sites

Sometimes I use Tracy for debugging (mainly for dumping variables). It's debugger bar sticks to the bottom right corner of the window so it's easy to spot the errors/notices. Of course when using it outside the Nette framework doesn't show queries and routing.

tracy-bar.png

If anyone's interested I can share how to set it up in PW (nothing special).

I'll also try Soma's logger in the future.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...