CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 纪念品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题(GitHub)
    • 面包店
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 新闻通讯
    • LinkedIn
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • HTTP
    • 国际化
    • 日志
    • 邮件
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用程序
    • 验证
    • 视图
      • 异常
      • 表单
      • 助手
      • 小部件

类 StringTemplate

提供一个接口,用于在简单的无逻辑字符串模板中注册和插入内容。

由多个助手使用,为生成 HTML 和其他内容提供简单灵活的模板。

命名空间: Cake\View

属性摘要

  • $_compactAttributes protected
    array<string, bool>

    可以压缩的属性列表。

  • $_compiled protected
    array<string, array>

    包含已编译模板的列表

  • $_config protected
    array<string, mixed>

    运行时配置

  • $_configInitialized protected
    bool

    配置属性是否已使用默认值配置

  • $_configStack protected
    array

    已暂时存储的模板集的堆栈。

  • $_defaultConfig protected
    array<string, mixed>

    此实例持有的默认模板。

方法摘要

  • __construct() public

    构造函数。

  • _compileTemplates() protected

    将模板编译成更有效的 printf() 兼容格式。

  • _configDelete() protected

    删除单个配置键。

  • _configRead() protected

    读取配置键。

  • _configWrite() protected

    写入配置键。

  • _formatAttribute() protected

    格式化单个属性,并返回组合属性的字符串值。适用于最小化的属性,其值与其名称相同,例如 'disabled' 和 'checked'

  • add() public

    按名称注册模板列表

  • addClass() public

    添加一个类并返回一个唯一的列表,以数组或以空格分隔的形式。

  • configShallow() public

    将提供的配置与现有配置合并。与 config() 不同,config() 对嵌套键进行递归合并,此方法进行简单的合并。

  • format() public

    使用 $data 格式化模板字符串

  • formatAttributes() public

    返回一个空格分隔的字符串,其中包含 $options 数组的项。如果 $options 数组的键恰好是 StringTemplate::$_compactAttributes 中列出的键之一,并且其值为

  • getConfig() public

    返回配置。

  • getConfigOrFail() public

    返回此特定键的配置。

  • load() public

    加载包含模板的配置文件。

  • pop() public

    恢复最近推送的模板集。

  • push() public

    将当前模板推入模板堆栈。

  • remove() public

    删除名为模板。

  • setConfig() public

    设置配置。

方法详细说明

__construct() ¶ public

__construct(array<string, mixed> $config = [])

构造函数。

参数
array<string, mixed> $config optional

要添加的一组模板。

_compileTemplates() ¶ protected

_compileTemplates(list<string> $templates = []): void

将模板编译成更有效的 printf() 兼容格式。

参数
list<string> $templates optional

要编译的模板名称。如果为空,将编译所有模板。

返回值
void

_configDelete() ¶ protected

_configDelete(string $key): void

删除单个配置键。

参数
string $key

要删除的键。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_configRead() ¶ protected

_configRead(string|null $key): mixed

读取配置键。

参数
string|null $key

要读取的键。

返回值
mixed

_configWrite() ¶ protected

_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void

写入配置键。

参数
array<string, mixed>|string $key

要写入的键。

mixed $value

要写入的值。

string|bool $merge optional

如果为 true,则递归合并,如果为 'shallow',则简单合并,如果为 false,则覆盖,默认值为 false。

返回值
void
抛出
Cake\Core\Exception\CakeException
如果尝试覆盖现有配置

_formatAttribute() ¶ protected

_formatAttribute(string $key, mixed $value, bool $escape = true): string

格式化单个属性,并返回组合属性的字符串值。适用于最小化的属性,其值与其名称相同,例如 'disabled' 和 'checked'

参数
string $key

要创建的属性的名称

mixed $value

要创建的属性的值。

bool $escape optional

定义值是否必须转义

返回值
string

add() ¶ public

add(array<string, string> $templates): $this

按名称注册模板列表

示例

$templater->add([
  'link' => '<a href="{{url}}">{{title}}</a>'
  'button' => '<button>{{text}}</button>'
]);
参数
array<string, string> $templates

命名模板的关联列表。

返回值
$this

addClass() ¶ public

addClass(mixed $input, list<string>|string|false|null $newClass, string $useIndex = 'class'): list<string>|string|null

添加一个类并返回一个唯一的列表,以数组或以空格分隔的形式。

参数
mixed $input

要添加类的数组或字符串

list<string>|string|false|null $newClass

要添加的新类或类

string $useIndex optional

如果你输入的是一个数组,其中包含除默认 'class' 以外的元素。

返回值
list<string>|string|null

configShallow() ¶ public

configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this

将提供的配置与现有配置合并。与 config() 不同,config() 对嵌套键进行递归合并,此方法进行简单的合并。

设置特定值

$this->configShallow('key', $value);

设置嵌套值

$this->configShallow('some.nested.key', $value);

同时更新多个配置设置

$this->configShallow(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键,或完整的配置数组。

mixed|null $value optional

要设置的值。

返回值
$this

format() ¶ public

format(string $name, array<string, mixed> $data): string

使用 $data 格式化模板字符串

参数
string $name

模板名称。

array<string, mixed> $data

要插入的数据。

返回值
string
抛出
InvalidArgumentException
如果找不到模板。

formatAttributes() ¶ public

formatAttributes(array<string, mixed>|null $options, list<string>|null $exclude = null): string

返回一个空格分隔的字符串,其中包含 $options 数组的项。如果 $options 数组的键恰好是 StringTemplate::$_compactAttributes 中列出的键之一,并且其值为

  • '1' (字符串)
  • 1 (整数)
  • true (布尔值)
  • 'true' (字符串)

那么该值将被重置为与键的名称相同。如果该值不是这 4 个中的一个,则该参数将不会输出。

'escape' 是一个特殊选项,它控制将属性转换为其 HTML 实体编码的等效项。设置为 false 可禁用 HTML 编码。

如果任何选项键的值设置为 null 或 false,则该选项将从输出中排除。

此方法使用 'attribute' 和 'compactAttribute' 模板。这两个模板都使用 name 和 value 变量。可以修改这些模板以更改属性的格式。

参数
array<string, mixed>|null $options

选项数组。

list<string>|null $exclude 可选

要排除的选项数组,这些选项将不会包含在返回值中。

返回值
string

getConfig() ¶ public

getConfig(string|null $key = null, mixed $default = null): mixed

返回配置。

用法

读取整个配置

$this->getConfig();

读取特定值

$this->getConfig('key');

读取嵌套值

$this->getConfig('some.nested.key');

读取默认值

$this->getConfig('some-key', 'default-value');
参数
string|null $key 可选

要获取的键,如果要获取整个配置,则为 null。

mixed $default 可选

当键不存在时的返回值。

返回值
mixed

getConfigOrFail() ¶ public

getConfigOrFail(string $key): mixed

返回此特定键的配置。

此键的配置值必须存在,永远不能为 null。

参数
string $key

要获取的键。

返回值
mixed
抛出
InvalidArgumentException

load() ¶ public

load(string $file): void

加载包含模板的配置文件。

模板文件应定义一个 $config 变量,其中包含要加载的所有模板。加载的模板将与现有模板合并。

参数
string $file

要加载的文件

返回值
void

pop() ¶ public

pop(): void

恢复最近推送的模板集。

返回值
void

push() ¶ public

push(): void

将当前模板推入模板堆栈。

返回值
void

remove() ¶ public

remove(string $name): void

删除名为模板。

参数
string $name

要删除的模板。

返回值
void

setConfig() ¶ public

setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this

设置配置。

用法

设置特定值

$this->setConfig('key', $value);

设置嵌套值

$this->setConfig('some.nested.key', $value);

同时更新多个配置设置

$this->setConfig(['one' => 'value', 'another' => 'value']);
参数
array<string, mixed>|string $key

要设置的键,或完整的配置数组。

mixed|null $value optional

要设置的值。

bool $merge 可选

是否递归合并或覆盖现有配置,默认为 true。

返回值
$this
抛出
Cake\Core\Exception\CakeException
当尝试设置一个无效的键时。

属性详细信息

$_compactAttributes ¶ protected

可以压缩的属性列表。

类型
array<string, bool>

$_compiled ¶ protected

包含已编译模板的列表

类型
array<string, array>

$_config ¶ protected

运行时配置

类型
array<string, mixed>

$_configInitialized ¶ protected

配置属性是否已使用默认值配置

类型
bool

$_configStack ¶ protected

已暂时存储的模板集的堆栈。

类型
array

$_defaultConfig ¶ protected

此实例持有的默认模板。

类型
array<string, mixed>
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题(GitHub)
  • 面包店
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 新闻通讯
  • LinkedIn
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成