Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture not supported for 'slim' template #120

Closed
maylogger opened this issue Apr 8, 2013 · 12 comments
Closed

Capture not supported for 'slim' template #120

maylogger opened this issue Apr 8, 2013 · 12 comments

Comments

@maylogger
Copy link

在使用 slim 這個語言的時候
_layout.html.slim 放置

= yield :header

以及 index.html.slim 裡面放置

- content_for :header do
  something

就會噴錯
Capture not supported for 'slim' template

原本想說這應該是 fire.app 提供的 helper 應該與 slim 無關才對?

@maylogger
Copy link
Author

相關資訊
jlong/serve#38

@maylogger
Copy link
Author

提供簡單環境供測試

https://github.com/evenwu/fireapp-slim-bug

@elct9620
Copy link
Contributor

elct9620 commented May 3, 2013

https://github.com/tka/serve/blob/master/lib/serve/view_helpers.rb

(約 75 行左右)
發生錯誤指出的檔案是這個,似乎是需要有一個叫做 capture_slim 的 method 但是沒有。

不過可以用下面方法解決

在 view_helpers.rb 加入下面的語法

def capture_slim(&block)
    buffer = ""
    old_buffer, @_out_buf = @_out_buf, buffer
    yield
    buffer
  ensure
    @_out_buf = old_buffer
end

大致上就是把裡面原本的 capture_erb 重做一次,我測試後是正常的。

@maylogger
Copy link
Author

@elct9620 非常有幫助!!!

@hlb
Copy link
Contributor

hlb commented May 3, 2013

@elct9620 would you like to send a pull request to https://github.com/tka/serve ? :)

@hlb
Copy link
Contributor

hlb commented May 10, 2013

@elct9620 測試了一下, 似乎只會在頁面原處出現,並沒有送去 yield 該在的地方

@elct9620
Copy link
Contributor

剛剛測試之後也發現這個問題,正在思考問題原因

@elct9620
Copy link
Contributor

def capture_slim(&block)
    buffer = ""
    old_buffer, @_out_buf = @_out_buf, buffer
    buffer << yield
  ensure
    @_out_buf = old_buffer
end

不過 contenr_for 得改成

= content_for :header do
  h1 Header

使用 - 會發生無法轉換 Array 為 String 的錯誤(並且內容會重複兩次)
目前只找到這個解法

補充:

使用
- content_for :header 得用 - yield :header 呼叫,不然會重複兩次(其中一次會在指定位置之外呈現出來)
目前還在研究原因,感覺像是 slim 設計上跟 serve 的某個部分衝突到了

補充2:

- content_for :header- yield :header 會在原地顯示

補充3:
http://stackoverflow.com/questions/14615705/making-content-for-work-with-slim

應該還是要使用 = content_for :header 的形式,似乎在 Rails 有特別的方式管理 Buffer

@elct9620
Copy link
Contributor

Final Code:

def capture_slim(&block)
    buffer = ""
    old_buffer, @_out_buf = @_out_buf, buffer
    new_buffer = yield
    new_buffer = new_buffer.to_s if new_buffer.is_a?(Array)
    buffer << new_buffer
  ensure
    @_out_buf = old_buffer
end

可以 Support - content_for :header 但是不會正常運作就是了⋯⋯
(至少不會用錯直接噴錯誤這樣)

至於支援,那就是很多複雜處理了 Orz ( Rails 似乎用了一個檔案去做一個 Handle 的動作 )

@tka
Copy link
Contributor

tka commented May 10, 2013

@elct9620,

trace 了 slim 的 code 之後我依據
https://github.com/judofyr/temple/blob/master/lib/temple/generators.rb 這個檔寫了 capture_slim

因為 slim 的輸出最後會透過他處理, 所以依照他的環境對_buf做了手腳

下面這 code 是我的結果, 測試 @evenwu 的例子沒問題, 不管是 = 還是 - 都有效

    def capture_slim(&block)
      old_buffer = eval  "_buf", block.binding
      eval  "origin_buf,_buf=_buf,[]", block.binding
      capture_content = yield.to_s
      eval  "_buf=origin_buf", block.binding
      capture_content
    ensure
      eval  "_buf=origin_buf", block.binding
    end

@maylogger
Copy link
Author

請問 @tka 這些東西下個版本會進入 fireapp 嗎?

@tka
Copy link
Contributor

tka commented Jun 27, 2013

fe0bb5b 之後支援 slim content_for 了

@hlb hlb closed this as completed Sep 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants