定制 louis-dj/xml-template 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

louis-dj/xml-template

Composer 安装命令:

composer require louis-dj/xml-template

包简介

Xml templating engine for dynamic xml string generation in php

README 文档

README

Generate dynamic xml strings using templated xml files.

Screenshot 2025-03-06 at 21 35 16

Motivation

Avoid building dynamic xml strings with messy string concatenations in your code.
Avoid adding static boilerplate elements when using a useful package like ArrayToXml.

Usage

Install the package with composer:

composer install louis-dj/xml-template

Create an xml file with templating directives

<xml>
  {{ var randomVar }}
</xml>

Import the package and convert with the replaceWith method

<?php
use LouisDj\XmlTemplate\XmlTemplate;

$template = new XmlTemplate('./test.xml');
$output = $template->replaceWith(['randomVar' => 'Some string']);

Absolute filepaths AND relative filepaths(relative to cwd) are supported

Features

  • Minified: The replaceWith method has an optional boolean parameter minified to remove all newline characters from the xml
  • Object & Array compatibility: The associative array passed to the replaceWith method can contain string keys OR objects with field names corresponding to the variables OR both
  • See the syntax below for full feature set

Syntax & Rules

  • All directives must be enclosed in {{}} and contain a space on the inside of both pairs
  • Variable names use a global scope, even variables defined in foreach directives
  • All directives can be freely nested as long as it is property closed with an end directive
  • if, else, foreach and end directives must be on their own line, but may be indented.

Variable Directives

<xml>{{ var myVariableName }}</xml>

If statements

  • All if statements must be closed by an end directive
{{ if booleanValue }}
<conditionalcontent></conditionalcontent>
{{ end }}

If else statements

  • All if else statements must be closed by an end directive
{{ if booleanValue }}
<True></True>
{{ else }}
<False></False>
{{ end }}

Foreach statements

  • All foreach statements must be closed by an end directive
{{ foreach countableVariable }}
<item>This will appear n=(length of countableVariable) times</item>
{{ end }}

Foreach as statements

  • All foreach as statements must be closed by an end directive
{{ foreach arrayVariable as arrayItem }}
<item>The array contains: {{ var arrayItem }}</item>
{{ end }}

Attribute access

  • The dot (.) operator works to access associative array keys' values too.
<xml>{{ var someObjectOrArray.attrName }}</xml>

Pass in Objects to the replaceWith method

  • View tests/Test.php for more detailed use cases
$subbed = $template->replaceWith([
  'book' => new Book('The Great Gatsby', '1234')
]);

Testing

Testing is done with phpunit and all tests are contained in the tests/ directory.

Run tests with:

vendor/bin/phpunit

louis-dj/xml-template 适用场景与选型建议

louis-dj/xml-template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 louis-dj/xml-template 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 louis-dj/xml-template 我们能提供哪些服务?
定制开发 / 二次开发

基于 louis-dj/xml-template 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-06