팁/튜토리얼

Context::set($key, $val, $set_to_get_vars = 0)

2015.08.03 09:18
3,105
0
0

정의 위치

  • ./classes/context/Context.class.php

정의 내용

/**
 * Set a context value with a key
 *
 * @param string $key Key
 * @param string $val Value
 * @param mixed $set_to_get_vars If not FALSE, Set to get vars.
 * @return void
 */
function set($key, $val, $set_to_get_vars = 0)
{
    is_a($this, 'Context') ? $self = $this : $self = self::getInstance();
    $self->context->{$key} = $val;
    if($set_to_get_vars === FALSE)
    {
        return;
    }
    if($val === NULL || $val === '')
    {
        unset($self->get_vars->{$key});
        return;
    }
    if($set_to_get_vars || $self->get_vars->{$key})
    {
        $self->get_vars->{$key} = $val;
    }
}

 

용도

  • XE 기반 코드 간에 변수를 전달합니다.
  • 템플릿 코드에 변수를 전달합니다.

파라메터

  • string $key : 변수 이름 문자열입니다. Context::set('key', $var); 로 전달한 변수를 Context::get('key'); 로 불러올 수 있습니다.
  • mixed $val : 변수에 저장할 내용입니다.
  • boolen $set_to_get_vars :  FALSE 를 전달하지 않을 경우 GET으로 전달 받은 변수 취급됩니다. getUrl 등 호출시 영향을 줄 수도 있습니다.

예시

  1. ./classes/editor/EditorHandler.class.php 내용 중 EditorHandler 클래스 setInfo($info) 메소드
    • /**
       * Superclass of the edit component.
       * Set up the component variables
       *
       * @class EditorHandler
       * @author NAVER (developers@xpressengine.com)
       */
      class EditorHandler extends Object
      {

          /**
           * set the xml and other information of the component
           * @param object $info editor information
           * @return void
           * */
          function setInfo($info)
          {
              Context::set('component_info', $info);

              if(!$info->extra_vars)
              {
                  return;
              }

              foreach($info->extra_vars as $key => $val)
              {
                  $this->{$key} = trim($val->value);
              }
          }

      }

       
  2. ./modules/point/point.admin.view.php 내용 중 dispPointAdminConfig() 메소드
    • /**
       * @brief Default configurations
       */
      function dispPointAdminConfig()
      {
          // Get the list of level icons
          $level_icon_list = FileHandler::readDir("./modules/point/icons");
          Context::set('level_icon_list', $level_icon_list);
          // Get the list of groups
          $oMemberModel = getModel('member');
          $group_list = $oMemberModel->getGroups();
          $selected_group_list = array();
          if(count($group_list))
          {
              foreach($group_list as $key => $val)
              {
                  $selected_group_list[$key] = $val;
              }
          }
          Context::set('group_list', $selected_group_list);
          //Security
          $security = new Security();
          $security->encodeHTML('group_list..title','group_list..description');

          // Set the template
          $this->setTemplateFile('config');
      }

       

댓글 0