简体   繁体   English

Symfony2:URL中的#id到焦点元素

[英]Symfony2: #id in url to focus element

I have this navigation with links like this: href="http://mamgrow.lt/veikla.php#konkursai" 我有这样的链接导航: href="http://mamgrow.lt/veikla.php#konkursai"

So it opens a page with element with id='konkursai' focused. 因此,它将打开一个页面,其中元素的id='konkursai'

How can I do this in Symfony? 如何在Symfony中做到这一点?

My pages are created as actions and links to them are described in routing.yml 我的页面是作为操作创建的,并且到它们的链接在routing.yml中进行了描述。

MainController: 主控制器:

public function aboutUsAction() {
    return $this->render('MamgrowMainBundle:Main:about-us.html.twig');
}

public function activitiesAction() {
    return $this->render('MamgrowMainBundle:Main:activities.html.twig');
}

public function contactsAction() {
    return $this->render('MamgrowMainBundle:Main:contacts.html.twig');
}

routing.yml: routing.yml:

about-us:
    path:     /apie-mus
    defaults: { _controller: MamgrowMainBundle:Main:aboutUs }

contacts:
    path:     /kontaktai
    defaults: { _controller: MamgrowMainBundle:Main:contacts }

activities:
    path:     /veikla
    defaults: { _controller: MamgrowMainBundle:Main:activities }

只需将哈希部分附加到生成的路径/ URL:

<a href="{{ path('contacts') }}#some-anchor">Contact Us</a>

This solutions can help you for statics pages: 此解决方案可以帮助您创建静态页面:

    static-page:
            path: /website/{page}
            defaults: { _controller: MamgrowMainBundle:Main:static }

        public function staticAction($page) {
            return $this->render('MamgrowMainBundle:Main:'.$page.'.html.twig'');
        }

TwigView.html.twig
    <a href="{{ path('static-page', {"page": "contact-us"} ) }}">Contact Us</a>
    <a href="{{ path('static-page', {"page": "about-us "} ) }}">AboutUs </a>

the var "page" has to be named exactly like your views for the staticAction method works fine!!! var“页面”的命名必须与您的staticAction方法的视图完全一样!

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

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