openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

ConditionalAttribute.class.php (936B)


      1 <?php
      2 
      3 
      4 namespace template_engine\element\attribute;
      5 
      6 
      7 use template_engine\element\attribute\SimpleAttribute;
      8 use template_engine\element\Value;
      9 
     10 /**
     11  * A conditional attribute is an attribute whose existence depends on a condition.
     12  * The condition would normally be an PHP expression.
     13 */
     14 class ConditionalAttribute extends SimpleAttribute
     15 {
     16 /** The condition must be an valid PHP expression.
     17     */
     18 	protected $condition;
     19 
     20 	/**
     21 	 * ConditionalAttribute constructor.
     22 	 * @param $condition must be a valid PHP expression
     23 	 * @param $name name of attribute
     24 	 * @param $value value
     25 	 */
     26 	public function __construct($condition,$name,$value)
     27 	{
     28 		$this->condition = $condition;
     29 		parent::__construct($name,$value);
     30 	}
     31 
     32 
     33     /**
     34     * Rendering of the value.
     35     * the value is wrapped into the conditional PHP expression.
     36     */
     37 	public function render()
     38 	{
     39 		return '<?php if('.$this->condition.'){ ?>'.parent::render().'<?php } ?>';
     40 	}
     41 }