百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nice_partials

> 编程语言
开源

一点魔法,使部分元素完美适用于组件。

385 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一点魔法,使部分元素完美适用于组件。

nice_partials [version]

Nice Partials adds ad-hoc named content areas, or sections, to Action View partials with a lot of extra power on top.

Everything happens through a new partial method, which at the base of it have method shorthands for partial specific content_for and content_for?s.

See, here we're outputting the image, title, and body sections:

app/views/components/_card.html.erb:

html+erb

Then in render we populate them:

html+erb

   # Same as `partial.content_for :title, t(".title")`

  
    Lorem ipsum dolor sit amet, …
  

  
    
  

So far these uses are pretty similar to Rails' global content_for & content_for?, except these sections are local to the specific partial, so there's no clashing or leaking.

More-in depth compared to regular Rails partials

Consider this regular Rails partials rendering:

html+erb

  

# app/views/components/_card.html.erb

There's a number of gotchas here:

  • The content_for writes to :title across every partial, thus leaking.
  • The rendering block isn't called until is, so the `content_for` isn't called and outputs nothing.

With Nice Partials the yield is automatic and we can write content for just that partial without leaking:

html+erb

  

# app/views/components/_card.html.erb

This happens because Nice Partials checks the partial source code for any yield calls that calls Rails' capture helper — e.g. yield and yield something but not yield :title. If there's no capturing yields Nice Partials calls capture for you.

This means Nice Partials also respect existing yield calls in your partial, so you can upgrade existing partials bit by bit or not at all if you don't want to.

Nice Partials:

  • are still regular Rails view partials.
  • reduces the friction when extracting components.
  • only ends up in the specific partials you need the functionality.
  • reduces context switching.
  • allows isolated helper logic alongside your partial view code.
  • doesn't require any upgrades to existing partials for interoperability.
  • are still testable!

Nice Partials are a lightweight and more Rails-native alternative to ViewComponent. Providing many of the same benefits as ViewComponent with less ceremony.

What extra powers does partial give me?

Having a partial object lets us add abstractions that are hard to replicate in standard Rails partials.

Passing content from the render call

Nice Partials will use Action View's local_assigns, which stores any locals passed to render, as the basis for contents.

Given a partial like

html+erb

 written by 

Can then be used like this:

html+erb

  

This will then output "Hello there written by Some guy, who writes stuff"

You can also use slice to pass on content from an outer partial:

html+erb

Declaring content as optional or required

In traditional Rails partials, you'll see lots of checks for whether or not we have content to then output an element.

With Nice Partials, it would look like this:

html+erb

  

However, we can remove the conditional using optional:

html+erb

  

This will avoid outputting an empty tag, which could mess with your markup, in case there's no content provided for title.

Note: with Nice Partials tag helpers support, this example could also be shortened to ``.

Required

Alternatively, if title is a section that we require to be provided, we can do:

html+erb

Here, required will raise in case there's been no title content provided by that point.

Appending content from the view into a section

Nice Partials supports calling any method on ActionView::Base, like the helpers shown here, and then have them auto-append to the section.

html+erb

  
  
  

I18n: translating and setting multiple keys at a time

partial.t is a shorthand to translate and assign multiple keys at once:

html+erb

# The above is the same as writing:

Capturing options in the rendering block and building HTML tags in the partial

You can pass keyword options to a writer method and they'll be auto-added to partial.x.options, like so:

html+erb

  

# app/views/components/_card.html.erb:
# From the render above `title.options` now contain `{ class: "text-m4", data: { controller: "title" } }`.
# The options can be output via `
 # => 
Title content

partial also supports auto-generating an element by calling any of Rails' tag methods e.g.:

html+erb
# This shorthand gets us the same h1 element from the previous example:
 # => 
Title content

# Internally, this is similar to doing:

Yielding tag builders into the rendering block

The above example showed sending options from the rendering block into the partial and having it construct elements.

But the partial can also prepare tag builders that the rendering block can then extend and finalize:

html+erb

# app/views/components/_card.html.erb
 # => 
Title content

Accessing the content returned via partial.yield

To access the inner content lines in the block here, partials have to manually insert a `` call.

html+erb

  Some content!
  Yet more content!

With Nice Partials, partial.yield returns the same content:

html+erb
# app/views/components/_card.html.erb
 # => "Some content!\n\nYet more content!"

Referring to the outer partial while rendering another

During a rendering block partial refers to the outer partial, so you can compose them.

html+erb

  

Passing content from one partial to the next

If you need to pass content into another partial, content_from lets you pass the keys to extract and then a hash to rename keys.

html+erb

  

Here, we copied the partial.title to cp.title and partial.byline became cp.header.

Defining and using well isolated helper methods

If you want to have helper methods that are available only within your partials, you can call partial.helpers directly:

html+erb
# app/views/components/_card.html.erb

# Later in the partial we can use the method:

Sponsored By

Would you like to support Nice Partials development and have your logo featured here? Reach out!

Setup

Add to your Gemfile:

ruby
gem "nice_partials"

Testing

bash
bundle exec rake test

MIT License

Copyright (C) 2022 Andrew Culver and Dom Christie . Released under the MIT license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言