简体   繁体   English

刀片模板,@ yield中的@yield()

[英]blade template, @yield() in @yield

This is my current implementation of the Open Graph tags using Laravel 5: 这是我使用Laravel 5的Open Graph标签的当前实现:

app.blade.php app.blade.php

<title>@yield('title')</title>
<meta property="og:title" content="@yield('og-title', 'DEFAULT PAGE TITLE')">
<meta property="og:image" content="@yield('og-image', 'default.png')">
<meta property="og:url" content="@yield('og-url', '{{ Request::url() }}')">
<meta property="og:site_name" content="SITE NAME">

page.blade.php page.blade.php

@extends('app')
@section('title', $article->title . ' | SITE NAME')
@section('og-title', $article->title)
@section('og-image', secure_url('img/news/' . $article->image .'.png'))

It works for the most part, but I have a couple inquiries: 它大部分都有效,但我有几个问题:

  1. Is there a way to use @yield() in @yield ? 有没有办法在@yield使用@yield()

    I tried <meta property="og:title" content="@yield('og-title', @yield('title'))"> but it did not work. 我尝试了<meta property="og:title" content="@yield('og-title', @yield('title'))">但它没有用。

  2. How can I get the current SECURE url of the page? 如何获取页面的当前SECURE URL?

    {{ Request::url() }} returns http://example.com/page , but I want https://example.com/page {{ Request::url() }}返回http://example.com/page ,但我想要https://example.com/page

As a meta, please let me know if you have any suggestions to improve my current method of OG. 作为元数据,如果您有任何建议可以改进我目前的OG方法,请告诉我。

There is an easy way to achieve what you want. 有一种简单的方法可以达到你想要的效果。 What you can do is to move the OG meta tags html to partial view, to which you can pass parameters on each page as you wish. 你可以做的是将OG元标记html移动到局部视图,你可以根据需要在每个页面上传递参数。 For example in your master layout just create section for the og tags: 例如,在主布局中,只需为og标记创建部分:

app.blade.php app.blade.php

<title>@yield('title')</title>
@section('ogtags')

@show

Now create your partial view let's call it: 现在创建您的局部视图让我们称之为:

og_tags.blade.php og_tags.blade.php

<meta property="og:title" content="{{ $title or 'DEFAULT PAGE TITLE' }}">
<meta property="og:image" content="{{ $image or 'default.png' }}">
<meta property="og:url" content="{{ isset($url) ? $url : str_replace('http://', 'https://', Request::url()) }}">

So now in you views you can easily add og-tags like this: 所以现在你可以在视图中轻松添加og-tags,如下所示:

home.blade.php home.blade.php

@extends('app')

@section('ogtags')
    @include('og_tags', ['title' => 'my title', 'image' => 'my-image.png'])
@stop

@section('content')
     your content here
@stop

For the secure url question, Request::url() returns the current url of the page, if it is via HTTPS it will start with https:// otherwise it will start with http:// . 对于安全URL问题, Request::url()返回页面的当前url,如果是通过HTTPS,它将以https://开头,否则将以http://开头。 So this is why I just replace it to always be https 所以这就是我将其替换为始终为https的原因

str_replace('http://', 'https://', Request::url())

If you wish to always have og_tags (in case your view does not define this section and you want to show default ones) you can just modify your app.blade.php like this: 如果您希望始终拥有og_tags(如果您的视图未定义此部分并且您想显示默认部分),您可以像这样修改app.blade.php

<title>@yield('title')</title>
@section('ogtags')
    @include('og_tags')
@show

This is the cool part of Blade that you can split everything in smaller parts, then include them with dynamic parameters, or just create view composers to handle the data. 这是Blade的一个很酷的部分,您可以将所有内容拆分为较小的部分,然后使用动态参数包含它们,或者只创建视图编辑器来处理数据。

