>  기사  >  백엔드 개발  >  Yii_php 스킬의 render와 renderPartial의 차이점

Yii_php 스킬의 render와 renderPartial의 차이점

WBOY
WBOY원래의
2016-05-16 20:36:351168검색

다음은 Xinyi Network Company에서 프로젝트를 개발하면서 얻은 경험 중 일부입니다
페이지 출력을 렌더링할 때.

1.render는 상위 템플릿의 콘텐츠를 출력하고 렌더링된 콘텐츠를 상위 템플릿에 삽입합니다. |
2.renderPartial은 상위 템플릿의 내용을 출력하지 않습니다. 이 렌더링의 일부 내용만 출력됩니다.

동시에 중요한 차이점이 있습니다.

processOutput($output) 함수는 기본적으로

render 함수 내에서 실행되며, CClientScript의
에 CTreeView와 같은 구성 요소를 등록합니다. 출력 렌더링에 필요한 스크립트입니다.

그리고 renderPartial()은 기본적으로 클라이언트 스크립트를 자동으로 렌더링하고 출력하지 않습니다. 출력하기 전에 매개변수를 지정해야 합니다.
renderPartial($view,$data=null,$return=false,$processOutput=false)
processOutput을 true로 지정하면 됩니다.

예를 들어 CTreeView를 부분적으로 출력하려면 renderPartial을 사용하여 렌더링합니다. 기본 processOutput=false를 따르면 클라이언트 스크립트 없이 콘텐츠가 출력됩니다.
출력 내용은 일반 ul 목록입니다. 나무 모양의 접는 효과는 없습니다. processOutput=true를 적극적으로 설정하면 CTreeView에 필요한 모든 클라이언트 스크립트가 목록 맨 앞에 정상적으로 출력됩니다.

다음은 사용되는 여러 관련 기능을 소개합니다.

render, renderPartial은 더 이상 도입되지 않습니다
프로세스출력()

<&#63;php
publicfunction render($view,$data=null,$return=false)
{
  if($this->beforeRender($view))
  {
    $output=$this->renderPartial($view,$data,true);
    if(($layoutFile=$this->getLayoutFile($this->layout))!==false)
      $output=$this->renderFile($layoutFile,array('content'=>$output),true);
    $this->afterRender($view,$output);
    $output=$this->processOutput($output);
    if($return)
      return $output;
    else
      echo $output;
  }
}
publicfunction renderPartial($view,$data=null,$return=false,$processOutput=false)
{
  if(($viewFile=$this->getViewFile($view))!==false)
  {
    $output=$this->renderFile($viewFile,$data,true);
    if($processOutput)
      $output=$this->processOutput($output);
    if($return)
      return $output;
    else
      echo $output;
  }
  else
    thrownewCException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
      array('{controller}'=>get_class($this),'{view}'=>$view)));
}
publicfunction processOutput($output)
{
  Yii::app()->getClientScript()->render($output);
  // if using page caching, we should delay dynamic output replacement
  if($this->_dynamicOutput!==null&& $this->isCachingStackEmpty())
  {
    $output=$this->processDynamicOutput($output);
    $this->_dynamicOutput=null;
  }
  if($this->_pageStates===null)
    $this->_pageStates=$this->loadPageStates();
  if(!empty($this->_pageStates))
    $this->savePageStates($this->_pageStates,$output);
  return $output;
}

위의 내용은 실제 작업에서 꽤 유용합니다. 예를 들어 큰 구성 요소를 사용하지 않으려면 템플릿에 변수를 직접 입력하거나 템플릿에 여러 변수를 배열로 입력할 수 있습니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.