Your Ad Here

Thursday, June 12, 2014

Laravel Building HTML

Building HTML

Content


Entities

When displaying user input in your Views, it is important to convert all characters which have significance in HTML to their "entity" representation.
For example, the < symbol should be converted to its entity representation. Converting HTML characters to their entity representation helps protect your application from cross-site scripting:

Converting a string to its entity representation:

  1. echo HTML::entities('<script>alert(\'hi\');</script>');

Using the "e" global helper:

  1. echo e('<script>alert(\'hi\');</script>');

Scripts And Style Sheets

Generating a reference to a JavaScript file:

  1. echo HTML::script('js/scrollTo.js');

Generating a reference to a CSS file:

  1. echo HTML::style('css/common.css');

Generating a reference to a CSS file using a given media type:

  1. echo HTML::style('css/common.css', array('media' => 'print'));
Further Reading:

Links

Generating a link from a URI:

  1. echo HTML::link('user/profile', 'User Profile');

Generating a link that should use HTTPS:

  1. echo HTML::link_to_secure('user/profile', 'User Profile');

Generating a link and specifying extra HTML attributes:

  1. echo HTML::link('user/profile', 'User Profile', array('id' => 'profile_link'));

Links To Named Routes

Generating a link to a named route:

  1. echo HTML::link_to_route('profile');

Generating a link to a named route with wildcard values:

  1. $url = HTML::link_to_route('profile', 'User Profile', array($username));
Further Reading:

Links To Controller Actions

Generating a link to a controller action:

  1. echo HTML::link_to_action('home@index');

Generating a link to a controller action with wildcard values:

  1. echo HTML::link_to_action('user@profile', 'User Profile', array($username));

Links To A Different Language

Generating a link to the same page in another language:

  1. echo HTML::link_to_language('fr');

Generating a link to your home page another language

  1. echo HTML::link_to_language('fr', true);

Mail-To Links

The "mailto" method on the HTML class obfuscates the given e-mail address so it is not sniffed by bots.

Creating a mail-to link:

  1. echo HTML::mailto('example@gmail.com', 'E-Mail Me!');

Creating a mail-to link using the e-mail address as the link text:

  1. echo HTML::mailto('example@gmail.com');

Images

Generating an HTML image tag:

  1. echo HTML::image('img/smile.jpg', $alt_text);

Generating an HTML image tag with extra HTML attributes:

  1. echo HTML::image('img/smile.jpg', $alt_text, array('id' => 'smile'));

Lists

Creating lists from an array of items:

  1. echo HTML::ol(array('Get Peanut Butter', 'Get Chocolate', 'Feast'));
  2.  
  3. echo HTML::ul(array('Ubuntu', 'Snow Leopard', 'Windows'));
  4.  
  5. echo HTML::dl(array('Ubuntu' => 'An operating system by Canonical', 'Windows' => 'An operating system by Microsoft'));

Custom Macros

It's easy to define your own custom HTML class helpers called "macros". Here's how it works. First, simply register the macro with a given name and a Closure:

Registering a HTML macro:

  1. HTML::macro('my_element', function()
  2. {
  3. return '<article type="awesome">';
  4. });
Now you can call your macro using its name:

Calling a custom HTML macro:

  1. echo HTML::my_element();