3 * TConditional class file.
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls
15 * TConditional displays appropriate content based on the evaluation result
16 * of a PHP expression specified via {@link setCondition Condition}.
17 * If the result is true, it instantiates the template {@link getTrueTemplate TrueTemplate};
18 * otherwise, the template {@link getFalseTemplate FalseTemplate} is instantiated.
19 * The PHP expression is evaluated right before {@link onInit} stage of the control lifecycle.
21 * Since {@link setCondition Condition} is evaluated at a very early stage, it is recommended
22 * you set {@link setCondition Condition} in template and the expression should not refer to
23 * objects that are available on or after {@link onInit} lifecycle.
25 * A typical usage of TConditional is shown as following:
27 * <com:TConditional Condition="$this->User->IsGuest">
29 * <a href="path/to/login">Login</a>
30 * </prop:TrueTemplate>
31 * <prop:FalseTemplate>
32 * <a href="path/to/logout">Logout</a>
33 * </prop:FalseTemplate>
37 * TConditional is very light. It instantiates either {@link getTrueTemplate TrueTemplate}
38 * or {@link getFalseTemplate FalseTemplate}, but never both. And the condition is evaluated only once.
40 * @author Qiang Xue <qiang.xue@gmail.com>
41 * @package System.Web.UI.WebControls
44 class TConditional extends TControl
46 private $_condition='true';
47 private $_trueTemplate;
48 private $_falseTemplate;
49 private $_creatingChildren=false;
52 * Processes an object that is created during parsing template.
53 * This method overrides the parent implementation by removing
54 * all contents enclosed in the template tag.
55 * @param string|TComponent text string or component parsed and instantiated in template
56 * @see createdOnTemplate
58 public function addParsedObject($object)
60 if($this->_creatingChildren)
61 parent::addParsedObject($object);
65 * Creates child controls.
66 * This method overrides the parent implementation. It evaluates {@link getCondition Condition}
67 * and instantiate the corresponding template.
69 public function createChildControls()
71 $this->_creatingChildren=true;
75 $result=$this->getTemplateControl()->evaluateExpression($this->_condition);
79 throw new TInvalidDataValueException('conditional_condition_invalid',$this->_condition,$e->getMessage());
83 if($this->_trueTemplate)
84 $this->_trueTemplate->instantiateIn($this->getTemplateControl(),$this);
86 else if($this->_falseTemplate)
87 $this->_falseTemplate->instantiateIn($this->getTemplateControl(),$this);
88 $this->_creatingChildren=false;
92 * @return string the PHP expression used for determining which template to use. Defaults to 'true', meaning using TrueTemplate.
94 public function getCondition()
96 return $this->_condition;
100 * Sets the PHP expression to be evaluated for conditionally displaying content.
101 * The context of the expression is the template control containing TConditional.
102 * @param string the PHP expression used for determining which template to use.
104 public function setCondition($value)
106 $this->_condition=TPropertyValue::ensureString($value);
110 * @return ITemplate the template applied when {@link getCondition Condition} is true.
112 public function getTrueTemplate()
114 return $this->_trueTemplate;
118 * @param ITemplate the template applied when {@link getCondition Condition} is true.
120 public function setTrueTemplate(ITemplate $value)
122 $this->_trueTemplate=$value;
126 * @return ITemplate the template applied when {@link getCondition Condition} is false.
128 public function getFalseTemplate()
130 return $this->_falseTemplate;
134 * @param ITemplate the template applied when {@link getCondition Condition} is false.
136 public function setFalseTemplate(ITemplate $value)
138 $this->_falseTemplate=$value;