2013-07-30 124 views
0

我写这依赖于每一页以下结构的刀片模板一个静态文件基于页面管理系统@section(“标题”)的内容:如何抓住

@extends('en/frontend/layouts/page_section') 
{{-- Page Title --}} 
@section('title') 
Current Page Title 
@parent 
@stop 

{{-- Page content --}} 
@section('pageContent') 
... 
@stop 

,这样我可以下拉链接到本节中的所有页面我想只抓取@section('title')内的内容。

我知道这是可能的,我只是不知道如何。

请指教,谢谢。

编辑为了澄清,我想视图加载到我的控制器和抢的章节标题的值,如下面的伪代码

Class Controller{ 

    // would return string 'Current Page Title' for above example 
    public function getTitle($viewSlug) 
    { 
     $view = View::make($viewSlug); 
     return $view->section('title'); 
    } 
} 

回答

0

我库仑没有找到一种使用View或甚至直接使用刀片引擎返回部分内容的方式。因此,我决定丑陋的蛮力方法是唯一的方法,因为这只是在flatfile => db转换器中使用,它只能每年运行几次,所以速度不是必需的。

function getTitle($viewSlug) 
{ 
    // get path to view file 
    $viewPath = \View::make($viewSlug) 
     ->getPath(); 

    $view = \File::get($viewPath); 
    // flatten view as we dont know how to use regex without this 
    $view = str_replace("\n", '', $view); 

    $titleRegex = "/@section\('title'\)(.*)@parent/"; 
    preg_match($titleRegex, $view, $matches); 
    return $matches; 
} 
1

在您的布局:

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

在你看来:

@section('title', 'Your title here') 
+0

啊,我注意到我的问题中令人困惑的语言。我已经通过澄清进行了编辑。我需要做的是通过控制器中的一些机制获取视图中定义的@section('title')的内容。这样,我的撰稿人可以编辑文本视图文件,我可以运行我的导入命令来更新页面标题等数据库。谢谢。 – carbontwelve