gpt4 book ai didi

php - 类属性中的多个类,一行最多 80 个字符

转载 作者:太空宇宙 更新时间:2023-11-04 02:16:07 24 4
gpt4 key购买 nike

我需要向 class 属性添加许多长类,所以现在它看起来像这样:

<div class="single_text_block--content single_text_block--content--60p single_text_block--content--bigger center-block text_content">
/* content */
</div>

很难以这种方式维护它。在一行代码中设置最多 80 个字符的同时在编辑器中格式化此代码的最佳方法是什么?

我试过这种方法:

<div class="
single_text_block--content
single_text_block--content--60p
single_text_block--content--bigger
center-block text_content
">

但它会产生很多空间。这是在内部浏览器中的样子:

enter image description here

我在这里使用 Twig 作为我的 View 模板引擎,所以我使用了 {% spaceless %} 但它只适用于 HTML 标签并且正在删除所有空格(我需要的是将多个空格更改为一个)。

最佳答案

您可以通过为 Twig 创建自己的 Tag 来实现一些目标。
下面我已经为你创建了标签,目前类属性的"前后还有一个额外的空间:

 <div class=" single_text_block--content single_text_block--content--60p single_text_block--content--bigger center-block text_content "> 

Here是关于标签以及如何将它们注册到 Twig

的一些更深入的细节

MyProject/Twig/MyTwigExtension.php

<?php
namespace MyProject\Twig;

class MyTwigExtension extends Twig_Extension {

public function getTokenParsers() {
return [
new \MyProject\TokenParser\OneLine(),
];
}

public function getName() {
return 'MyTwigExtension';
}

}

MyProject/Twig/TokenParser/OneLine.php

<?php
namespace MyProject\Twig\TokenParser;

use \MyProject\Twig\Node\OneLine as OneLineNode;

class OneLine extends \Twig_TokenParser
{
public function decideOneLineEnd(\Twig_Token $token)
{
return $token->test('endoneline');
}

public function getTag()
{
return 'oneline';
}

public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideOneLineEnd'), true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);

return new OneLineNode($body, $lineno, $this->getTag());
}
}

MyProject/Twig/Node/OneLine.php

<?php
namespace MyProject\Twig\Node;

class OneLine extends \Twig_Node {
public function __construct(\Twig_NodeInterface $body, $lineno, $tag = null) {
parent::__construct(['body' => $body,], array(), $lineno, $tag);
}

public function compile(\Twig_Compiler $compiler) {
$compiler
->addDebugInfo($this)
->write('ob_start();')
->write(PHP_EOL)
->subcompile($this->getNode('body'))
->write(PHP_EOL)
->write('echo str_replace(PHP_EOL, \' \', preg_replace(\'!\s+!\', \' \', ob_get_clean()));')
->write(PHP_EOL);
}
}

template.twig.html

{% oneline %}
<div class="
single_text_block--content
single_text_block--content--60p
single_text_block--content--bigger
center-block text_content
">
{% endoneline %}

关于php - 类属性中的多个类,一行最多 80 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38890758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com