Skip to content

Can't use dynamic class names #8

@morgannunan

Description

@morgannunan

I am really enjoying Pug to Twig, but it doesn't want to create dynamic class names. For instance, if I write

html(lang=site.language)

It will output

<html lang="{{ site.language | e }}">

Which I can then process with twig. However, if I write this:

html(class=site.language)

I'll get this:

<?php $pugModule = [
  'Phug\\Formatter\\Format\\TwigHtmlFormat::class_attribute_assignment' => function (&$attributes, $value) use (&$pugModule) {

            $split = function ($input) {
                return preg_split('/(?<![\[\{\<\=\%])\s+(?![\]\}\>\=\%])/', strval($input));
            };
            $classes = isset($attributes['class']) ? array_filter($split($attributes['class'])) : [];
            foreach ((array) $value as $key => $input) {
                if (!is_string($input) && is_string($key)) {
                    if (!$input) {
                        continue;
                    }

                    $input = $key;
                }
                foreach ($split($input) as $class) {
                    if (!in_array($class, $classes, true)) {
                        $classes[] = $class;
                    }
                }
            }

            return implode(' ', $classes);
        },
  'Phug\\Formatter\\Format\\TwigHtmlFormat::stand_alone_class_attribute_assignment' => function ($value) use (&$pugModule) {
    $classAttributeAssignment = $pugModule['Phug\\Formatter\\Format\\TwigHtmlFormat::class_attribute_assignment'];

                    $attributes = [];

                    return $classAttributeAssignment($attributes, $value);
                },
]; ?><!DOCTYPE html><html class="<?= $pugModule['Phug\\Formatter\\Format\\TwigHtmlFormat::stand_alone_class_attribute_assignment'](site.language) | e ?>">

It will always put this PHP at the top of the page, and inline where the class value should be. If I write html(class='home') it works fine, but if the class value is a variable, it goes haywire.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions