Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use dynamic class names #8

Open
morgannunan opened this issue Aug 8, 2024 · 0 comments
Open

Can't use dynamic class names #8

morgannunan opened this issue Aug 8, 2024 · 0 comments

Comments

@morgannunan
Copy link

morgannunan commented Aug 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant