CodeIgniter Forums
Title in View or in Controller [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Title in View or in Controller [SOLVED] (/showthread.php?tid=82241)



Title in View or in Controller [SOLVED] - InstantT - 06-25-2022

Hello all,
I wonder what is the best way to display the Title tag.

Is it better to display it in the View or in the Controller?

Controller Example

PHP Code:
$data = [
    'meta_title' => "Blog Post ".$post_name,
];

return 
view('my-view'$data); 

PHP Code:
<?php if(isset($meta_title) && !empty($meta_title)): ?>
    <?= '<title>'.esc($meta_title).'</title>'?>
<?php 
endif; ?>

But I wonder if it's better to fill in the Title tags in the Views with renderSection

PHP Code:
<title><?= $this->renderSection('title'?></title> 

PHP Code:
<?= $this->section('title'?> Blog Post <?= esc($post_name); ?> <?= $this->endSection() ?>

What is the best option?

Thank you in advance for your answer.


RE: Title in View or in Controller - InsiteFX - 06-26-2022

It depends on the title, is it ever going to change?
if not then hard code it into the view.
else save in database and assin it to $data array and pass it to the view.


RE: Title in View or in Controller - demyr - 06-26-2022

@InsiteFX is right. It depends but I would go with the first option but not typing this way

PHP Code:
<?php if(isset($meta_title) && !empty($meta_title)): ?>
    <?= '<title>'.esc($meta_title).'</title>'?>
<?php 
endif; ?>


but shortly this way

PHP Code:
<title><?php echo $title ?? 'No title' ?></title> 



RE: Title in View or in Controller - kenjis - 06-26-2022

Don't forget `esc()`.


RE: Title in View or in Controller - InstantT - 06-27-2022

Hello,

Thank you for your reply ?

So I will continue to use the Controller method to manage Meta tags