惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
O
OpenAI News
美团技术团队
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
S
Security Affairs
博客园_首页
S
Schneier on Security
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
Vercel News
Vercel News
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 最新话题
D
DataBreaches.Net
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Check Point Blog
H
Hacker News: Front Page

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to use Code Injection in Ghost How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS Trigger the Ghost Portal popup based on page scroll progress How to Deploy Your Ghost Theme with GitHub Actions How to create a custom recommendations page for your Ghost CMS theme Ghost CMS News: New editor, refreshed settings & recommendations Ghost CMS News: Source - the new default theme and native image editing How to Add Different Logos for Dark & Light Themes in Ghost Upgrade to Node v18 for Ghost CMS & common errors Ghost Snippets: Adding Schema markup in Ghost How to add a scroll-based progress bar to your Ghost CMS site How to Create a Custom Navigation Template in Ghost How to Segment Subscribers with Member Labels in Ghost The best way to create a contact page in Ghost How to Use the Ghost Subscription Forms in Your Theme & Posts How to update your Ghost site URL when self-hosting Ghost CMS News: Signup cards and better landing pages How to Add Breadcrumbs to Your Ghost Theme How to integrate the Applause Button into your Ghost theme How to Hide a Post From the Home Page in Ghost CMS How to add Image Lightbox to Ghost Themes Ghost CMS News: Signup form embed, new editor and bulk actions. Biron Themes becomes Bright Themes 🎉 How to Audit & Test a Ghost Theme How to Create Related Posts Section in Ghost How to Use Ghost Filter Expressions Ghost CMS newsletter settings and post analytics How to Update Your Ghost Theme When We Release New Versions Ghost CMS News: Automatic list cleaning & email troubleshooting How to Create a Featured Post Slider in Ghost How to Add Reading Time to Ghost Blog Posts How to Create a Post Archive for Your Ghost Theme Ghost CMS Price Helper Overview & Tips Self-Hosted Ghost CMS with DigitalOcean Ghost CMS News: Member Growth Sources & Audience Feedback How to Open External Links in a New Tab in Ghost How to Integrate Google Analytics with Ghost Ghost CMS News: History Log & Newsletter Analytics How to Automatically Add Member Labels From Referral Traffic in Ghost? Ghost CMS News: Free trials & email notifications Ghost CMS Match Helper Usage & Tips Ghost CMS News: Native Comments Ghost CMS News: Ghost Explore & Premium Newsletter Previews Ghost Snippets: Custom Progress Cards & Steps Ghost CMS News: Native Search The Best Ghost CMS Search Plugins & Libraries Ghost Snippets: Custom Stylish Tables How to Add a Scroll Top & Progress Indicator Button in Ghost Ghost Snippets: Accordions How to Add Tag Dropdown to Your Ghost Theme Ghost 5.0 released 🎉 Ghost Snippets: Custom Subscribe Forms Ghost Snippets: Custom Alert Boxes How to Show Post or Newsletter Issue Number in Ghost How to Self-Host Google Fonts in Ghost CMS Ghost CMS News: Membership, Tiers & Activity Feed Custom Pages in Ghost CMS Themes Ghost CMS Search Using Fuse.js Ghost CMS News: Offers and Discounts & New Cards Ghost CMS News: Membership tiers, email CTA & filters Ghost CMS News: Grammarly Support & Feature Image Captions. Ghost CMS News: Improved Member Accounts & Personalized Newsletter. Ghost 4.0 Launched With Native Newsletters & Membership Ghost CMS News: Cancellation Feedback & Newsletter Stats. Ghost CMS News: Improved Member Dashboard & Sidebar Views. Ghost CMS News: Google Pay support, email newsletters & secondary navigation Ghost CMS News: Member Support, Portal & Content Snippets. Ghost CMS News: Referral Programs & Responsive Images. Ghost Themes as Progressive Web Apps Ghost 3.0 is here with brand new features Ghost 2.0 is here with new features How to Add Search to Ghost Using ghostHunter
Ghost CMS block and contentFor helper overview & tips
Bright Themes · 2023-07-18 · via Example blog

Ghost continues to prove its worth as a versatile content management system, enabling bloggers and publishers to create stunning websites. Among its array of features, the block and contentFor helpers stand out as powerful tools that facilitate the creation of customizable templates.

In this blog post, we will delve into the usage and potential of the {{{block}}} and {{{contentFor}}} helpers in Ghost CMS, providing examples and highlighting their benefits for enhancing your website's design and functionality.

Understanding the block helper

The block helper in Ghost CMS serves as a placeholder within a custom handlebars template, enabling you to create slots that can be populated when the template is inherited by another template file.

Here's a breakdown of its usage:

  • use the triple curly braces {{{block "block-name"}}} to define a block and create a placeholder within the template. Replace "block-name" with a unique identifier for the block.
  • when a template file inherits another template using {{!< template-name}}, the contentFor helper is used to access and populate the block definitions within the inherited template.
  • if the contentFor helper is not used, the block will be gracefully skipped, allowing flexibility in customizing templates.

To illustrate the usage of the block helper, let's consider one of the most common use cases. We regularly use these helpers to add scripts in a "scripts" block.:

<!-- default.hbs -->
<body>
  ...
  {{!-- Block for scripts --}}
  {{{block "scripts"}}}
  ...
</body>

In this example, the block helper is used within the default.hbs template to create a placeholder for scripts. Then in any template file like page.hbs, post.hbs that inherits the default.hbs can utilize contentFor helper to populate the scripts block. This allows specific scripts to be injected into the inherited template when rendering the page.

Understanding the contentFor helper

The contentFor helper complements the block helper by providing a means to access and populate block definitions within inherited templates.

  • utilize the {{#contentFor "block-name"}}...{{/contentFor}} syntax to define and fill the block definitions within the template being inherited.
  • the inherited template is referenced at the top of the file using {{!< template-name}}. This establishes the connection between the inherited template and the template containing the contentFor helper.
  • the content placed within the contentFor helper will be rendered into the corresponding block within the inherited template.

Here's how the contentFor definition looks like for the above example:

<!-- template file or partial file .hbs -->
{{#contentFor "scripts"}}
  <script>
    ...script here
  </script>
{{/contentFor}}

In this example, the content within the contentFor block will be added in place of the block when the page renders.

Use cases and benefits

The block and contentFor helpers provide several use cases and benefits that enhance the flexibility and customization of your Ghost CMS website. Here are some ideas how these helpers can be used in Ghost themes:

  1. Dynamic script injection

You have the possibility of injecting scripts or other dynamic content into specific sections of your templates, enhancing interactivity and functionality. In our themes we use this functionality to add table of contents or social sharing scripts.

  1. Custom CSS classes

You can dynamically assign different CSS classes to the body tag (or any other HTML tag) based on specific templates or content types, allowing for targeted styling and customization throughout your Ghost CMS theme.

  1. Meta tag changes

It's possible to dynamically change the meta tags for specific pages or posts within your Ghost theme. For example you can set different language or title tag based the post tag.

  1. Modular template structure

By using the block helper, you can create modular templates with defined slots, allowing for easy customization and content injection within inherited templates. For example, define a place for an ad unit in your default template and then use the contentFor in your post templates to place the ads based on certain conditions.

These are just some examples, but you can leverage these blocks for even more things.

Conclusion

The block and contentFor helpers in Ghost CMS provide powerful capabilities for creating customizable and modular templates. By utilizing these helpers, you can enhance the design, functionality, and interactivity of your website.

Whether you want to inject dynamic scripts, customize specific sections, or maintain consistency across your templates, the block and contentFor helpers offer the flexibility and control you need.