Welcome Guest, Not a member yet? Register   Sign In
Title in View or in Controller [SOLVED]
#1
Information 
(This post was last modified: 06-27-2022, 07:05 AM by InstantT.)

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.
--- I am not a developer ---
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

@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> 
Reply
#4

Don't forget `esc()`.
Reply
#5

Hello,

Thank you for your reply ?

So I will continue to use the Controller method to manage Meta tags
--- I am not a developer ---
Reply




Theme © iAndrew 2016 - Forum software by © MyBB