3 * TConditional class file.
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TConditional.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Web.UI.WebControls
16 * TConditional displays appropriate content based on the evaluation result
17 * of a PHP expression specified via {@link setCondition Condition}.
18 * If the result is true, it instantiates the template {@link getTrueTemplate TrueTemplate};
19 * otherwise, the template {@link getFalseTemplate FalseTemplate} is instantiated.
20 * The PHP expression is evaluated right before {@link onInit} stage of the control lifecycle.
22 * Since {@link setCondition Condition} is evaluated at a very early stage, it is recommended
23 * you set {@link setCondition Condition} in template and the expression should not refer to
24 * objects that are available on or after {@link onInit} lifecycle.
26 * A typical usage of TConditional is shown as following:
28 * <com:TConditional Condition="$this->User->IsGuest">
30 * <a href="path/to/login">Login</a>
31 * </prop:TrueTemplate>
32 * <prop:FalseTemplate>
33 * <a href="path/to/logout">Logout</a>
34 * </prop:FalseTemplate>
38 * TConditional is very light. It instantiates either {@link getTrueTemplate TrueTemplate}
39 * or {@link getFalseTemplate FalseTemplate}, but never both. And the condition is evaluated only once.
41 * @author Qiang Xue <qiang.xue@gmail.com>
42 * @version $Id: TConditional.php 3245 2013-01-07 20:23:32Z ctrlaltca $
43 * @package System.Web.UI.WebControls
46 class TConditional extends TControl
48 private $_condition='true';
49 private $_trueTemplate;
50 private $_falseTemplate;
51 private $_creatingChildren=false;
54 * Processes an object that is created during parsing template.
55 * This method overrides the parent implementation by removing
56 * all contents enclosed in the template tag.
57 * @param string|TComponent text string or component parsed and instantiated in template
58 * @see createdOnTemplate
60 public function addParsedObject($object)
62 if($this->_creatingChildren)
63 parent::addParsedObject($object);
67 * Creates child controls.
68 * This method overrides the parent implementation. It evaluates {@link getCondition Condition}
69 * and instantiate the corresponding template.
71 public function createChildControls()
73 $this->_creatingChildren=true;
77 $result=$this->getTemplateControl()->evaluateExpression($this->_condition);
81 throw new TInvalidDataValueException('conditional_condition_invalid',$this->_condition,$e->getMessage());
85 if($this->_trueTemplate)
86 $this->_trueTemplate->instantiateIn($this->getTemplateControl(),$this);
88 else if($this->_falseTemplate)
89 $this->_falseTemplate->instantiateIn($this->getTemplateControl(),$this);
90 $this->_creatingChildren=false;
94 * @return string the PHP expression used for determining which template to use. Defaults to 'true', meaning using TrueTemplate.
96 public function getCondition()
98 return $this->_condition;
102 * Sets the PHP expression to be evaluated for conditionally displaying content.
103 * The context of the expression is the template control containing TConditional.
104 * @param string the PHP expression used for determining which template to use.
106 public function setCondition($value)
108 $this->_condition=TPropertyValue::ensureString($value);
112 * @return ITemplate the template applied when {@link getCondition Condition} is true.
114 public function getTrueTemplate()
116 return $this->_trueTemplate;
120 * @param ITemplate the template applied when {@link getCondition Condition} is true.
122 public function setTrueTemplate(ITemplate $value)
124 $this->_trueTemplate=$value;
128 * @return ITemplate the template applied when {@link getCondition Condition} is false.
130 public function getFalseTemplate()
132 return $this->_falseTemplate;
136 * @param ITemplate the template applied when {@link getCondition Condition} is false.
138 public function setFalseTemplate(ITemplate $value)
140 $this->_falseTemplate=$value;