我在我的Rails應用中實施(或多或少)WordPress's concept of shortcodes。問題是,當我在視圖中通過content_for
定義的任何佈局中的:yield
時,它只是空白。所以額外的JavaScript和標題標籤不會呈現。content_for登錄時爲空
換句話說,在佈局中調用content_for? :title
返回false。
這只是發生在帖/指數,並且只有當我在我登錄時運行filter_shortcodes
助手時。有沒有人遇到過這樣的事情?
在視圖/帖/ index.html.haml:
- content_for :script do
= javascript_include_tag '/assets/autoload.js'
- content_for :title do
Blog
...
= render template: 'article-content',
而在視圖/物品content.html.haml(filter_shortcodes
是Shortcode
模塊中定義的一個輔助函數。):
:plain
#{filter_shortcodes instance.content}
我仍然堅信,這個問題是在我的短代碼模塊,所以這裏是,回來不受歡迎的需求:
module Shortcode
def filter_shortcodes content
content.gsub /(?<!\\)\[.+\]/ do |code|
# A shortcode must:
# - be on its own line
# - be [contained within square brackets]
# - be named using only lowercase letters
# If it contains parameters, they must come in the form:
# key="value"
shortcode = /^\s*\[(?<name>[a-z]+) (?<params>.*)\s*\]\s*$/.match code
params_list = shortcode[:params].gsub /"|"/, '"'
param_regexp = /([a-z]+)="([^"]*)"/
shortcode_params = {}
params_list.scan param_regexp do |param|
shortcode_params[param[0].to_sym] = param[1]
end
render_to_string template: "shortcodes/#{shortcode[:name]}",
:locals => shortcode_params, layout: false
end
end
end
我們不需要看到你的filter_shortcodes模塊,但是產生內容的佈局很好看。 – MrYoshiji
好的,我編輯了我的問題以包含佈局 – acobster