\_o< ~ Mirmo Dynamics

Rien de grand ne se fit jamais sans enthousiasme.

To content | To menu | To search

Keyword - handy

Entries feed - Comments feed

Sunday 2 November 2008

Passing arbitrary parameters to a route in symfony

Parameters are an important parts of routes in symfony, and in any other routing framework for that matter, and everyone knows how to add mandatory or optional parameters to a route. But did you know you can also silently pass arbitrary parameters to a route from the routing definitions ? This may not be a very clear description, so let's take an example. Say you have a tabbed navigation system, and you want to know in your template which tab is the current one. In a perfect world, you could assume one tab corresponds to a module and check this module, but let's say that one tab can contain multiples modules. How are you doing now ? Quite simply actually, by passing a tab parameter to your route.

foo_route:
  url:       /foo
  param: { tab: foobar }

bar_route:
  url:       /bar
  param: { tab: foobar }

Now you can access the tab parameter in your layout:

<?php $currentTab = $sf_params->get('tab'); ?>

Easy heh ? Of course this kind of parameters is not limited to tell which tab we're currently in, you can, for example, bypass the limitation of a system that requires a particular file to be in a particular location (not that using mod_rewrite would not be more efficient, I just wanted one more example):

foobar_xml:
  url:       /a/stupid/url/that/you/cant/change/foobar.xml
  param: { module: default, action: static, file: "/assets/foobar.xml", mimetype: "text/xml" }

Here we simply pass the file and mimetype parameters to an action that'll take care to serve the right file.

Handy ! But please note that you can't pass such parameters in sfPropelRouteCollection for now (yup, this a bug, and there's a ticket already).

Thursday 16 October 2008

Mass vim file opening reloaded

Thinking about it, the snippet I posted earlier was a bit silly as vim can open by itself multiple files way more efficiently. The only benefit from my script is that I don't have to type the --servername and --remote-silent-tab.

So let's add some usefulness ! First, when I need this script, it's often that I first grepped the files, and then decide that I want to edit them all. From this point of view, having to pass them as arguments is not that handy, so we'll add a way to pass them via STDIN.

Also, what if I want to send them to another vim server ? We'll add this ability too.

Here is the resulting script:

#!/usr/bin/env bash

# set a default SERVERNAME
SERVERNAME="ash0"

# look for files on stdin
if ! [ -t 0 ]; then
  FILES=`cat /dev/stdin`
else
  exit;
fi;

# now let's check if we want a specific vim server
if ! [ -z $1 ]; then
  SERVERNAME=$1
fi;

# we can now open the files
/usr/bin/gvim --servername $SERVERNAME --remote-silent-tab $FILES

Tada !

You can now do the followings:

grep foo | sendtovim
grep foo | sendtovim grep0

Tuesday 14 October 2008

Mass vim file opening

Remember my old post about vim and rox-filer ? Well I've got one tiny and silly addition to it now, I can mass open files from any shell command line, with this little script placed in my ~/bin/ (don't forget to chmod +x):

for i in $*; do
  /usr/bin/gvim --servername ash0 --remote-silent-tab $i
  sleep 1
done;

The sleep 1 is necessary as vim does not seem to like being flooded with files.

Example usage:

sendtovim `grep foo *`

Opens in vim all files containing "foo" in the current directory. Who said handy ?

Note: I know have an updated and useful version of this script

Thursday 10 July 2008

Rename directories to lowercase

Using a single (and somewhat simple) shell line, you can rename any directory to its lowercase version.

for i in *; do lcase=`echo $i | tr A-Z a-z`; if [ ! -d $lcase ]; then mv $i $lcase; fi; done;

Viva el chell !