Elementor 开发者文档

title: "代码控制" post_status: publish comment_status: open taxonomy: category: - elementor-developers-docs post_tag: - Editor Controls - Src - Repos


代码控制

Code Control

Elementor 代码控制基于 Ace 编辑器 显示一个代码编辑器文本区域。它接受一个语言参数来定义编程 language,并包含语法高亮器以突出显示代码。

该控件在继承 Base_Data_Control 类的 Control_Code 类中定义。

使用此控件时,应将 type 设置为 \Elementor\Controls_Manager::CODE 常量。

参数

名称 类型 默认值 描述
type string code 控件的类型。
label string 显示在字段上方的标签。
description string 显示在字段下方的描述。
show_label bool true 是否显示标签。
label_block bool true 是否在单独一行显示标签。
separator string default 设置控件分隔符的位置。可用值为 defaultbeforeafterdefault 将隐藏分隔符,除非控件类型有特定的分隔符设置。before / after 会将分隔符定位在控件之前/之后。
language string html 任何 Ace 编辑器支持的语言
rows int 10 行数。
default string 字段的默认值。

Return Value

(string) The code field value.

Usage

```php {14-22,36,50}

start_controls_section( 'content_section', [ 'label' => esc_html__( 'Content', 'textdomain' ), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'custom_html', [ 'label' => esc_html__( 'Custom HTML', 'textdomain' ), 'type' => \Elementor\Controls_Manager::CODE, 'language' => 'html', 'rows' => 20, ] ); $this->end_controls_section(); } protected function render(): void { $settings = $this->get_settings_for_display(); if ( empty( $settings['custom_html'] ) ) { return; } ?>
    <div class="your-class">
        <?php echo $settings['custom_html']; ?>
    </div>
    <?php

}

protected function content_template(): void {
    ?>
    <#
    if ( '' === settings.custom_html ) {
        return;
    }
    #>
    <div class="your-class">
        {{{ settings.custom_html }}}
    </div>
    <?php
}

} ```