Functions, Filters and Debugging with dump

Just like PHP or JavaScript, Twig has functions that can be used once we're
inside either a say something or do something
Twig delimiter. To see the built-in functions, once again check out the bottom
of the Twig Documentation page. In your application - especially
if you're using Twig inside something like Symfony or Drupal - you may have
even more functions available to you. Fortunately, the syntax to use a function
is always the same: just check out your project's documentation to see what other
goodies you have.

This is nice, but Twig has something even cooler: filters! Like with everything,
you can find a filter list in the Twig Documentation, though
you may have even more in your project. Let's check out the upper filter.
We can use the upper filter to uppercase each product by placing a pipe (|)
to the right of the variable and using the filter:

{# ... #}
<h2>{{ product|upper }}</h2>

This works just like a function, except with a different syntax. Whatever
you have to the left of the pipe is passed to the filter, which in this example,
upper-cases everything.

This filter takes an array or collection to the left and transforms it into
a number, which represents the number of items in the collection. We can use
this to see if there are no products. I'll temporarily pass in zero
so we can check this out.

Just like functions, sometimes a filter has one or more arguments. A really
common filter is date. This takes a date string or PHP DateTime
object and changes it into a string. We can go to date() to look up
the letters used in the date format. To try this out, we can just hardcode
a string to start:

The "tomorrow noon" part is just a valid input to PHP's strtotime()
function, which accepts all sorts of interesting strings as valid dates.
The Twig date filter takes that string and renders it in the new format that
we want. Of course, we can also send a variable through the date filter.
Let's pass in a saleEndsAt variable into the template and render it the
same way:

Before we move on, let's talk about the dump() function. If you don't know
what a variable looks like, use the dump() function to see its details:

{{ dump(products) }}

Even better, use the dump() function with no arguments to see all the variables
that you have access to:

{{ dump() }}

With this function, there's not much you won't be able to do!

We experimented a lot in this section. I'll use the {# syntax to comment
out some of the things we've done so that our page makes a bit more sense.

To clean things up, we removed the upper and reverse filters,
the entire spot where we print the random numbers, and the printing of
the current year.

Leave a comment!

2017-10-31Victor Bocharsky

Hey Matthew,

Glad to know you MAMP Pro has this option. Thanks for sharing this information with others.

Cheers!

2017-10-30Matthew Appleton

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!

2017-10-30Victor Bocharsky

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!

2017-10-28Matthew Appleton

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!

2016-03-07Shairyar Baig

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.

2016-03-06weaverryan

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!

2016-03-03Shairyar Baig

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.

2016-03-03weaverryan

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!

2016-03-01Shairyar Baig

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.