News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Rob Allen:
Overriding the built-in Twig date filter
December 16, 2014 @ 09:45:31

In his latest post Rob Allen shows a way you can override the default Twig date filter with your own custom Date extension handling.

In one project that I'm working on, I'm using Twig and needed to format a date received from an API. The date string received is of the style "YYYYMMDD", however date produced an unexpected output. [...] This surprised me. Then I thought about it some more and realised that the date filter is treating my date string as a unix timestamp. I investigated and discovered the problem in twig_date_converter.

He includes some example code you'll need to create the custom renderer. As part of the internals of how Twig formats the date currently is internal and can't be changed, he opted to override the extension itself. As a result, the call to the filter is exactly the same as before, the output results are just formatted more correctly.

0 comments voice your opinion now!
twig override default date filter custom extension

Link: http://akrabat.com/php/overriding-the-built-in-twig-date-filter/

Bernhard Schussek:
Puli Powerful Resource Management for PHP
December 04, 2014 @ 11:53:22

Bernhard Schussek has announced a tool for handling resources in a more Composer-inspired way: Puli. Puli is described as a tool that "manages files, directories and other resources in a filesystem-like repository".

Unfortunately, sharing your work gets a lot harder when you leave PHP code and enter the land of configuration files, images, CSS files, translation catalogs - in short, any file that is not PHP. For brevity, I'll call these files resources here. Using resources located in Composer packages is quite tedious: You need to know exactly where the package is installed and where the resource is located in the package. That's a lot of juggling with absolute and relative file system paths and prone to error.

[...] One and a half years ago I talked about this problem with PHP-FIG. I wrote a blog post about The Power of Uniform Resource Location in PHP. Many people joined the discussion. The understanding of the problem and its solution got riper as we spoke. Today, I am glad to present to you the first (and probably last) alpha version of Puli, a framework-agnostic resource manager for PHP.

He walks through the basics of the tool - how it manages the various resources, what the configuration looks like and how it can directly be integrated with Composer. He also points to a Twig extension that allows for Puli integration via paths in your templates. There's also a Symfony bridge that lets you use it in your YAML configuration files.

0 comments voice your opinion now!
resource management puli tool composer twig symfony integration

Link: http://webmozarts.com/2014/12/03/puli-powerful-resource-management-for-php/

Rob Allen:
Using ZF2 Forms with Twig
September 23, 2014 @ 09:28:53

Rob Allen has a new post today showing how to integrate Zend Framework 2 forms into a Slim framework based application. He started the topic in a previous post and continues, this time using the Twig templating framework to handle the rendering.

The ZF2 view helpers, formRow and formElement now look like Twig functions, however we don't want to have to rewrite all our ZF2 view helpers into Twig. Fortunately, Twig supports the concept of a undefined function callback is called whenever Twig encounters a function that it doesn't know how to call. We can use this to proxy through to the ZendView system and get it to render the ZF2 view helpers.

He shows how to use this callback functionality and a custom view layer with the Slim-Views component to render the output. He includes a simple Slim example, setting up the custom View class as a parser extension and how to register the callback to invoke Twig.

0 comments voice your opinion now!
zendframework twig render slimframework integration tutorial view

Link: http://akrabat.com/zend-framework-2/using-zf2-forms-with-twig/

NetTuts.com:
Working With Templates in Symfony 2
May 20, 2014 @ 10:34:05

NetTuts.com continues their introductory screencast series today with the latest episode focusing on using templates in Symfony 2. Other parts of the series have looked at using controllers, routing and some of the basics of the framework.

Today, let's learn about Symfony 2's Twig Templates. With Twig, you can seamlessly and painlessly integrate your PHP and HTML together, giving you a very clean looking presentation of your view code, without all the clutter of PHP tags, an overabundance of parenthesis, or semicolons. Instead, we have a simple and elegant syntax that we can use.

The screencast works with a sample application and a bundle to render the Twig template and includes examples of defining blocks of content and extending other templates. They also show you how to use layouts and pass data out to the template to be rendered on load. You can watch the screencast in-page or over on YouTube.

0 comments voice your opinion now!
introduction symfony2 template twig screencast series

Link: http://code.tutsplus.com/tutorials/working-with-templates-in-symfony-2--cms-21172

Custom Scripts Blog:
Dynamic layouts in Twig
December 18, 2013 @ 12:18:14

On the Custom Scripts blog they've shared a new tutorial showing you how to use the popular PHP templating tool Twig to create dyanmic layouts for your application.

Recently I worked on a project that needed dynamic layouts for each bundle. The idea was to have a base template, then several layout templates that each extends from the base. Each view would then extend from one of the layout files.

In the scenario I had, I needed to allow each bundle to specify there own stylesheets and javascripts. So each bundle needed to create a custom layout that overrides the parent stylesheet and javascript block adding their own content, and I did not want to re-create the layout files for each bundle.

He's created a base template for the entire site leaving areas for stylesheet and javascript injection as well as making it flexible enough to accept multiple column layouts. Layered on top of this is a base template for each bundle that's then extended via a custom extension class as needed. The PHP is also included to define this and the other extension classes you'll need to make the setup work.

