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
Diary of a Startup: Create a startup for £14
Date: 2012-01-07
This post is the first in a series discussing my new startup, proudly named recensus.com (latin for review). So after spending about 24 hours trying to find a .com domain name I was happy with here were my steps.
1. Domain Registration
Registered the domain name with 123-reg.co.uk. I choose them because they are reasonably priced and you get control over the DNS records which they don’t charge you extra for and they haven’t gone down in. (Cost: £12)
2. Setup Cloudflare
Cloudflare.com is an amazing service that give you a DNS server which blocks spam, protects against security attacks, caches content using a CDN and a lot of other features. Well worth the free price tag, takes about 10 minutes to setup, I only wish they gave you the nameservers at the beginning of the setup and they had Google App’s DNS settings as a one click button. (Cost: free)
3. Setup Google Apps
Just in case you don’t know Google App’s give you Gmail, Google Calendar, Google Sites and Google Docs all under your own domain (free for up to 10 users). Again well worth the free price tag (although you have to look hire for the free version), just don’t forget to setup the subdomains. (Cost: free)
4. Setup Webspace and Upload Holding Page
I’ve got my own server (well Welford Media Ltd has) and I’ve just created a new domain and uploaded my holding page. If you haven’t got hosting you could go to Hostpapa.co.uk and click install wordpress, then download a coming soon template (see http://www.net-kit.com/12-free-attractive-themes-templates-for-coming-soon-pages/ or http://www.hongkiat.com/blog/coming-soon-templates-tutorials/). (Cost: £2 per month)
5. Signup to MailChimp
Mailchimp enables you to send out tracked email newsletters to subscribers and they have a free account which give you 2000 subscribers! After signing up put the signup form on your website. (Cost: free)
That’s the easy bit done, now i’ve just got build the app, do the sales and marketing, convert leads and make money. Also if possible get funding, find mentors and hire staff.
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
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
- Banoffee Pie with quadrupel beer amazing! @ The Met Hotel instagr.am/p/Kx8jCts_7h/ 22 hours ago
- More beer tasting with food this time @ The Met Hotel instagr.am/p/Kx5HpDM_5m/ 23 hours ago
- Beer testing in different glasses. I like this. @ The Met Hotel instagr.am/p/KxmKI5M_x3/ 1 day ago
- The unhappy crowd after my talk @ The Met Hotel instagr.am/p/Kxk8SUs_xX/ 1 day ago
- Plus new beer, quite strong @ The Met Hotel instagr.am/p/KxZDnus_8R/ 1 day ago
- About to do a talk in front of a lot of beer bloggers @ The Met Hotel instagr.am/p/KxYsTHs_8H/ 1 day ago
- 11" fits perfectly, when I upgrade to 13" I won't be able to work on the train anymore. instagr.am/p/Kutp5WM_5t/ 2 days ago
- RT @TheDeveloper: Self-hosted git management gitlabhq.com Essentially, open-source GitHub 2 days ago
- Instagram screensaver, love it. instagr.am/p/KuX9VdM_zH/ 2 days ago
- I'm at O2 Workshop (Camden Town, Greater London) 4sq.com/IVLiQR 4 days 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




