[Slim/Twig] Custom template functions url_for / is_current_url > Web/PHP/API

본문 바로가기
사이트 내 전체검색

Web/PHP/API

[Slim/Twig] Custom template functions url_for / is_current_url

페이지 정보

작성자 sbLAB 댓글 0건 조회 1,986회 작성일 23-07-02 21:25

본문

 Custom template functions  

 https://github.com/slimphp/Twig-View 


[TwigExtension] provides these functions to your Twig templates:

  • url_for() - returns the URL for a given route. e.g.: /hello/world
  • full_url_for() - returns the URL for a given route. e.g.: http://www.example.com/hello/world
  • is_current_url() - returns true is the provided route name and parameters are valid for the current path.
     ※  호출된 라우터url(파라미터 포함, 파라미터 args 일치 필수)이 자신의 라우터 페이지라면 true 리턴 
    예) class="{{ is_current_url('root') ? 'active' : 'none' }}" <- 이 템플릿(twig)을 호출하는 라우터가  root 라면  class="active" 출력.  
  • current_url() - returns the current path, with or without the query string.
  • get_uri() - returns the UriInterface object from the incoming ServerRequestInterface object
  • base_path() - returns the base path.


    <br>
    <li><a href="{{ url_for('profile', { 'name': 'josh', 'age': '100' }) }}">profile</a></li>
    <li><a href="{{ url_for('selectall') }}">selectall</a></li>
    <li><a href="{{ url_for('s_all') }}">s_all</a></li>
    <li><a href="{{ url_for('root') }}">root</a></li>
    <br><br>
    <li><a href="{{ url_for('selectall') }}">url_for selectall</a></li>
    <li><a href="{{ full_url_for('selectall') }}">full_url_for selectall</a></li>

    <li><a href="{{ is_current_url('selectall') }}">is_current_url selectall</a></li>
    <li><a href="{{ current_url('selectall') }}">current_url selectall</a></li>
    <li><a href="{{ get_uri('selectall') }}">get_uri selectall</a></li>
    <li><a href="{{ base_path('selectall') }}">base_path selectall</a></li>

    <br><br>
    <li><a href="{{ url_for('profile', { 'name': 'josh', 'age': '100' }) }}"
{% if not is_current_url('profile', { 'name': 'josh' , 'age': '100' }) %} class="active"{% endif %}>not josh</a></li>
    <li><a href="{{ url_for('profile', { 'name': 'josh', 'age': '100' }) }}"
{% if is_current_url('profile', { 'name': 'josh' , 'age': '100' }) %} class="active"{% endif %}>is josh</a></li>

    <li><a href="{{ url_for('profile', { 'name': 'josh', 'age': '100' }) }}"
class="{{ is_current_url('root') ? 'active' : 'none' }}">is josh</a></li>


[웹페이지 생성 결과]
    <li><a href="/slim/public/hello/josh/100">profile</a></li>
    <li><a href="/slim/public/pall">selectall</a></li>
    <li><a href="/slim/public/s/all">s_all</a></li>
    <li><a href="/slim/public/">root</a></li>
    <br><br>
    <li><a href="/slim/public/pall">url_for selectall</a></li>
    <li><a href="http://localhost/slim/public/pall">full_url_for selectall</a></li>

    <li><a href="">is_current_url selectall</a></li>
    <li><a href="/slim/public/">current_url selectall</a></li>
    <li><a href="http://localhost/slim/public/">get_uri selectall</a></li>
    <li><a href="">base_path selectall</a></li>

    <br><br>
    <li><a href="/slim/public/hello/josh/100" class="active">not josh</a></li>
    <li><a href="/slim/public/hello/josh/100">is josh</a></li>
    <li><a href="/slim/public/hello/josh/100" class="active">is josh</a></li>
 


[참고 링크]

https://github.com/slimphp/Twig-View 

https://discourse.slimframework.com/t/slim4-how-to-get-args-for-is-current-url-in-twig/4273 




댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
24
어제
396
최대
1,279
전체
222,074

그누보드5
Copyright © sebom.com All rights reserved.