Ama*_*nut 29 php template-engine laravel blade laravel-4
如果您查看laravel官方文档http://laravel.com/docs/4.2/templates 它说给出这个布局:
<!-- Stored in app/views/layouts/master.blade.php -->
<html>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
通过这种观点扩展
@extends('layouts.master')
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@stop
@section('content')
<p>This is my body content.</p>
@stop
Run Code Online (Sandbox Code Playgroud)
将附加到该部分sidebar.但实际上如果你尝试它不会附加,它只是覆盖扩展模板中的内容.
我听说过其他刀片功能@append, @prepend, @parent......似乎没有人工作.
除此之外,官方文档中的这个例子不起作用,我发现刀片文档非常差.例如@parent,刀片功能没什么.
Bog*_*dan 49
Larvel网站文档中的示例确实看起来确实存在缺陷,但我认为这是网站上的降价解析问题,github上的相同文档显示正确的代码:
无论如何@parent确实有效.文档中的示例应如下所示:
@extends('layouts.master')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@stop
@section('content')
<p>This is my body content.</p>
@stop
Run Code Online (Sandbox Code Playgroud)
快速浏览Illuminate/View/Factory.php确认了什么@parent:
/**
* Append content to a given section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendSection($section, $content)
{
if (isset($this->sections[$section]))
{
$content = str_replace('@parent', $content, $this->sections[$section]);
}
$this->sections[$section] = $content;
}
Run Code Online (Sandbox Code Playgroud)
use*_*364 20
你可以简单地使用@append......
@extends('layouts.master')
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@append
@section('content')
<p>This is my body content.</p>
@stop
Run Code Online (Sandbox Code Playgroud)
看到这里.
要了解这是如何工作的......
将compileStatements()在BladeCompiler方法调用的方法compileAppend(),你可以在这里看到:
/**
* Compile Blade Statements that start with "@"
*
* @param string $value
* @return mixed
*/
protected function compileStatements($value)
{
$callback = function($match)
{
if (method_exists($this, $method = 'compile'.ucfirst($match[1])))
{
$match[0] = $this->$method(array_get($match, 3));
}
return isset($match[3]) ? $match[0] : $match[0].$match[2];
};
return preg_replace_callback('/\B@(\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $callback, $value);
}
Run Code Online (Sandbox Code Playgroud)
反过来,插入一个如下所示的调用appendSection():
/**
* Stop injecting content into a section and append it.
*
* @return string
*/
public function appendSection()
{
$last = array_pop($this->sectionStack);
if (isset($this->sections[$last]))
{
$this->sections[$last] .= ob_get_clean();
}
else
{
$this->sections[$last] = ob_get_clean();
}
return $last;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37716 次 |
| 最近记录: |