HTML Helper in CodeIgniter 4 Tutorial

Reading Time: 8 minutes
4,457 Views

Helpers are the standalone functions which helps to complete specific task. There are several pre defined helpers available in CodeIgniter 4.

Here, you can find article over these helpers as well –

You can find complete details of each.

Inside this article we will discuss the concept of HTML helper in CodeIgniter 4 and its’s available functions.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.


Loading HTML Helper into Application

We can load HTML helper in CodeIgniter 4 in two different ways. Methods to load by –

  • By BaseController.php [parent controller]
  • Loading into any specific controller

Loading by Parent Controller

Open BaseController.php file from /app/Controllers folder and search for $helpers. Add helper into it.

protected $helpers = ["html"];

Loading into Specific Controller

Open any application controller, go inside method and write this code to load html helper.

helper("html");

OR

helper(["html"]);

HTML Helper Functions

HTML helper provides many functions which helps to create image tag, link stylesheet file, add script tag, define doctype, create ul and li list etc. This helper is going to provide the functions which helps to work with HTML tags.

Let’s see few functions from list.

  • img()
  • link_tag()
  • script_tag()
  • ul()
  • ol()
  • video()
  • audio()
  • embed()
  • doctype()

HTML Helper Functions – Details

Now, we will see the detailed concept about each functions what we discussed above.

img()

This function creates HTML <img /> tags.

Syntax –

img([$src = ''[, $indexPage = false[, $attributes = '']]])

Example –

Example #1

echo img('images/picture.jpg');

Example #2

$imageProperties = [    
    'src'    => 'images/picture.jpg',    
    'alt'    => 'Me,how to eat 4 slices of pizza at one time',    
    'class'  => 'post_images',    
    'width'  => '200',     
   'height' => '200',     
   'title'  => 'That was quite a night',    
    'rel'    => 'lightbox' 
 ];

echo img($imageProperties);

It generates a image tag which is going to link a image file picture.jpg of example 1 from /public/images folder.

Generated HTML

Output #1

<img src="http://site.com/images/picture.jpg" />

Output #2

<img src="http://site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a night" rel="lightbox" />

link_tag()

This function creates HTML <link /> tags. This is useful for stylesheet links, as well as other links.

Syntax –

link_tag([$href = ''[, $rel = 'stylesheet'[, $type = 'text/css'[, $title = ''[, $media = ''[, $indexPage = false[, $hreflang = '']]]]]]])

Example –

Example #1

echo link_tag('css/mystyles.css');

Example #2

$link = [    
   'href'  => 'css/printer.css',    
   'rel'   => 'stylesheet',     
  'type'  => 'text/css',     
  'media' => 'print' 
]; 

echo link_tag($link);

It generates a link tag which is going to link a css file mystyles.css of example 1 from /public/css folder.

Generated HTML

Output #1

<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />

Output #2

<link href="http://site.com/css/printer.css" rel="stylesheet" type="text/css" media="print" />

script_tag()

This function creates HTML <script></script> tags.

Syntax –

script_tag([$src = ''[, $indexPage = false]])

Example –

Example #1

echo script_tag('js/mystyles.js');

Example #2

$script = ['src'  => 'js/printer.js'];

echo script_tag($script);

It generates a link tag which is going to link a js file mystyles.js of example 1 from /public/js folder.

Generated HTML

Output #1

<script src="http://site.com/js/mystyles.js" type="text/javascript"></script>

Output #2

<script src="http://site.com/js/printer.js" type="text/javascript"></script>

ul()

This function generates unordered HTML lists from simple or multi-dimensional arrays.

Syntax –

ul($list[, $attributes = ''])

Example –

$list = ['red', 'blue', 'green', 'yellow'];

$attributes = ['class' => 'boldlist', 'id'=> 'mylist'];

echo ul($list, $attributes);

Generated HTML

<ul class="boldlist" id="mylist">     
   <li>red</li>     
   <li>blue</li>     
   <li>green</li>     
   <li>yellow</li> 
</ul>

ol()

This function generates ordered HTML lists from simple or multi-dimensional arrays. It produces the <ol> tag for ordered lists instead of <ul>.

Syntax –

ol($list, $attributes = '')

Example –

$list = ['red', 'blue', 'green', 'yellow'];

$attributes = ['class' => 'boldlist', 'id'=> 'mylist'];

echo ol($list, $attributes);

Generated HTML

<ol class="boldlist" id="mylist">     
   <li>red</li>     
   <li>blue</li>     
   <li>green</li>     
   <li>yellow</li> 
</ol>

video()

Permits you to generate HTML video element from simple or source arrays. 

Syntax –

video($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])

Example –

Example #1

echo video('test.mp4', 'Your browser does not support the video tag.', 'controls');

Example #2

$tracks = [     
   track('subtitles_no.vtt', 'subtitles', 'no', 'Norwegian No'),     
   track('subtitles_yes.vtt', 'subtitles', 'yes', 'Norwegian Yes') 
];

echo video (
 [       
    source('movie.mp4', 'video/mp4', 'class="test"'),       
    source('movie.ogg', 'video/ogg'),       
    source('movie.mov', 'video/quicktime'),       
    source('movie.ogv', 'video/ogv; codecs=dirac, speex')     
 ],     
 'Your browser does not support the video tag.',    
 'class="test" controls',    
 $tracks  
);

It generates a video tag which is going to link a mp4 file test.mp4 of example 1 from /public folder.

Generated HTML

Output #1

<video src="test.mp4" controls>   
    Your browser does not support the video tag. 
</video>

Output #2

<video class="test" controls>   
   <source src="movie.mp4" type="video/mp4" class="test" />   
   <source src="movie.ogg" type="video/ogg" />   
   <source src="movie.mov" type="video/quicktime" />   
   <source src="movie.ogv" type="video/ogv; codecs=dirac, speex" />   
   <track src="subtitles_no.vtt" kind="subtitles" srclang="no" label="Norwegian No" />   
   <track src="subtitles_yes.vtt" kind="subtitles" srclang="yes" label="Norwegian Yes" />   
   Your browser does not support the video tag. 
</video>

doctype()

It helps to generate document type declarations, or DTD’s. HTML 5 is used by default

Syntax –

doctype([$type = 'html5'])

Example –

Example #1

echo doctype();

Example #2

echo doctype('html4-trans');

Generated HTML

Output #1

<!DOCTYPE html>

Output #2

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

We hope this article helped you to learn about HTML Helper in CodeIgniter 4 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more