Doctrine entities in Twig templates

In a Symfony2 project, Doctrine entities can be used inside Twig templates with the help of Twig extensions.

The example works with the standard Symfony2 installation and the AcmeDemoBundle, and it is supposed to add a set of links on one of the demo pages. Each link represents a color, while the HEX code for that color is displayed when the link is clicked. The colors are retrieved from the database.

The example is using Twig functions, but an alternative which is using global variables is also presented.

The Doctrine entity:

This entity has a basic structure which should store the name and HEX code for a color.

The Twig extension:

The following list contains the most important remarks related to the Twig extension:

  • The string returned by the “getName”  method – “color_extension” will be used to register the extension;
  • The “getColors” method is the one which returns the entities, using the EntityManager;
  • In order to have access to the EntityManager, the Twig extension needs to be registered by specifying that the EntityManager service (identified as “doctrine.orm.entity_manager”) will be passed as argument;
  • The key “get_colors” from the array returned by the “getFunctions” method is the actual Twig function name which needs to be called in the Twig template to return the entities.

Twig extension registration:

In order to register the Twig extension the following piece of code needs to be added to src/Acme/DemoBundle/Resources/config/services.xml:

As previously mentioned, the “color_extension” string is used in the service id and the EntityManager service is sent as parameter for the Twig extension.

Everything should be set by now, the only thing that needs to be done is to call the “get_colors” function inside a Twig template. Here’s an example:

The global variables alternative.

Update the Twig extension (src/Acme/DemoBundle/Twig/Extension/ColorExtension.php) with the following code:

This means that the “colors” key will become a global variable and can be used in a Twig template.

Easy file sharing with Python SimpleHTTPServer

An easy way to share files from your computer is to use Python SimpleHTTPServer. You don’t need to know Python programming to use the SimpleHTTPServer, the only requirement is to have Python installed on the machine where the files that needs to be shared are located.

Most Linux distributions are shipped with Python installed by default, but for Windows you might need to check how to use Python on Windows.

To start the SimpleHTTPServer “cd” into the directory which you want to be shared and run the command:

python -m SimpleHTTPServer 8000

You need to specify a port, usually 8000, like in the example should be just fine.
To access the shared files o to http://your_ip_address:8000.

Display random posts in WordPress

WordPress posts may be randomly displayed using the “Get random results from a MySql table” tip placed in a simple plugin.

What this plugin does is to simply add the “RAND()” value to “ORDER BY” clause in the sql query which retrieves the WordPress posts.

In order to use the plugin, just place the above code in a file, upload it to your WordPress plugins directory and activate it. If you use cache related plugins it may be necessary to disable them to view the randomized posts.

Get random results from a MySql table

It is possible to retrieve random results from a MySql table using the RAND() mathematical function.

In order to get the random results, the RAND() function should be used in the ORDER BY clause, like in the example below:

Retrieving random results directly from MySql is useful since you don’t need another step in your application in order to manually randomize the sql results. For example if the sql results are retrieved without being randomized you need to use a “shuffle” like function to get random results in PHP.