PHPackages                             pug/bemto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pug/bemto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pug/bemto
=========

Smart mixins for writing BEM in Pug

1.2.0(2mo ago)333MITPugPHP &gt;=7.0CI failing

Since Nov 18Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/phug-php/bemto)[ Packagist](https://packagist.org/packages/pug/bemto)[ Fund](https://opencollective.com/pug-php)[ Fund](https://tidelift.com/funding/github/packagist/pug/bemto)[ RSS](/packages/pug-bemto/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (5)Dependencies (12)Versions (6)Used By (0)

Bemto
=====

[](#bemto)

[![Latest Stable Version](https://camo.githubusercontent.com/a224ebf1b64960c8468047803f12355bf0c8ef4ec94b33a327a5b2b0db4e264c/68747470733a2f2f706f7365722e707567782e6f72672f7075672f62656d746f2f762f737461626c652e706e67)](https://packagist.org/packages/pug/bemto)[![License](https://camo.githubusercontent.com/613c944708fb8c35f438e07c22b9dfc4716f8417bfe69590a50afa53048a30cb/68747470733a2f2f706f7365722e707567782e6f72672f7075672f62656d746f2f6c6963656e7365)](https://packagist.org/packages/pug/bemto)[![Build Status](https://camo.githubusercontent.com/44c3bf1d71426eba18f39f0bfa991f55c2bf97c1651e8e9f54ca22cbaca31b02/68747470733a2f2f7472617669732d63692e6f72672f706875672d7068702f62656d746f2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/phug-php/bemto)[![StyleCI](https://camo.githubusercontent.com/5d323317a5b8014c7d32f55e7cc53cb51e386dd632e2aceded91a00918a6d1d3/68747470733a2f2f7374796c6563692e696f2f7265706f732f3135333134303439392f736869656c643f7374796c653d666c6174)](https://styleci.io/repos/153140499)[![Test Coverage](https://camo.githubusercontent.com/d81f8569622ac97defdb7a653010dcee4a537dfff78a69b50a1b4340a46b767e/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f706875672d7068702f62656d746f2f6261646765732f636f7665726167652e737667)](https://codecov.io/github/phug-php/bemto?branch=master)[![Code Climate](https://camo.githubusercontent.com/558ef1b34444ee416b79a55fea79c3e133491d051a6ef83b56b31ef7076574cb/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f706875672d7068702f62656d746f2f6261646765732f6770612e737667)](https://codeclimate.com/github/phug-php/bemto)[![Dependencies](https://camo.githubusercontent.com/656b20f3ab8c570c017f96b80127439759d4d77c374898f70151c8dbcacad526/68747470733a2f2f746964656c6966742e636f6d2f6261646765732f6769746875622f7075672d7068702f707567)](https://tidelift.com/subscription/pkg/packagist-pug-php-pug?utm_source=packagist-pug-php-pug&utm_medium=referral&utm_campaign=readme)[![Backers on Open Collective](https://camo.githubusercontent.com/eaa4a8494c8d08b17406c170b353e539c2b009c48079afe155d498001b984816/68747470733a2f2f6f70656e636f6c6c6563746976652e636f6d2f7075672d7068702f6261636b6572732f62616467652e737667)](#backers)[![Sponsors on Open Collective](https://camo.githubusercontent.com/e2aad488327620a86ad0913448186e9401d93ebca00b3d42deff02924667538f/68747470733a2f2f6f70656e636f6c6c6563746976652e636f6d2f7075672d7068702f73706f6e736f72732f62616467652e737667)](#sponsors)

Smart mixins for writing BEM in Pug-php.

This project is a port of [kizu/bemto](https://github.com/kizu/bemto) for [Phug](https://www.phug-lang.com)and it's also compatible with [Pug-php](https://github.com/pug-php/pug).

Table of Contents
-----------------

[](#table-of-contents)

1. [Install &amp; Use](#install--use)
2. [Features](#features)

    - [Blocks](#blocks)
    - [Elements](#elements)
    - [Modifiers](#modifiers)
    - [Changing the tag name](#changing-the-tag-name)
    - [Attributes](#attributes)
    - [Context](#context)
    - [Redefining tag's metadata](#redefining-tags-metadata)
3. [Settings](#settings)

    - [Adding Prefix](#adding-prefix)
    - [Setting for Element syntax](#setting-for-element-syntax)
    - [Setting for Modifier syntax](#setting-for-modifier-syntax)
    - [Setting for allowing nested elements](#setting-for-allowing-nested-elements)
    - [Scope for the settings](#scope-for-the-settings)
    - [Setting for the output syntax of the elements/modifiers](#setting-for-allowing-nested-elements)
4. [Using for building complex mixins](#using-for-building-complex-mixins)

Install &amp; Use
-----------------

[](#install--use)

```
composer require pug/bemto
```

```
// Working with Pug-php:
Pug::addExtension(PhugBemto\PhugBemto::class);
// Working with Phug:
Phug::addExtension(PhugBemto\PhugBemto::class);
```

This will make bemto features available in your tempaltes:

```
+b.block1
  +e.element1 Foo
  +b.block2
    +e.A(href="#bar").element Bar
  +e.element2 Baz
```

would render to something like

```

    Foo

    Bar

    Baz

```

Features
--------

[](#features)

### Blocks

[](#blocks)

“Block” is the main thing there. It creates the block from the first passed class and creates the context for all nested elements.

You can create block calling the `b` mixin with some class attached to it:

```
+b.foo bar
```

That would render as

```
bar
```

While the simple block's syntax, of course, is harder than the simple Pug's tags, the main point is to create the contexts for elements.

### Elements

[](#elements)

“Element” is a accessory element of the block. You can read on the concept of the elements in the [bem methodology](http://bem.github.com/bem-method/pages/beginning/beginning.en.html#Element), or in the [great article by Nicolas Gallagher](http://nicolasgallagher.com/about-html-semantics-front-end-architecture/) (see the “Structured class names” part). Elements often are written using the block's name plus element's name with some separator (often `__` or `-`) in-between.

Bemto gives a convenient way to declare the elements: just use the `e` mixin inside any block context:

```
+b.foo
  +e.bar baz
```

This would render like

```

  baz

```

#### Context of the element

[](#context-of-the-element)

Note that bemto uses the first classname of the block as a context for further elements. If you'd like to use another class without changing the order, you can mark it with `__` in the end:

```
+b.foo.bar__
  +e.baz
```

This way instead of `foo` bemto would base the nested elements from the `bar`:

```

```

### Modifiers

[](#modifiers)

“Modifier” is a state of the block or element. It is often written with the addition of it's type and/or value after the single underscore like `block_mode_foo` or just `block_foo`. However, at the most cases, the block must contain either the original block/element's class, either the modifier class.

Bemto makes it easy to write such modifiers, 'cause you don't need now to write the same block's name twice:

```
+b.block_foo bar
```

Becomes

```
bar
```

See? You write just `.block_foo` but by fact get the `.block.block_foo` instead!

But what if you need to have more than one modifier on one block or element? Bemto have a way to do so: add a class to your block or element starting with a modifier token:

```
+b.block_foo._bar._baz
  +e.element_type_lol._mode_moddy Blah
```

and that would render as

```

    Blah

```

You can also use shorter modifier syntax like `class="block -modifier"` (but only when this syntax won't be used for delimiting full modifiers or elements).

```
+b.-foo.-bar.-baz
```

this would render to

```

```

### Changing the tag name

[](#changing-the-tag-name)

By default the blocks and elements render as `div`s. You can change it by passing the desired tag name as the first class in uppercase:

```
+b.SPAN.foo bar
```

Or by passing an `options` object with a `tag` param:

```
+b({tag: 'span'})foo bar
```

Either way would render as

```
bar
```

### Attributes

[](#attributes)

Like any Pug tag or mixin, blocks and elements can take attributes that would go to the desired tags:

```
+b.foo(title="Oh, it's a title")
  +e.A.bar(href='#baz') baz
```

would render like

```

  baz

```

#### Automatic attributes

[](#automatic-attributes)

There are some tags like `img` that must have at least one attribute set. Bemto would create attributes with some predefined values for such tags. So, for images this code — `+b.image(src="foo.png")` would render `` — you can see that in that case there is the added empty `alt`.

Also, in some cases there is a need to adjust some attributes according to other ones. For `img` if the `alt` is set, but the `title` is not we'd need to set it to empty, 'cause there'd be a inconsistency between browsers (IE would show the `title` bubble for `alt`). And from the other side, if there is only `title` set in an image, we'd need to clone it to `alt`. Bemto do all those things.

### Context

[](#context)

Look at the previous example: you have there some excess code that you can throw away. It's the `('a')` part — as long as you set the `href` attribute, the block would automagically become the link. Also, there are other tags that you can omit: `li` in `ul` or `ol` context, or `span` in any already inline context.

So, here is a bigger example:

```
+b.UL.list
  +b.list-item
    +e.link(href="foo")
      +e.text foo
  +b.list-item
    +e.link(href="bar")
      +e.text bar
```

Would render to

```

      foo

      bar

```

For now that's all, but there would be other contexts in the future of bemto.

### Redefining tag's metadata

[](#redefining-tags-metadata)

In a case you'd like some tag to set a different context, i.e. to override it's content\_type, you can use a `metadata` option for the block/element. For example, if you'd like a link to have block context, you can redefine it in this way:

```
+b({ metadata: { content_type: 'block' } }).A.foo
  +e.bar
```

would render as

```

baz

```

Settings
--------

[](#settings)

There are some settings you can set for different syntaxes of BEM.

For doing so, you must set them after including the `bemto` like this:

```
-
  set_bemto_settings({
    prefix: '',
    element: '__',
    modifier: '_'
  })
```

Here you can see all available settings with their default values.

### Adding Prefix

[](#adding-prefix)

If you'd like to prefix all your bemto-generated blocks, you can set the `prefix` setting to a String or an Object.

#### Strings for prefix setting

[](#strings-for-prefix-setting)

If you'd set a string, it would be just prepended to the names of all blocks:

```
- set_bemto_settings({ prefix: 'b-' })

+b.block
  +e.element foo
```

and that would then render as

```

    foo

```

Note that if you had already used this prefix in a classname, it won't be added, so you won't have occasional duplicated prefixes.

#### Objects for prefix setting

[](#objects-for-prefix-setting)

If you'd want to have more control over prefixes, you can use a special object instead of a string:

```
-
  set_bemto_settings({
    prefix: {
      '': 'b-',
      'js-': true,
      'is-': 'is-',
      'global-': '',
      'nope-': false,
      'sc-': 'shortcut-'
    }
  })
```

Look at the above example, it have all the variations the prefix object accepts:

- The empty string for key works the same as a string setting: you'd get the value for this key prepended for all classnames without detected prefixes.
- If a value for any key is `true`, this prefix would be always treated as such and won't be prepended by other prefixes.
- If a value for a key is `false` or an empty string, the classnames with this prefix would be rendered without it.
- In other cases, where the key and the value are both strings, all the key prefixes in the source code would be replaced with the value ones, and also all of those prefixes would be treated as registered ones, so you wouldn't add other prefixes for them.

### Setting for Element syntax

[](#setting-for-element-syntax)

If you don't like the default elements syntax with the `__` delimiter, you can set using the `element` setting:

```
- set_bemto_settings({ element: '-' })

+b.block
  +e.element foo
```

this would render to

```

    foo

```

### Setting for Modifier syntax

[](#setting-for-modifier-syntax)

If you'd like to use different modifier syntax, like the one Nicolas Gallagher mentioned in [his article](http://nicolasgallagher.com/about-html-semantics-front-end-architecture/), you can use the `modifier` setting:

```
- set_bemto_settings({ modifier: '--' })

+b.block--modifier-name.--other-modifier foo
```

and that would expand to

```

  foo

```

### Setting for allowing nested elements

[](#setting-for-allowing-nested-elements)

There can be cases when you could want to make elements of elements, i.e. when using element names instead of block names:

```
+b.block-element
  +e.element2
```

renders by default to

```

```

If you'd like to have `foo__bar__baz` in the output instead, you can set the `flat_elements` to `false`:

```
- set_bemto_settings({ flat_elements: false })

+b.foo__bar
  +e.baz
```

This would render with the nested element:

```

```

### Scope for the settings

[](#scope-for-the-settings)

If you'll need to have some settings just in a certain scope, you can wrap your code in `bemto_scope` mixin, passing your desired settings right into it:

```
+b.foo_bar
  +bemto_scope({
      prefix: 'b-',
      element: '-',
      modifier: '--'
    })
    +b.nnnn
      +e.mmmm--kkkk
  +e.baz
```

Would render as

```

```

### Setting for the output syntax of the elements/modifiers

[](#setting-for-the-output-syntax-of-the-elementsmodifiers)

If you'd like to use different syntax of the element/modifier delimiter in Pug source and the html output, you can use the `ouput_element` and `output_modifier` settings, otherwise the same delimiter as in the `element` and `modifier` settings would be used.

```
-
  set_bemto_settings({
    element: '-',
    modifier: '--',
    output_element: '__',
    output_modifier: '_'
  })

+b.block.block2-
  +e.element--modifier foo
```

would output

```

  foo

```

You can see how the source uses the `-` for element and `--` for modifier, but the result gets `__` and `_` instead.

### Setting for delimiters between classnames

[](#setting-for-delimiters-between-classnames)

If you'd like to have extra delimieters between the rendered classnames for clarity, you can use a `class_delimiter` setting:

```
- set_bemto_settings({ class_delimiter: '|' })

+b.foo.bar_baz
```

Would be rendered as

```

```

Note that there would be always added spaces around the given delimiter, so you don't need to include them in the setting's value.

Using for building complex mixins
---------------------------------

[](#using-for-building-complex-mixins)

This is somewhat obvious, but I must mention that the bemto blocks would be great for using as the bricks for building more complex blocks. The Pug mixins work in the way where you can translate any attributes through to the any inner blocks. So you can do this:

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

And then use it in this way:

```
+link('#Foo') Foo

+link('https://github.com')._external Github

+link('http://kizu.ru').url(rel="me") Here I am

+link Ah, I'm not a link

+link('https://github.com')
  +e.icon(src="http://favicon.yandex.net/favicon/github.com")
  +e.text Github
```

And that would render to

```
Foo

Github

Here I am

Ah, I'm not a link

  Github

```

There you can see almost all of the bemto features that can be used for any mixin with `attributes` variable attached to any inner bemto block inside of it.

Security contact information
----------------------------

[](#security-contact-information)

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.

Sponsor
-------

[](#sponsor)

[Professionally supported pug/bemto](https://tidelift.com/subscription/pkg/packagist-pug-bemto?utm_source=packagist-pug-bemto&utm_medium=referral&utm_campaign=readme)

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance88

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~668 days

Total

5

Last Release

60d ago

Major Versions

0.1.0 → 1.0.02020-04-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/df90c422f521bf26355c3e185a59fe7afb35531af56244dfaec03ee1d0ca6f24?d=identicon)[kylekatarn](/maintainers/kylekatarn)

---

Top Contributors

[![kylekatarnls](https://avatars.githubusercontent.com/u/5966783?v=4)](https://github.com/kylekatarnls "kylekatarnls (32 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pug-bemto/health.svg)

```
[![Health](https://phpackages.com/badges/pug-bemto/health.svg)](https://phpackages.com/packages/pug-bemto)
```

###  Alternatives

[kris/laravel-form-builder

Laravel form builder - symfony like

1.7k2.2M45](/packages/kris-laravel-form-builder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
