![]() |
|
Snippets |
|
sometimes you may need to include a stylesheet only for IE, the best effort you give, this box-model gives you headaches and there is no other solution. Same issue for JS files (but it should not happen anymore thanks to prototype).
In these cases you can simply add this in your <head> section
<head>
<?php include_http_metas() ?>
<?php include_metas() ?>
<?php include_title() ?>
<link rel="shortcut icon" href="/favicon.ico" />
<?php include_javascripts() ?>
<?php include_stylesheets() ?>
<!--[if IE 7]>
<?php echo stylesheet_tag('my-css-for-IE7') ?>
<![endif]-->
<!--[if lte IE 6]>
<?php echo stylesheet_tag('my-css-for-IE6-or-lower') ?>
<![endif]-->
</head>
I made a helper for this, so your header will look more like this :
<?php use_helper('IE') ?>
<?php use_ie_stylesheet('my-css-for-IE7', 'version=7') ?>
<?php use_ie_stylesheet('my-css-for-IE6-or-lower', 'version=6 operator=lte') ?>
...
<head>
<?php include_http_metas() ?>
<?php include_metas() ?>
<?php include_title() ?>
<link rel="shortcut icon" href="/favicon.ico" />
<?php include_javascripts() ?>
<?php include_stylesheets() ?>
<?php include_ie_metas() ?>
</head>
Code is not shorter, but it's more "symfony-like" as you don't explicitly write yourself the strange-syntax comments and don't have to remember their (strange) syntax anymore.
The ideal way would be adding a filter inspired by "lib/symfony/sfCommonFilter.class.php" so we would not even have to call the include_* functions ;)
IMPORTANT :
If you do this (using IE helper or not) to OVERRIDE some CSS rules, don't forget that Symfony automagically adds your CSS and JS files just before </head>, so your CSS will be inserted BEFORE all the CSS included via "use_stylesheet()". So you have to call
<?php include_stylesheets() ?> <?php include_javascripts() ?>
before adding your conditional comments.
And the code of the helper, to be written in "lib/helper/IEHelper.php"
<?php function get_opening_ie_conditional_comments($version = null, $operator = null) { return '<!--[if ' . ($version ? $operator.' ' : '') . 'IE' . ($version ? ' '.$version : '') . ']>'; } function get_closing_ie_conditional_comments($version = null, $operator = 'lte') { return '<![endif]-->'; } function get_ie_conditional_comments($version = null, $operator = null) { return array( get_opening_ie_conditional_comments($version, $operator), get_closing_ie_conditional_comments($version, $operator) ); } function get_ie_metas($type = null) { if ($type == 'javascripts' || $type == 'stylesheets') { $metas = ''; $already_seen = array(); // Stylesheets $assets = sfContext::getInstance()->getResponse()->getParameter($type, array(), 'ie'); foreach ($assets as $asset_info) { list($asset, $options) = $asset_info; $file = $type == 'stylesheets' ? stylesheet_path($asset) : javascript_path($asset); if (isset($already_seen[$file])) { continue; } else { $already_seen[$file] = true; } // IE version (default : none) $version = isset($options['version']) ? $options['version'] : null; unset($options['version']); // IE version comparison operator (default : "le") $operator = isset($options['operator']) ? $options['operator'] : null; unset($options['operator']); // get corresponding comments list($start_comment, $end_comment) = get_ie_conditional_comments($version, $operator); $include = $type == 'stylesheets' ? stylesheet_tag($file, $options) : javascript_include_tag($file, $options); $metas .= $start_comment . "\n" . $include . $end_comment . "\n"; } return $metas; } elseif (is_null($type)) { return get_ie_metas('javascripts') . "\n" . get_ie_metas('stylesheets'); } } function include_ie_metas() { echo get_ie_metas(); } function use_ie_stylesheet($stylesheet, $options = array()) { $stylesheets = sfContext::getInstance()->getResponse()->getParameter('stylesheets', array(), 'ie'); if (!is_array($options)) $options = sfToolkit::stringToArray($options); $stylesheets[] = array($stylesheet, $options); sfContext::getInstance()->getResponse()->setParameter('stylesheets', $stylesheets, 'ie'); } function include_ie_stylesheets() { echo get_ie_stylesheets(); } function get_ie_stylesheets() { return get_ie_metas('stylesheets'); } function use_ie_javascript($js, $options = array()) { $javascripts = sfContext::getInstance()->getResponse()->getParameter('javascripts', array(), 'ie'); if (!is_array($options)) $options = sfToolkit::stringToArray($options); $javascripts[] = array($javascript, $options); sfContext::getInstance()->getResponse()->setParameter('javascripts', $javascripts, 'ie'); } function include_ie_javascripts() { echo get_ie_javascripts(); } function get_ie_javascripts() { return get_ie_metas('javascripts'); }
I use this small bash script to automatically detect /plugins/*/web directories and symlink them in /web folder.
This is particularly useful for SVN installations of plugins, which do not handle this for you.
#!/bin/bash if [ ! -d web -o ! -d plugins ] then echo "You must be in a Symfony project root" exit 1 fi # Create links from /web directory cd web # List plugins' web directories ls -d "../plugins/*/web" | while read plugin_web do plugin=$(basename $(dirname $plugin_web)) if [ ! -d $plugin ] then ln -f -s -v $plugin_web $plugin fi done # Go back to project's root cd ..
You could use a "compact" version as an alias ;)
alias ln_plugins_web="cd web && ls ../plugins/*/web | while read plugin_web; do plugin=$(basename $(dirname $plugin_web)); if [ ! -d $plugin ]; then ln -f -s -v $plugin_web $plugin; fi; done; cd .."
Note : if your bash installation does not provide "basename" and "dirname" commands, you can add them manually with those aliases
function dirname() { echo "$1" | sed 's/\/[^\/]*\(\/*\)\?$//'; } function basename() { echo "$1" | sed 's/^.*\/\([^\/]\+\)\/*$/\1/ }
I share here the script I use to initialize a new Symfony project, as I always subversion them.
This script handles the following default scenario :
You can customize the behaviour of the script defining environment variables. This is an unusual way to do, but it has two advantages :
Here is what you can change in the default scenario :
Here is what is always done, and the really goal of this script (to simplify your life) :
Full usage :
Usage : new-symfony-project PROJECT-NAME You can define following variables to customize the creation : - SYMFONY_BRANCH : The Symfony branch used - SYMFONY_REPOS : The full Symfony SVN repository URL, if set, SYMFONY_BRANCH is ignored - SYMFONY_DATA : The path to the data folder of your Symfony installation. If not left empty, no SVN version will be downloaded (using local installation) and two precedent variables will be ignored - TRUNK_URL : The trunk of your project (default : repos created) - SVNADMIN_REPOS : Local root for SVN repositories - WEB_DIR : Local root for Web directories
Examples :
# Start a new project using default options $ new-symfont-project MyProject # Start a new project embedding 1.1 Symfony branch $ SYMFONY_BRANCH=1.1 new-symfont-project MyProject # Start a new project embedding 1.1 Symfony branch and versionned in a remote repository (already initialized) $ SYMFONY_BRANCH=1.1 TRUNK_URL=my-svn-host/my-svn-repos/trunk new-symfont-project MyProject # Start a new project using system-wide install $ SYMFONY_DATA=/usr/share/php/data new-symfont-project MyProject
Here is the code, you must customize the first 5 lines to fit your default environment options :
#!/bin/bash DEFAULT_WEB_DIR=/home/naholyr/www DEFAULT_SVNADMIN_REPOS=/home/naholyr/svn DEFAULT_SYMFONY_BRANCH=1.0 PROJECT=$1 if [ "$PROJECT" = "" ]; then # Show help and exit echo "Usage : new-symfony-project PROJECT-NAME" echo "You can define following variables to customize the creation :" echo "- SYMFONY_BRANCH : The Symfony branch used (default : $DEFAULT_SYMFONY_BRANCH)" echo "- SYMFONY_REPOS : The full Symfony SVN repository URL, if set, SYMFONY_BRANCH is ignored" echo "- SYMFONY_DATA : The path to the data folder of your Symfony installation. If not left" echo " empty, no SVN version will be downloaded (using local installation) and two precedent" echo " variables will be ignored" echo "- TRUNK_URL : The trunk of your project (default : repos created)" echo "- SVNADMIN_REPOS : Local root for SVN repositories (default : $DEFAULT_SVNADMIN_REPOS)" echo "- WEB_DIR : Local root for Web directories (default : $DEFAULT_WEB_DIR)" fi if [ "$SVNADMIN_REPOS" = "" ]; then SVNADMIN_REPOS=$DEFAULT_SVNADMIN_REPOS fi if [ "$WEB_DIR" = "" ]; then WEB_DIR=$DEFAULT_WEB_DIR fi if [ "$SYMFONY_BRANCH" = "" ]; then SYMFONY_BRANCH=$DEFAULT_SYMFONY_BRANCH fi if [ "$SYMFONY_REPOS" = "" ]; then SYMFONY_REPOS=http://svn.symfony-project.com/branches/$SYMFONY_BRANCH fi if [ "$TRUNK_URL" = "" ]; then SVN_REPOS="file://$SVNADMIN_REPOS" SVN_PROJECT=$SVN_REPOS/$PROJECT # Create repository svnadmin create $SVNADMIN_REPOS/$PROJECT svn mkdir -m "Standard structure" $SVN_PROJECT/trunk $SVN_PROJECT/branches $SVN_PROJECT/tags svn mkdir -m "Create folder for Symfony" $SVN_PROJECT/trunk/lib $SVN_PROJECT/trunk/lib/vendor TRUNK_URL=$SVN_PROJECT/trunk fi # Create web folder cd $WEB_DIR svn co $TRUNK_URL $PROJECT cd $PROJECT if [ "$SYMFONY_DATA" = "" ]; then # Download Symfony svn propset svn:externals "symfony $SYMFONY_REPOS" lib/vendor svn commit -m "External link to Symfony" svn update SYMFONY_DATA=$(pwd)/lib/vendor/symfony/data fi # Create Symfony project $SYMFONY_DATA/bin/symfony init-project $PROJECT # Link to Symfony web elements ln -s $SYMFONY_DATA/web/sf web/sf # Commit the new project svn status | grep ^? | sed "s/? *//" | xargs svn add svn mkdir lib/model/om lib/model/map svn propset svn:ignore "*" log svn propset svn:ignore "*" cache svn propset svn:ignore "*schema-transformed.xml" config svn propset svn:ignore "lib.model.schema.sql plugins.*.sql sqldb.map" data/sql svn propset svn:ignore "*" lib/model/om svn propset svn:ignore "*" lib/model/map svn commit -m "Project initialized" # Conclusion ./symfony -V echo "Working directory : $WEB_DIR/$PROJECT" echo "You should be ready to go :)"
Note that if you work this way, intensively take advantage of Subversion and install plugins as svn:externals ;)