Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "link"

CSS link class adder

<?php
 
/**
 * Automatically adds a class name to a link (or a parent tag) if the link route matches
 * the current module or route.
 * (based on the work made by scott meves on 2007-02-24 : http://snippets.symfony-project.org/snippet/153)
 *
 * @author  ESCANDELL Stéphane <stephane.escandell[a]gmail.com>
 * @date    2010-07-16
 *
 *
 * @param  <name> : string name of the link, i.e. string to appear between the <a> tags
 * @param  <internal_uri> string 'module/action' or '@rule' of the action
 * @param  <options> array additional style options
 * @return string XHTML compliant <a href> tag (encapsulated by an optionnal tag)
 * @see    link_to
 * 
 *
 *
 * <b>Available Options:</b>
 *    -> All classic options from link_to() method
 *
 *    -> 'moduleOnly'
 *      recognized values:
 *        -> true
 *        -> false
 *      If set to <true>, the link is identify as matched only if the module of the route associated to <internal_uri>
 *      is matching to the current module.
 *      If not set, the default value is false
 *
 *    -> 'currentClass'
 *      recognized values : 
 *        -> string 
 *      The class identifier added to the link (or the parent tag, see below) if <internal_uri> is matching to
 *      the current page.
 *      If not set, the default class added is 'active'.
 *
 *    -> 'separator'
 *      recognized values:
 *        -> string
 *      Used only if the <class> option is set. If the link match, this separator will be used between the <class>
 *      option and <currentClass> option (the generated class will be <class>.<separator>.<currentClass>).
 *      If not set, the default value is a space caracter ' '.
 *
 *    ->  'tag'
 *      recognized values:
 *        -> html tag name
 *      Identify an html tag to encapsulate the link. If this option appears, the <currentClass> name will be added
 *      to this tag rather than the <a> tag.
 *
 *    -> 'tagOptions'
 *      recognized values:
 *        -> array of html parameters
 *      Used only if the parameter <tag> exists. <tagOptions> is used to generate the tag to encapsulate the link with
 *      user specific options.
 *
 * <b>Examples:</b>
 * <code>
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="active" title="Home">home</a>
 * 
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'currentClass' => 'myActiveClass')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="myActiveClass" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'separator' => '_')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="active" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'class' => 'myClass tab', 'separator' => '_')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="myClass tab_active" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'currentClass' => 'myActiveClass',
 *                                 'tag' => 'li')); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="myActiveClass"><a href="path/to/homepage/action" title="Home">home</a></li>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home',
 *                                 'tag' => 'li',
 *                                  'tagOptions' => array('class' => 'anotherClass',
 *                                                        'width' => '250px')
 *                                )); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="anotherClass active" width="250px"><a href="path/to/homepage/action" title="Home">home</a></li>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home',
 *                                 'currentClass' => myActiveClass,
 *                                 'tag' => 'li',
 *                                  'tagOptions' => array('class' => 'anotherClass other',
 *                                                        'width' => '250px')
 *                                )); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="anotherClass other myActiveClass" width="250px"><a href="path/to/homepage/action" title="Home">home</a></li>
 * </code>
 *
 *
 * Changelog :
 *    2010/07/16 :
 *      Author : ESCANDELL Stéphane <stephane.escandell[a]gmail.com>
 *      Modification :
 *        First version (based on the work made by scott meves on 2007-02-24 : http://snippets.symfony-project.org/snippet/153)
 */
function link_to_styled($name, $internal_uri, $options = array())
{
  static $context = null;
  static $currentModuleName = null;
  static $applicationConfiguration = null;
 
  if(is_null($context))
  {
    $context = sfContext::getInstance();
  }
  if(is_null($currentModuleName))
  {
    $currentModuleName = $context->getModuleName();
  }
  if(is_null($applicationConfiguration))
  {
    $applicationConfiguration = $context->getConfiguration();
  }
 
  $options    = _parse_attributes($options);
  $separator  = isset($options['separator']) ? $options['separator'] : ' ';
  $tag        = isset($options['tag']) ? $options['tag'] : null;
  $tagOptions = isset($options['tagOptions']) ? $options['tagOptions'] : array();
 
  $isCurrent = false;
  // In order to findRoute() works, we need to delete the reference to
  // the file controller used
  //
  $route = $context->getRouting()->findRoute( str_replace('/'.$applicationConfiguration->getApplication().'_'.$applicationConfiguration->getEnvironment().'.php',
                                                      '',
                                                      url_for($internal_uri,false)
                                                      ));
  if(isset($options['moduleOnly']) && true===$options['moduleOnly'])
  {
    $isCurrent = isset($route['parameters']) ? (isset($route['parameters']['module']) ? $route['parameters']['module']==$currentModuleName : false) : false;
  }
  else
  {
    $isCurrent = url_for($internal_uri,false) == url_for($context->getRouting()->getCurrentInternalUri(),false);
  }
 
  if($isCurrent)
  {
    if(is_null($tag))
    {
      if(!isset($options['class']))
        $options['class'] = isset($options['currentClass']) ? $options['currentClass'] : 'active';
      else
        $options['class'] .= isset($options['currentClass']) ? $separator.$options['currentClass'] : $separator.'active';
    }
    else
    {
      if(!isset($tagOptions['class']))
        $tagOptions['class']  = isset($options['currentClass']) ? $options['currentClass'] : 'active';
      else
        $tagOptions['class'] .= isset($options['currentClass']) ? $separator.$options['currentClass'] : $separator.'active';
    }
  }
 
 
  unset($options['moduleOnly'],$options['currentClass'],$options['separator'],$options['tag'],$options['tagOptions']);
 
  if(is_null($tag))
    return link_to($name, $internal_uri, $options);
  else
    return content_tag($tag, link_to($name, $internal_uri, $options), $tagOptions);
}
 
