Search code examples
asp.netwebformshaml

Create tag alias in HAML


Hi i am trying to find a way to create aliases for certain tags using HAML.

Eg. The HAML is flexible enough to get this HAML markup:

%asp:TextBox#txtName/

to be rendered as

<asp:TextBox id="txtName" />

meanwhile all server side tags in asp.net require the

runat="server" 

attribute.

First: How can I make HAML write this required attribute to me?

Seconde: Is to dumb use HAML to organize my live with asp.net tags?

Its no just about the runat="server" attribute its more like an clear and organized path to write my asp.net markup.


Solution

  • I would go with a helper method. In ruby it would be something like:

    def asp_tag attrs_hash={}
      result = '<asp:TextBox '
      result << 'runat="server" '
      attrs_hash.each {|k,v| result << "#{k}=\"#{v}\"" }
      result << '>'
      result << yield # not necessary if you never have a body in your tag
      result << '</asp:TextBox>'
      result.html_safe # in rails anyways.....
    end
    

    and use it in you view like this:

    = asp_tag{id: 'txtName'}
      .moreStuff
        teh awesome
    

    should render

    <asp:TextBox id="txtName" runat="server">
      <div class="moreStuff">
        teh awesome
      </div>
    </asp:TextBox>
    

    Since you are on a microsoftish environment some or most of this may or may not work. The ruby method may be replaced by something in whatever language you are running haml with.

    Another option is to use a partial. I makes the called code nicer but the calling somewhat worse. I tend to like hiding away ugliness from the view.