02.
Functions, Filters and Debugging with dump
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
12 Comments
I think that Twig has trouble guessing the getter name, is have a boolean property in DB called is_active, the getter (generated) is getIsActive():?bool , Twig says it cannot find a method [list of guessed names], like I had to rename the getter to a proposed one like getis_active() to make it work. However Symfony has no problem with this. Do I have to conform to a naming convention with Twig?
Hey Benoit L.!
Hmmm. If you have a getIsActive method, would expect that you could use
{{ object.isActive }}. Is that not working? Or were you using something like{{ object.is_active }}> The second would indeed not work withgetIsActive()... but let me know what your situation is :).Cheers!
Yeah it works thanks! I was expecting Twig to literally access the object property directly, in fact it guesses it from the getter method name.
Hi Ryan,
Do you know if it is possible to clear cache of a particular twig file rather than clearing the entire cache. Suppose on a login page i add a hyperlink to the registration page, now for this change to be seen by the user should not require me to clear the entire cache directory so thats why i am asking if it is possible to clear the cache of a single file? or setup symfony in a way that if it detects a change in view then recreate the cache.
Hey Shairyar!
Obviously, the "dev" environment already detects cache changes, but that's not something you'll want to activate for the "prod" environment - it just has too much performance impact. The best answer to your question is - don't try this :). Symfony's cache is meant to be completely cleared and warmed up. I assume you're making changes on production and don't want to run cache:clear? Is there a reason why clearing the full cache is a problem? Technically, you could probably do something like this (but no guarantees!)
That almost certainly won't work yet - but if it's possible, this is the general idea. But mostly, you shouldn't do it ;).
Cheers!
Thanks, I am working on a project where the admin wants a way to be able to edit email templates and right now the templates are twig files in bundle so I was wondering if I allow them to update twig files then cache has to be cleared and that's why I asked if we can clear a single file cache in this case the email templates. Right now it is clearing the entire cache, my only concern while clearing the entire cache is if the users on website will face problem surfing the website while admin is making the changes.
If this is a bad idea then I am thinking to save email templates in DB this way no cache clear will be required.
Hey Shairyar!
Ah, yea - that's a cool use for Twig. Yes, save them to the database (it's just easier anyways - no need to worry about file permissions on your web server, etc). Then, create your *own* twig service (instead of the core Twig service) and use *it* to render the templates. Literally, in service.yml, you'll have a new entry called something like 'email_template_twig' and you'll set the class to Twig_Environment and configure as needed. For this service, I wouldn't even use caching - you'll be rendering these templates so rarely, it won't matter much. And if you do use caching, you can set it to a specific directory (away from the normal Twig cache) and then delete that entire directory when one of the templates is updated.
Good luck - sounds cool!
Hi Ryan,
Thanks alot. I ended up creating a service and a twig extension that will give me access to all the emails content that admin has set in admin panel, it worked out pretty well.
Thanks alot.
Hello — just wondering how come the dump function in the tutorial shows a clearly formatted chunk of info, whereas I get a non-formatted string (same info) on my setup?
(macosx browsers running off MAMP)
— what is generating the styled dump in the tutorial screenshot at 5'25" mins into the tutorial? Perhaps a different software tool / browser?
thanks!
Hey Matthew,
Yes, it's a special PHP extension called Xdebug. While it's installed - you'll get more nice output for var_dump() function. You can install it easily with Homebrew MacOS package manager if you're handling your PHP version with Homebrew in console, but if you're using MAMP - probably you can activate it somewhere in its preferences, or, just Google for how to do it properly for MAMP.
P.S. Also take a look at VarDumper Symfony Component: https://symfony.com/doc/cur... - you can install this library with Composer in your project and use it. So even if you do not have Xdebug, with dump() function you'll get much more nice output and even can easily search inside it ;)
Cheers!
Hi Victor — many thanks, great tip, and now I know what it does, it is a simple toggle in the PHP settings in MAMP Pro to get this running — big improvement!
Hey Matthew,
Glad to know you MAMP Pro has this option. Thanks for sharing this information with others.
Cheers!
"Houston: no signs of life"
Start the conversation!