by Stéphane Escandell on 2010-07-16, tagged css  helper  link  menu  navigation 
(2 comments)

Create Automatic Canonical Link Tags

<link rel="canonical" href="<?php echo url_for($sf_context->getRouting()->getCurrentInternalUri(true), true); ?>" />
 
by Erik Osterman on 2009-02-19, tagged canonical  link  tags 
(1 comment)

Using a link to submit a form or button to link

For ergonomic and graphism reasons, I sometimes prefer to use link rather than buttons and/or button rather than links...

To do so, I created this small function :

<?php
# File : JsHelper.php
 
// use of "normal" Javascript Helper
use_helper('Javascript');
 
 
/*!
 * Create a link that submit the closest parent form
 *
 * @param $string    string : text to display
 * @param $options mixed : options to be pass to the tag
 * @return string                : HTML code for the link
 */
function link_to_submit( $string, $options = null )
{
  $func = ";var a=this.ancestors();for(var i in a){if(a[i].tagName=='FORM'){try{a[i].onsubmit();} catch(err){a[i].submit();}break;}}";
  return link_to_function( $string, $func, $options );
}
 
 
 
/*!
 * Create a button that act as a link
 *
 * @param $string    string : text to display in the button
 * @param $url         string : URL of the link
 * @param $options mixed : options to be pass to the tag
 * @return string                : HTML code for the link
 */
function button_to_link( $string, $url, $options = null )
{
  $url = url_for($url);
 
  $func = ";document.location = '$url';return false;";
  if ( is_string($options) ) $options.= " onclick=$func";
  else $options['onclick'] = $func;
 
  return tag('button', $options ).htmlspecialchars($string).'</button>';
}
 

Then, you simply use it as a normal link_to function :

<? use_helper('Js') ?>
 
<?=form_tag('module/action')?>
  <?=input_tag('text')?>
  <?=link_to_submit('Validate')?>
  <?=link_to('Cancel','module/index')?>
</form>
 

Or :

<? use_helper('Js') ?>
 
<?=form_tag('module/action')?>
  <?=input_tag('text')?>
  <?=submit_tag('Validate')?>
  <?=button_to_link('Cancel','module/index')?>
</form>
 

In these two examples, links/buttons of the form are graphicly consistents. For a better user experience ;)

PS : works as well with AJAX forms form_remote_tag()

PPS : Don't forget that thanks to CSS, you can make a button looking like a link (et vice-versa).

PPPS : Don't forget that buttons and links doesn't have the same meaning for search engine bots.

PPPPS : Don't forget that this rely on the fact that user is using javascript!!!

by jugjug on 2008-03-26, tagged form  javascript  link  submit 
(2 comments)

Div to toggle and remote

This helper used JavascriptHelper. It create a div which open a another onclick with a visual effect. The content of the div is the template's result of a call to a module/action.

/**
 * Fonction my_div_to_remote
 * @author Julien Levasseur
 * @since - 13 sept. 07
 * Extend of my_button_to_remote. return a div.
 * 
 * Returns an html button to a remote action defined by 'url' (using the
 * 'url_for()' format) that's called in the background using XMLHttpRequest.
 *
 * See link_to_remote() for details.
 *
 */
function my_div_to_remote($name, $options = array(),$effect, $html_options = array())
{
  return my_div_to_function($name, $effect, remote_function($options), $html_options);
}
 
function my_div_to_function($name,$effect, $function, $html_options = array())
{
  $html_options = _parse_attributes($html_options);
  $html_options['onclick'] = $function.';'.$effect['onclickadd'].';return false;';
  return content_tag('div', $name, $html_options);
}
 

Example:

<?php echo my_div_to_remote('div de test',array('url'=>'principal/pret'),array('onclickadd' => visual_effect('toggle_blind', 'rem', array('duration' => 4.0))),array('style'=>'border: solid 1px;width:40px;height:40px;')) ?>
    <div id="rem" style="display:none;height:300px;border:solid 1px;">youpi</div>
 
by julien levasseur on 2007-09-14, tagged ajax  div  link  remote  toggle  visualeffect