SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Applying an attribute to many HTML classes easily

    I have a bunch of classes for HTML input elements (textbox, textarea, elements) that derive from a base class. I want to programmatically make these elements readonly without having to set each instatiation to read only seperately. Is there a easy way to do this? Maybe there's a design pattern that will help me?

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Every html element has attributes so you could add a function to your base class to build attributes.


    PHP Code:
    // Example
    abstract class HtmlElement
    {
       protected 
    $_attribs = array();
       public function 
    setAttrib(){/* ... */}
       public function 
    setAttrib(){/* ... */}
       protected 
    _htmlAttrib()
       {
          
    $html '';
          foreach(
    $this->_attribs as $name => $value)
          {
             if(
    is_array($value)) $value implode(' '$value);
             
    $html .= $name '="' $value '"';
          }
          return 
    $html;
       }

    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My base class already builds the attributes. However, I want to be able to make all the elements of a certain page readonly, while still keeping the rest of the site still non-readonly. However, I don't want to have to set it for each element instantiated 1 by 1.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either you set the default to readonly or you set them one by one that's really the simplest solution.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    However, I want to be able to make all the elements of a certain page readonly
    Do you mean keep the elements of a certain form readonly?

    That might depend on whether you have designed things so that there is an overarching form object which contains or marshalls the html elements you talk of, if so there may be a nice solution.

  6. #6
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tdnxxx444 View Post
    My base class already builds the attributes. However, I want to be able to make all the elements of a certain page readonly, while still keeping the rest of the site still non-readonly. However, I don't want to have to set it for each element instantiated 1 by 1.
    You could have a static field on your base class like

    Code PHP:
    class HtmlElement
    {
        public static $disabled = false;
    }

    And then, on that only page you set the property to true. A static variable is
    shared by all objects instantiated from that class.

    Code PHP:
    HtmlElement::$disabled = true;
    I'm under construction | http://igstan.ro/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •