PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4/lib/View.php

https://github.com/mahimarathore/mahi
PHP | 92 lines | 58 code | 0 blank | 34 comment | 3 complexity | fa646dd2d33b7da236a50edf22672ef0 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /*
  3. * Undocumented
  4. *
  5. * @link http://agiletoolkit.org/
  6. *//*
  7. ==ATK4===================================================
  8. This file is part of Agile Toolkit 4
  9. http://agiletoolkit.org/
  10. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  11. Distributed under Affero General Public License v3 and
  12. commercial license.
  13. See LICENSE or LICENSE_COM for more information
  14. =====================================================ATK4=*/
  15. /**
  16. * HtmlElement is a base class of any View which would render as a
  17. * single HTML element. By default it puts <div> on your page, but
  18. * you can change the element with setElement()
  19. *
  20. * Use:
  21. * $tabs=$this->add('View')->set('Hello')->addClass('myclass');
  22. *
  23. * @license See http://agiletoolkit.org/about/license
  24. *
  25. **/
  26. class View extends AbstractView {
  27. /** Change which element is used. 'div' by default, but change with this funciton */
  28. function setElement($element){
  29. $this->template->trySet('element',$element);
  30. return $this;
  31. }
  32. /** Add attribute to element. Also supports hash for multiple attributes */
  33. function setAttr($attribute,$value=null){
  34. if(is_array($attribute)&&is_null($value)){
  35. foreach($attribute as $a=>$b)$this->setAttr($a,$b);
  36. return $this;
  37. }
  38. $this->template->appendHTML('attributes',' '.$attribute.'="'.$value.'"');
  39. return $this;
  40. }
  41. /** Add class to element. */
  42. function addClass($class){
  43. if(is_array($class)){
  44. foreach($class as $c)$this->addClass($class);
  45. return $this;
  46. }
  47. $this->template->append('class'," ".$class);
  48. return $this;
  49. }
  50. function removeClass($class){
  51. $cl=' '.$this->template->get('class').' ';
  52. $cl=str_replace($cl,' '.$class.' ',' ');
  53. $this->template->set('class',trim($cl));
  54. return $this;
  55. }
  56. function setClass($class){
  57. $this->template->trySet('class', $class);
  58. return $this;
  59. }
  60. /** Add style to element. */
  61. function setStyle($property,$style=null){
  62. if(is_null($style)&&is_array($property)){
  63. foreach($property as $k=>$v)$this->setStyle($k,$v);
  64. return $this;
  65. }
  66. $this->template->append('style',";".$property.':'.$style);
  67. return $this;
  68. }
  69. /** Add style to element */
  70. function addStyle($property,$style=null){
  71. return $this->setStyle($property,$style);
  72. }
  73. /** Sets text appearing inside element. Automatically escapes HTML characters */
  74. function setText($text){
  75. $this->template->trySet('Content',$text);
  76. return $this;
  77. }
  78. /** Alias for setText. Escapes HTML characters. */
  79. function set($text){
  80. return $this->setText($text);
  81. }
  82. /** Sets HTML */
  83. function setHtml($html){
  84. $this->template->trySetHTML('Content',$html);
  85. return $this;
  86. }
  87. function defaultTemplate(){
  88. return array('htmlelement');
  89. }
  90. }