在 Twig 中呈现 HTML 个标题元素
Rendering HTML Heading Elements in Twig
我正在使用 twig 渲染一些标题,想知道是否有更好、更易读的方法来执行以下操作...
<{{ block.headingType }} class="measure-wide">{{ block.headingText }}</{{ block.headingType }}>
{{ block.headingType }} 是在编辑器中选择的标题的值。值为 h2、h3、h4、h5 等
HTML 标题的模板化方式看起来很难看(即使渲染有效)。有没有更好的方法根据所选值在 twig 中呈现标题标签?
如果你使用了很多 headers,我建议创建一个 class 让你处理这个并添加一个 toString
方法,这样更容易渲染标签
class Heading {
private $heading_type = 'h1';
private $heading_text;
private $classes = [];
public function __construct($text) {
$this->setHeadingText($text);
}
public function addClass($c) {
if (!in_array($c, $this->classes)) $this->classes[] = $c;
return $this;
}
public function getHtml() {
return new \Twig_Markup($this->__toString(), 'UTF-8');
}
public function __toString() {
return '<'.$this->getHeadingType().(!empty($this->getClasses()) ? ' class="'.implode(' ',$this->getClasses()).'"':'').'>'.$this->getHeadingText().'</'.$this->getHeadingType().'>';
}
/**============================================
GETTERS/SETTERS
============================================**/
public function setHeadingType($value) {
$this->heading_type = $value;
return $this;
}
public function getHeadingType() {
return $this->heading_type;
}
public function setHeadingText($value) {
$this->heading_text = $value;
return $this;
}
public function getHeadingText() {
return $this->heading_text;
}
public function getClasses() {
return $this->classes;
}
}
<?php
$twig->render('template.twig', [
'heading1' => new Heading('Title'),
'heading2' => (new Heading('Subtitle'))->setHeadingType('h2')
->addClass('foo'),
]);
{{ heading1 | raw }} {# out: <h1>Title</h1> #}
{{ heading2 | raw }} {# out: <h2 class="foo">Subtitle</h2> #}
编辑:添加了 getHtml
允许您删除原始过滤器,例如
{{ heading1.getHtml() }} {# out: <h1>Title</h1> #}
我正在使用 twig 渲染一些标题,想知道是否有更好、更易读的方法来执行以下操作...
<{{ block.headingType }} class="measure-wide">{{ block.headingText }}</{{ block.headingType }}>
{{ block.headingType }} 是在编辑器中选择的标题的值。值为 h2、h3、h4、h5 等
HTML 标题的模板化方式看起来很难看(即使渲染有效)。有没有更好的方法根据所选值在 twig 中呈现标题标签?
如果你使用了很多 headers,我建议创建一个 class 让你处理这个并添加一个 toString
方法,这样更容易渲染标签
class Heading {
private $heading_type = 'h1';
private $heading_text;
private $classes = [];
public function __construct($text) {
$this->setHeadingText($text);
}
public function addClass($c) {
if (!in_array($c, $this->classes)) $this->classes[] = $c;
return $this;
}
public function getHtml() {
return new \Twig_Markup($this->__toString(), 'UTF-8');
}
public function __toString() {
return '<'.$this->getHeadingType().(!empty($this->getClasses()) ? ' class="'.implode(' ',$this->getClasses()).'"':'').'>'.$this->getHeadingText().'</'.$this->getHeadingType().'>';
}
/**============================================
GETTERS/SETTERS
============================================**/
public function setHeadingType($value) {
$this->heading_type = $value;
return $this;
}
public function getHeadingType() {
return $this->heading_type;
}
public function setHeadingText($value) {
$this->heading_text = $value;
return $this;
}
public function getHeadingText() {
return $this->heading_text;
}
public function getClasses() {
return $this->classes;
}
}
<?php
$twig->render('template.twig', [
'heading1' => new Heading('Title'),
'heading2' => (new Heading('Subtitle'))->setHeadingType('h2')
->addClass('foo'),
]);
{{ heading1 | raw }} {# out: <h1>Title</h1> #}
{{ heading2 | raw }} {# out: <h2 class="foo">Subtitle</h2> #}
编辑:添加了 getHtml
允许您删除原始过滤器,例如
{{ heading1.getHtml() }} {# out: <h1>Title</h1> #}