0 comments voice your opinion now!
twig template dyanmic tutorial

Link: http://blog.customscripts.co.za/dynamic-layouts-twig/

Gonzalo Ayuso:
Sending automated emails with PHP, Swiftmailer and Twig
September 24, 2013 @ 11:40:57

Gonzalo Ayuso has posted a new tutorial today showing you how to combine PHP, Swiftmailer and Twig to send automated emails from your application.

My work as host is basically pick the place and encourage people to join to the Coding Dojo. One way of doing this (besides twitter buzz) is take my address book and send one bulk email to all of them inviting to join us. I don't like this kind of mails. They look like spam, so I prefer to send a personalized email. This email has a common part (the place location, the hour, the event description, ...) and the personalized part. I can do it manually, the list isn't so huge, but definitely that's not cool. Because of that I have done a little script to perform this operation.

His example extracts the information from a simple spreadsheet exported as a CSV file. He creates a simple Mailer class that uses Swiftmailer to do the actual sending. You pass in the Twig rendering object (Twig_Environment) that's used to render the email output. He includes a "Spammer" class that uses the Symfony EventDispatcher to send the email if everything works or an error email if something fails. He then drops it all into a Symfony Console command structure, defines several configuration settings in a "config.yml" and makes the script to execute the commands.

0 comments voice your opinion now!
email swiftmailer twig template tutorial symfony console eventdispatcher

Link: http://gonzalo123.com/2013/09/23/sending-automated-emails-with-php-swiftmailer-and-twig

Alessandro Nadalin:
Integrating Twig in Your Legacy PHP Code
June 18, 2013 @ 10:19:49

Alessandro Nadalin has posted a two part series to his site to help you get the Twig templating tool integrated with your legacy codebase.

It might happen that you are working on a legacy code that is years old, with its own templating mechanism1 that doesn't really allow you to take advantage of the benefits that a structured and object-oriented engine like Twig. In this situations, when a complete replacement would cost too much to your organization, you can take advantage of a wild integration between this advanced template engine and your existing code.

In the first part of the series, he proposes integrating it at the base level, essentially wrapping the "render" method of your framework of choice. In the second part of the series, though, he comes back with a "less wild" approach that also lets you take advantage of some of the other features twig has to offer. He modifies the previous example to output a template file and reuse that in the Twig "render" call.

0 comments voice your opinion now!
twig legacy codebase integration template render

Link: http://odino.org/integrating-twig-in-your-legacy-php-code

Anthony Ferrara:
On Templating
December 11, 2012 @ 11:50:31

In this latest post to his site Anthony Ferrara take a look at templating in web applications - more specifically as it deals with his experience with the Mustache templating engine.

I've been playing around with tempting engines a lot lately. For a recent project, I needed the ability to re-use the same template set in both JS and PHP (coupled with the History API, providing seamless dynamic behavior, yet still having raw content pages). Realistically today, there's only one choice for that sort of requirement: Mustache. I've learned a lot while playing with Mustache, and it's really changed my entire viewpoint on presentation layer construction.

He briefly gives an overview of "the past" of templating in PHP (including a mention of Smarty) and how templating tools - like Mustache - have helped to improve the situation, especially when it comes to the separation of presentation and processing. As an alternative, there's also a mention of the Twig templating engine in the comments, another popular option from the Symfony project.

0 comments voice your opinion now!
templating presentation mustache twig introduction opinion


Dev.Umpirsky.com:
Use Poedit to Extract Translations from Twig Templates
September 04, 2012 @ 11:34:51

In this new post to the Umpirsky.com development blog, they show you how to use poedit to extract translations from Twig templates.

By default, Poedit does not have the ability to parse Twig templates. In Twig documenation there is a simple example which shows how to extract translations. This can work with few modifications, but I wanted to make reusable tool which will allow you to parse Twig templates just like you do with plain PHP templates.

The result is the Twig-Gettext-Extractor that's set up inside Poedit as a parser command for "*.twig" files. This automatically extracts the information so that the editor can read the data from the template.

0 comments voice your opinion now!
twig template poedit translation gettext extract


Dean Clatworthy:
Theming/styling error messages in Symfony 2
August 30, 2012 @ 11:40:40

For the Symfony2 users out there, Dean Clatworthy has a handy tip to help you customize the output of your application a bit more - a method for styling the error messages coming from forms using a custom template.

I spent a large portion of my day today trying to customize the HTML produced by Symfony 2 for form errors. The documentation has a section on how to do this, but for the life of me, I could not make it work. Here is a working, re-usable solution.

His solution involves the creation of a template in your "/Resources/views/Form/" directory that contains a Twig template for the error set output. This is then applied in your view using an additional parameter on the error output tag, including this new template from the "Form" directory. This sort of styling could also be applied if you needed custom elements with their own layouts in your forms as well.

0 comments voice your opinion now!
symfony2 error message theme style twig template tutorial



Community Events





Don't see your event here?
Let us know!


language framework interview podcast introduction community laravel library series version tool voicesoftheelephpant security opinion list symfony composer example conference package

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework