2017-03-04 228 views
0

如何鏈接樹枝模板?事情是這樣的:如何鏈接href的樹枝模板?

<a href="Events.twig" class="btn btn-primary btn-lg square" role="button">Learn more</a>

有沒有辦法做到這一點還是我必須使用URL重寫?

回答

2

你的問題是概念上的缺陷。 Twig是一種模板語言,不能單獨呈現爲網頁。

您可能不想直接鏈接到.twig文件(實際上,.twig文件不應直接在您的公共文檔根目錄中訪問)!相反,您需要一些PHP代碼(或者是獨立腳本,或者最好是front controller route),用於在您的應用程序上下文中呈現您的模板,並將您完成渲染的頁面返回到您指定的URL。

例如,在超薄的框架,你可以這樣做:

// Render Twig template in route 
$app->get('/events', function ($request, $response, $args) { 
    return $this->view->render($response, 'events.html.twig'); 
}); 

// Run app 
$app->run(); 

this article的詳細信息,從程序麪條代碼移開並在MVC思維。

0

如果您使用的是twig,則需要解析模板,因此無法按照您嘗試的方式進行鏈接。在Symfony 2中,您可以使用類似{{ path('name_of_path') }}的東西,但您需要先指定路線。

可以命名與name="event"路線與註解:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

/** 
* Matches /event exactly 
* 
* @Route("/event", name="event") 
*/ 
public function eventAction() 
{ 
    // ... 
} 

在樹枝模板鏈接與此:

<a href="{{ path('event') }}">Event</a> 

此處瞭解詳情: http://symfony.com/doc/current/templating.html#linking-to-pages

1

您可以使用模板控制這一點。只需添加新路線,指定模板控制器FrameworkBundle:Template:template和枝條模板。

routes.yml

qwerty: 
    path: /qwerty 
    defaults: 
     _controller: FrameworkBundle:Template:template 
     template: Events.twig 

,並更改此鏈接:

<a href="{{ path('qwerty') }}" class="btn btn-primary btn-lg square" role="button">Learn more</a> 

http://symfony.com/doc/current/templating/render_without_controller.html

+0

愚蠢的問題,但我在哪裏把'routes.yml'?我在鏈接的文檔中使用php做了這個,但是我得到了'Uncaught Twig_Error_Syntax:Unknown「path」function' –