开发者

Proper embedded Ruby Code writing

I am trying to write this :

 %meta{ :name => "keywords", :content => "#{@page_city}, #{truncate_words(@page_keywords, 7) || 'Meta, Words, Are, Dope, For, SEO'}"}

Basically it sees if there is a local page that has a page city or keywords and adds those as they meta keywords. If it doesn't, it says the latter.

This works, but the only problem is tha开发者_运维知识库t first comma after page_city is appearing. So right now it appears as ..

 <meta content=', Meta, Words, Are, Dope, For, SEO' name='keywords' />

Does anyone know how to include that "," into the embedded variables as well?


You can usually lean on Array for situations like this:

- default_keywords = %w[ Meta Words Are Dope For SEO ]
- meta_content = [ @page_city, (truncate_words(@page_keywords, 7) || default_keywords) ]

%meta{ :name => "keywords", :content => meta_content.flatten.compact.join(',') }

Array#compact strips out all nil values to avoid inserting extra commas.


You could always just do this:

#{@page_city + ', ' if @page_city}

Personally, I would probably go for a different approach to this altogether:

@arg1 = 'foo'
@arg2 = 'bar'
@arg3 = 'baz'
[@arg1, @arg2, @arg3].join(", ")    # => "foo, bar, baz"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