Drupal link function
The Drupal link function is a handy and easy way to create hyperlinks in your module or theme.
The syntax for the link function is:
- print l(<em>anchor text</em>, <em>path</em>);
This is an example of a simple link that might exist in a theme template file:
- print l('See full post', 'node/'.$node->nid);
The above example would create the following hyper link (assuming that the given $node->nid is 1):
You can easily add attributes to the link.
For example, lets say you want to add a class to your anchor link called "see-more" and
you want to add target="_blank"
- $attributes = array( 'class' => 'see-more', 'target' => '_blank' );
- print l('See full post', 'node/'.$node->nid, $attributes);
- $options['attributes'] = array( 'class' => 'see-more', 'target' => '_blank' );
- print l('See full post', 'node/'.$node->nid, $options);
The above examples would create the following hyper link:
If the link is external, then use the full URL including the http://.
- print l('Google', 'http://www.google.com');
This would create:
It is generally easier to create a hyper link using the link l() function that typing out the full html for an anchor link.
Using the link function is more secure because check_plain and check_url will run automatically, which helps prevent xss attacks.
And finally, active classes are automatically assigned when using the link function.
For more information, see the Drupal API page on the Drupal link function
New comments for this tutorial have been turned off.