2014-11-20 60 views
0

我正在開發一個Symfony2項目,但這個問題可能仍然適用於一般的PHP。我仍然非常瞭解。Symfony2拆分變量

我有一個網址,需要像website.com/2014/fall/other。在symfony裏的樹枝模板我有聯繫的website.com/{{ allSemesters }}/other

我傳遞中的變量是

$allSemesters=array("Fall 2014", "Spring 2015", "Summer 2015"); 

這是我的模板。

{% for allSemester in allSemesters %} 
    <a href="{{ allSemester }}/other">{{ allSemester | capitalize }}</a> 
{% endfor %} 

在去鏈接產生的:website.com/Spring%202015/other而不是website.com/2015/Spring/other

任何方式來重新配置我的數組或分裂變量莫名其妙得到所需的網址?提前謝謝了!

回答

1

你可以用樹枝的replace過濾

<a href="{{ allSemester |replace({' ': "/"}) }}/other"> 

的「Symfony的方式」是使用path()計算給出一個「賽季」和「年」參數

+0

是的,我想我可能應該這樣做,並通過symfony路徑()傳遞變量。好的電話,謝謝你的文檔。 – Moose 2014-11-20 23:48:10

0

使用.yml路由的路由,我會做這樣的事情:

semester_other: 
    pattern: /{year}/{semester}/other/ 
    defaults: { _controller: "AppBlablaBundle:Semester:other", year: null, semester: null} 

而且控制器:

public function otherAction($year, $semester) { 

}