How to make a Symfony2 Twig Extension
I couldn’t find a decent tutorial online that explained twig extensions in a simple way so for those of you in need here is a step by step guide on how to create a twig extension for Symfony2.
The first thing to know is the difference between twig global, filter and functions (or just how the look in twig templates).
Global
{{ global_variable }}
Filter
{{ 'some sort of text'|filter }}
Function
{% function('some variable or text') %}
If you want more information look here:
http://twig.sensiolabs.org/doc/templates.html
For the following steps remember to change the ‘Bundle’ and ‘NameBundle’ to your bundles values. Also note that the following code is just for creating filters.
Step 1: Create the filter
Create a file Bundle/NameBundle/Extension/MyTwigExtension.php and use the following code inside it.
<?php
namespace Bundle\NameBundle\Extension;
use Symfony\Component\HttpKernel\KernelInterface;
class MyTwigExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
'uppercase_first_letter' => new \Twig_Filter_Method($this, 'uppercase_first_letter'),
'var_dump' => new \Twig_Filter_Function('var_dump')
);
}
function uppercase_first_letter($var){
return ucwords($var);
}
public function getName()
{
return 'recensus_twig_extension';
}
}
So I’ve created two filters that I can use in my twig templates, uppercase_first_letter and var_dump. var_dump is already a function within php so that just works without having to create the method within the MyTwigExtension class. uppercase_first_letter require me to create a public method within the class.
Notice the different classes used, Twig_Filter_Method for a custom methods and Twig_Filter_Function for a php method.
Step 2: Register with config
Then you need to add the following code to the bottom of the config file (/app/config/config.yml).
services:
anywordyouwant.twig.extension.mytwigextension:
class: Bundle\NameBundle\Extension\MyTwigExtension
tags:
- { name: twig.extension }
Step 3: Use the filters
Inside your twig templates
{{ 'what ever you want'|uppercase_first_letter }}
How to ipconfig/flush dns on Mac OSX
On Lion (Mac OS X 10.5, 10.6, or 10.7)
dscacheutil –flushcache
On Mac OS X 10.4 Tiger
lookupd –flushcache
Sendmail on Ubuntu
Install Sendmail on Ubuntu
I also need my php applications to be able to send our email using the mail function. Therefore I need to install sendmail and enable it in my php.ini file.
Install Sendmail
apt-get install sendmail
Check its working
ps -aux | grep sendmail
Change php.ini
replace ;sendmail_path = with sendmail_path = /usr/sbin/sendmail
Config Sendmail
sudo sendmailconfig
Change your host files
homename /etc/hosts 127.0.0.1 localhost.localdomain localhost myhostname
Adobe Air with Netbeans
I love Netbeans, it’s by far my favourite IDE for developing PHP applications. Recently, I’ve needed to create an Adobe Air application and from what I remember the best free IDE on windows for this was Aptana, which is ok however I found it a little slow (and I already know all the Netbeans shortcuts). So what I really wanted to do was developed Adobe Air with Netbeans, and you can…
The video below shows you step by step however it’s 5 minutes long and I can explain in 5 lines.
- Download the Adobe Air SDK, unzip.
- Open Netbenas, Create a new PHP Project
- On the 3rd page of creating a new project (Run Configuration) choose Run As: Script (run in command line)
- Then choose AdobeAIRSDK\bin\adle.exe as the PHP Interpreter
- The final step which isn’t in the video, (after creating the index.html file and application.xml file) click the Run Project button and it will ask you for Run Configuration, makes sure you choose application.xml as the Index File NOT index.html.
Hopefully this will work for you as well!
Recent Posts
- How to get environment in Symfony2
- How to make a Symfony2 Twig Extension
- How to ipconfig/flush dns on Mac OSX
- Diary of a Startup: Create a startup for £14
- Sendmail on Ubuntu
- Adobe Air with Netbeans
- Best Firefox Plugins for SEO aka Addons
- Setup Ubuntu as a Web Server
- Changing Plesk Fast CGI to Apache PHP with ZF
- How to Access PUT data in Zend Framework
My External Links
Twitter Updates
- What I do when my mum calls me for help using her computer xkcd.com/627/ via @neil_pie 1 day ago
- Fancy this? @bwaine @WillBooth Hackathon London #hackldn - An Event for developers and Startups hackathonlondon.com via @hackathonldn 1 day ago
- New Kings Cross, much much better @ Kings Cross St Pancras New Departures Hall instagr.am/p/KAi6iNs_-6/ 2 days ago
- Cine: Ed and Rob cinemagr.am/show/5227880 #cinemagraph #gif 3 days ago
- The 8 Signs That You Have an Extraordinary Boss inc.com/geoffrey-james… 4 days ago
- How Social Media Is Taking Over the News Industry [INFOGRAPHIC] mashable.com/2012/04/18/soc… via @mashable 1 week ago
- UK's largest water feature. Cost £24.4m in Bradford @ City Park instagr.am/p/JuaRBxs_yK/ 1 week ago
- RT @lucywardleeds: Hilarious! Newcastle got our score wrong and have reported winning the title nufc.co.uk/articles/20120… @ed_liddell 1 week ago
- Zombie google maps fastcodesign.com/1669550/zombie… 1 week ago
- The worst names for variables in code petdance.com/2012/04/the-wo… 1 week ago
Archives
- April 2012
- March 2012
- January 2012
- December 2011
- November 2011
- September 2011
- August 2011
- June 2011
- May 2011
- March 2011
- February 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- March 2010
- February 2010
- January 2010
- November 2008
- September 2007
- June 2007
- May 2007
- April 2007