I always use a meta.blade.php and keep my meta separate from the main layout and not only meta but also, styles and scripts when possible and include then using @include(...) , anyways, but how to make it dynamic? 我总是使用meta.blade.php并将我的meta与主要布局分开,不仅仅是meta ,还有stylesscripts如果可能的话,包括然后使用@include(...) ,但是如何让它变得动态? In this case i use a similar approach like you but a little differently which let me full flexibility to make it real dynamic. 在这种情况下,我使用类似于你的方法,但有点不同,这让我充分灵活地使它真正动态。 Here is how I do it, probably it'll answer what you are seeking here. 我就是这样做的,可能它会回答你在这里寻求的东西。 So, let's create a 'meta.blade.php' file and mine is given below: 那么,让我们创建一个'meta.blade.php'文件,我的文件如下:

<!-- Title-->
<title>@yield('title', smart('site.title'))</title>
<!-- Meta-->
<meta name="keywords" content="@yield('keywords', getSiteMeta('keywords'))" />
<meta name="description" content="@yield('description', getSiteMeta('description'))" />
<meta name="author" content="@yield('author', getSiteMeta('author'))" />
<!-- CSRF for AJAX-->
<meta name="csrf-token" content="{{ csrf_token() }}" />

Notice @yield('keywords', getSiteMeta('keywords')) so here; 注意@yield('keywords', getSiteMeta('keywords'))所以这里; the getSiteMeta is a helper function which is declared in my app\\Helpers\\Common.php file and the function looks like this: getSiteMeta是一个帮助函数,在我的app\\Helpers\\Common.php文件中声明,函数如下所示:

/**
 * Prapare meta Tags by $name
 * 
 * @param  String $metaname Meta Name
 * @return Meta Content
 */
function getSiteMeta($name)
{
    return smart("site.meta.$name");
}

Also another function is involved here, which is: 这里还涉及另一个功能,即:

/**
 * Short cut to config call for smart.php file items
 * @param  String $str Config Key
 * @return String Config Value
 */
function smart($str)
{
    return config("smart.$str");
}

Actually, I've a config file of my own at config\\smart.php and smart is my site name so I've kept all of my site related configs in config\\smart.php which looks like this: 实际上,我在config\\smart.php有一个我自己的配置文件, smart是我的站点名称所以我在config\\smart.php保存了所有与站点相关的配置,如下所示:

// config/smart.php
return [

    'site' => [
        'name' => 'Smart',
        'title' => 'Smart Title',
        'tagline' => 'Smart Tag Line',
        'meta' => [
            'keywords' => 'Smart Keywords',
            'language' => 'english',
            'description' => '...',
            'author' => 'Sheikh Heera'
        ],
    ],

    'role' => [
        'default' => 'genaral',  # By default, a user gets this role when registers.
        'reserved' => 'superuser' # Application's Admin must have this role assigned.
    ],
    // More...
];

Probably you got the idea, I can call smart('key_name') to get configs of my site so I don't need to use config('smart.key') and by default all the meta values are stored in my config/smart.php file so if I don't use a section like this: 可能你有这个想法,我可以调用smart('key_name')来获取我的网站的配置,所以我不需要使用config('smart.key') ,默认情况下所有的meta值都存储在我的config/smart.php文件,所以如果我不使用这样的部分:

@section('keywords')
    Some, Keywords, Here
@stop

Then I've always my default settings and by using a helper function getSiteMeta I have enough flexibility to change the logic of my function, for example, I can change the default source of my meta tags and can build it on the fly. 然后我总是使用我的默认设置并使用辅助函数getSiteMeta我有足够的灵活性来更改我的函数的逻辑,例如,我可以更改我的meta标记的默认源并可以动态构建它。

Regarding https it depends on your site, if you have used secured url ( ssl/https ) and Request::url() just returns what your current url is, so if it's https then it'll return that otherwise you'll get http . 关于https它取决于你的网站,如果你使用了安全网址( ssl/https ), Request::url()只返回你当前的网址,所以如果它是https然后它会返回,否则你会得到http

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM