开发者

Create your own tags/functions with Erubis

开发者 https://www.devze.com 2022-12-19 17:29 出处:网络
I have a ruby class that extends Erubis (a ruby templating engine) and I would like to create my own tags. The following is an example of what i\'d like to be reproduce:

I have a ruby class that extends Erubis (a ruby templating engine) and I would like to create my own tags. The following is an example of what i'd like to be reproduce:

<%= link_to "/some/url" %>

This code should generate a html 'a' tag linking to some url. Now i'd like to be able to create my own tags such as开发者_C百科:

<%= javascript_file "/some/javascript/file" %>

which would generate a script tag linking to some javascript file of my choice.

How can i easily extend erubis to do that?

Thanks for your time.


Those are just function calls that return the tag in a string:

def javascript_file( file_path )
    "<script src=\"#{ file_path }\" type=\"text/javascript\"/>"
 end

You just need to ensure that the functions are within scope at the time you call the binding.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号