ARCANEDEV / SEO-Helper

:mag: SEO Helper is a package that provides tools and helpers for SEO (Search Engine Optimization).
MIT License
322 stars 49 forks source link

get only title string #37

Closed Themesfa closed 6 years ago

Themesfa commented 6 years ago

hi . i want to get site title and use in other place on page layout . how i should get title string without meta html ?

arcanedev-maroc commented 6 years ago

What's the output you're getting ?

Or you want to do something like this ?

<h1>{{ seo_helper()->meta()->getTitle() }}</h1>
Themesfa commented 6 years ago

my codes :

UserController.php

public function show() 
{
    $this->seo()->setTitle('Users','Themesfa','&lsaquo;');

    return view( 'users' );
}

users.blade.php

<head>
    {!! seo_helper()->render() !!}
</head>
<body>
    ...
    ...
    <div class="page-header"><h1>"I want to print page title here"</h1></div>
</body>

the output in head is => Users ‹ Themesfa and its ok . but i want to use title in h1 tag again. how i can do it ? i cant find any method to help me in this case .

arcanedev-maroc commented 6 years ago

There is a workaround for this:

UserController.php

public function show() 
{
    $this->seo()->setTitle($title = 'Users','Themesfa','&lsaquo;');

    return view('users', compact('title'));
}

users.blade.php

<head>
    {{ seo_helper()->renderHtml() }}
</head>
<body>
    ...
    ...
    <div class="page-header"><h1>{{ $title or 'Default title' }}</h1></div>
</body>

I'm going to think about this feature if doesn't exists 👍

Themesfa commented 6 years ago

it works, but Have any idea how to make it more beautiful? i think you must create a method and return all seo Variables . so users can access seo Variables in blade.

arcanedev-maroc commented 6 years ago

In my projects, i always extend my base controller to make things beautiful and organized, something like this:

Controller.php (using Arcanedev\SeoHelper\Traits\Seoable)

public function setTitle($title, $siteName = null, $separator = null)
{
    view()->share('title', $title);

    return parent::setTitle($title, $siteName, $separator);
}

UserController.php

public function show()
{
    $this->setTitle('Users','Themesfa','&lsaquo;');

    return view('users');
}

users.blade.php

<head>
    {{ seo_helper()->renderHtml() }}
</head>
<body>
    ...
    ...
    <div class="page-header"><h1>{{ $title or 'Default title' }}</h1></div>
</body>
Themesfa commented 6 years ago

you right👍 . i'm new in laravel . forgive me thank you for your help.

arcanedev-maroc commented 6 years ago

PR: #38

Now you can access the title entity with: seo_helper()->meta()->getTitleEntity()->getTitleOnly()