I've starting programming on ASP.NET MVC Framework a year ago. Recently. I've learning Ruby On Rails Framework There is "custom html helper" feature in ASP.NET MVC So I can create my own html helper
<%= Html.MyOwnHtmlHelper() %>
I've learned that there is html helpers in Ruby such as
<% text_area %>
which render at html
I have a question. Can I create my own html helper for rendering my own html?
Creating HTML Helpers with Static MethodsThe easiest way to create a new HTML Helper is to create a static method that returns a string. Imagine, for example, that you decide to create a new HTML Helper that renders an HTML <label> tag. You can use the class in Listing 2 to render a <label> .
HTML Custom Helpers HTML helper is a method that returns a HTML string. Then this string is rendered in view. MVC provides many HTML helper methods. It also provides facility to create your own HTML helper methods. Once you create your helper method you can reuse it many times.
To create a custom HTML helper you have create a static class and static method. below example is for a custom HTML helper for submit button. Make sure you add below using statements. Now, You can now use it on the page where you want to define a button.
A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again. And then in the view code, you call the helper method and pass it to the user as an argument.
To create a new helper:
create a module according to the file name. In this case
module TagsHelper end
define your helper as method
module TagsHelper def hello_world(name) "hello #{name}" end end
Now you can use the hello_world
helper method in your view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With