什麼時機要用 Helper, 什麼時機要用 partial
source link: https://blog.niclin.tw/2015/12/09/%E4%BB%80%E9%BA%BC%E6%99%82%E6%A9%9F%E8%A6%81%E7%94%A8-helper-%E4%BB%80%E9%BA%BC%E6%99%82%E6%A9%9F%E8%A6%81%E7%94%A8-partial/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Helper:
使用 Helper 的情境多半是:
- 產生的 HTML code 需要與原始程式碼進行一些邏輯混合,但不希望 View 裡面搞得太髒。
- 需要與預設的 Rails 內建的一些方便 Helper 交叉使用。
使用 Helper 封裝程式碼可以帶給專案以下一些優點:
- Don’t repeat yourself(DRY)程式碼不重複
- Good Encapsulation好的封裝性
- 提供 view 模板良好的組織
- 易於修改程式碼
partial:
Partial 簡單說就是程式碼中的一小段,通常使用在 HTML 中讓 View的Code 可以更乾淨,將重複使用到的區塊切成獨立的 Partial,比方說頁首頁尾、表單、社群插件等等,讓任何一個頁面都可以讀取這段Partial而不用重複寫一次一模一樣的Code。
使用 的情境是:
- long template | 如果當檔 HTML 超過兩頁
- highly duplicated | HTML 內容高度重複
- independent blocks | 可獨立作為功能區塊
什麼時機使用:
partial 負責處理大片的 HTML code,或是之後要利用 ajax render 出來的片段。
helper 則負責處理跟邏輯判斷有關的東西。
Recommend
-
82
什麼鬼_图片_抽屉
-
21
什麼鬼沒有邦交。。。。
-
20
什麼是 MLOps ? 簡單來說就是以 Machine Learning 為主的元素加上 DevOps 的概念讓整個 Machine Learning 生命週期有一個完整的 Pipeline 管理, 如果說 DevOps 是為了解決 Developer 和 Operator 的問題,而 MLOps...
-
11
SEO是現在行銷領域上最多人關注的領域之一,不過SEO到底是什麼意思?為什麼重要?要怎麼自學呢?有辦法自學...
-
5
大家接觸 Go 語言肯定對
-
3
下載檔案時為什麼要設計一頁顯示「檔案下載即將開始」?-黑暗執行緒 相信大家都有過這種經驗,在網站下載檔案沒有直接開始下載,而是跳轉到另一個網頁顯示「檔案下載即將開始 / Your download will start shortly」。點完直接下載不是比較乾脆?花功夫多做...
-
10
五倍券官網當機亂碼為什麼「滚」出來? 2021-09-26 12:50 PM 4 3,585 前幾天的五倍券開搶,九點一到不意外地系統當機了。話說,幾百萬人同時殺進來,系統...
-
4
為何要使用Service Object:顧名思義,Service Object是因為有某些類似的特定功能,像是一個『service』,跟資料庫中的model並無直接關係,因此拉出來獨立成為一個class,在邏輯上會更容易管理。不過在文章中有定義了幾個需要使用service object的情況...
-
4
Nic Lin's Blog喜歡在地上滾的工程師在 Ruby 中常量(constant)其實是可以變更的DEFAULT_MESSAGE = "Hello" DEFAULT_MESSAGE << "123" puts DEFAULT_MESSAG...
-
8
2022年9月14日星期三 恐慌再現 CPI 跌眼鏡 是買入時機? 昨日美股因為 CPI 數據強差人意而大跌, 納指跌 5.16% ,標普跌 4.32% ,雙雙成為有史以來最大跌...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK