How to Use Bemto: A Guide to Writing BEM-Style Code in Pug

Jul 14, 2023 | Programming

Bemto is a powerful mixin that simplifies writing BEM-style code in Pug. If you’re looking to create reusable, maintainable components using the BEM methodology, this guide will navigate you through the installation, features, and usage of Bemto. Let’s embark on this coding adventure together!

Table of Contents

Install & Use

Cloning

1. Clone Bemto somewhere in your project:
sh
git clone git@github.com:kizubemto.git

2. Include it in your `.pug` project:
Pug
include bemto/bemto.pug

3. Use it:
Pug
+b.block1
  +e.element1 Foo
  +b.block2
    +e.A(href=#bar).element Bar
  +e.element2 Baz

This code snippet will create a nested structure resembling BEM methodology in HTML, efficiently organizing your components.

Features

Blocks

In Bemto, a “Block” is akin to the cornerstone of your structure—it initiates the context for all nested elements. Think of it as the foundation of a house where all the rooms are built. You can create blocks using the b mixin with a specific class:

+b.foo bar

This will render as:

<div class="foo">bar</div>

Elements

Elements are like the rooms in your house—dependent on the block yet unique in their purpose. Using the e mixin inside a block context allows you to create accessorial elements:

+b.foo
  +e.bar baz

Resulting in:

<div class="foo">
  <div class="foo__bar">baz</div>
</div>

Modifiers

Modifiers indicate the variation of a block or an element, similar to adjusting the decor of your rooms to match different moods. With Bemto, you can easily create modifiers without redundancy. For example:

+b.block_foo bar

This is rendered as:

<div class="block block_foo">bar</div>

Settings

Bemto comes with a series of settings to customize your BEM syntax. For instance, you can choose to add a prefix to all your blocks which allows for personalized organization:

set_bemto_settings(
  prefix: 'b-',
  element: '__',
  modifier: '_'
)

Using for Building Complex Mixins

Building complex mixins with Bemto is made easy due to its structure. You can create a mixin with links and custom attributes to keep your code DRY:

mixin link(url)
  +b.SPAN.link(href=url)attributes(attributes)
    block

This modularity enhances code reusability and maintainability, making it perfect for developing intricate web applications.

Troubleshooting

If you encounter problems, consider checking your syntax for correct mixin usage. Ensure your block and element classes do not conflict with existing classes, and confirm that you’ve included the Bemto mixin correctly in your Pug project.

For persistent issues, consult the documentation or seek assistance from the community. For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox