uom / tk-domtemplate
使用 XML/HTML 文件作为模板的 PHP DOM 模板库
Requires
- php: ^7.3||^8.0
- ext-dom: *
- ext-tidy: *
- scssphp/scssphp: 1.0.*
- wikimedia/less.php: 1.7.*
- 3.2.x-dev
- 3.2.4
- 3.2.2
- 3.2.0
- 3.0.x-dev
- dev-master / 3.0.x-dev
- 3.0.36
- 3.0.34
- 3.0.32
- 3.0.30
- 3.0.28
- 3.0.26
- 3.0.24
- 3.0.22
- 3.0.20
- 3.0.18
- 3.0.16
- 3.0.14
- 3.0.10
- 3.0.8
- 3.0.6
- 3.0.4
- 3.0.2
- 3.0.0
- 2.2.64
- 2.2.62
- 2.2.60
- 2.2.58
- 2.2.56
- 2.2.54
- 2.2.52
- 2.2.50
- 2.2.48
- 2.2.46
- 2.2.44
- 2.2.42
- 2.2.40
- 2.2.38
- 2.2.36
- 2.2.34
- 2.2.32
- 2.2.30
- 2.2.28
- 2.2.26
- 2.2.24
- 2.2.22
- 2.2.20
- 2.2.18
- 2.2.16
- 2.2.14
- 2.2.12
- 2.2.10
- 2.2.8
- 2.2.6
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.14
- 2.0.13
- 2.0.12
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- dev-ver2
- dev-ver1
This package is auto-updated.
Last update: 2024-09-30 02:07:58 UTC
README
项目: uom/tk-domtemplate 网站: http://www.domtemplate.com/
作者: Michael Mifsud http://www.tropotek.com/
PHP5 DOM 模板引擎,用于 XHTML/XML
内容
安装
可在 Packagist (uom/tk-domtemplate) 上找到,并可通过 Composer 安装。
composer require uom/tk-domtemplate
或者将以下内容添加到您的 composer.json 文件中
"uom/tk-domtemplate": "~3.0"
如果您不使用 Composer,您可以从 GitHub 下载代码,并使用任何 PSR-0 兼容的自动加载器来加载类。
简介
注意:此引擎使用 PHP DOM 模块,该模块要求所有加载到其中的文档都必须是严格的 XML/XHTML 标记。关闭所有标签,并确保所有 & 都是 &,即使在 URL 查询字符串中也是如此。
DOM 模板引擎已开发,以便设计师有一种简单的方式来构建模板,并将他们的需求传达给开发者。
模板引擎使用了三个自定义属性。这些是
不必担心这些属性不符合 HTML5 规范或其他规范,因为一旦解析模板,它们就会被删除。
这就是从设计师的角度来看的全部内容。对于开发者来说,这使与 HTML 模板交互变得容易,而不会覆盖设计师的辛勤工作。
PHP DOMTemplate 还提供了一些其他功能,这些功能有助于渲染表单、CSS、JavaScript 元标签等。以下各节将说明如何使用这些功能。还可以查看代码示例,了解我们如何使用 DOMTemplate。
VAR
这是 var
属性。如果您想修改节点的内容或属性,则可以在节点中使用它。以下是一个在模板中使用 var
的示例
<div><a href="#" var="link"></a></div>
开发者可以构建代码来操纵此节点,使其满意
<?php // Load a new template from a file. (The file must be XHTML valid or errors will be produced) $template = new \Dom\Template::loadFile('index.html'); // Add some text content inside the anchor node $template->insertText('link', 'This is a link'); //Add some HTML content inside the ancor node $template->insertHtml('link', '<i class="fa fa-times"></i> Close'); // Add a real URL to the ancor $template->setAttr('link', 'href', 'http://www.example.com/'); ...
CHOICE
choice
属性允许移除 DOM 节点。如果存在该属性,则默认情况下将移除节点。您必须调用 setChoice()。以下是一个示例。
<div choice="showNode"><a href="#" var="link"></a></div>
因此,默认情况下,此节点将从此 DOM 树中移除。要使其可见,请简单地使用
<?php // Load a new template from a file. (The file must be XHTML valid or errors will be produced) $template = new \Dom\Template::loadFile('index.html'); // Add some text content inside the anchor node $template->setVisible('showNode'); ...
REPEAT
repeat
属性用于重复数据,如列表或表格。重复块可以包含嵌套的 var
、choice
、repeat
节点。当从模板检索 repeat
对象时,请注意,重复对象是模板对象的子类,因此具有相同的功能,并增加了附加的 appendRepeat() 调用,该调用在完成 repeat
渲染并希望将其附加到父模板节点时调用。以下是一个示例。
<ul> <li repeat="item" var="item"><a href="#" var="url">Link</a></li> </ul>
设置了重复标记后,您可以继续填充您的列表或表格。
<?php // Load a new template from a file. (The file must be XHTML valid or errors will be produced) $template = new \Dom\Template::loadFile('index.html'); $list = array( 'Link 1' => 'http://www.example.com/link1.html', 'Link 2' => 'http://www.example.com/link2.html', 'Link 3' => 'http://www.example.com/link3.html', 'Link 4' => 'http://www.example.com/link4.html' ); // Loop through the data and render each item foreach($list as $text => $url) { $repeat = $template->getRepeat('item'); $repeat->insertText('url', $text); $repeat // Finish the repeat item and append it to its parent. $repeat->appendRepeat(); } ...
表单
使用DOMTemplate对象处理表单时略有不同。您无需任何变量或选择即可访问表单元素节点,但如果需要,也可以这样做。
如果我们有以下基本表单
<form id="contactForm" method="post"> <table> <tr> <td class="label">Name:</td> <td class="input"><input type="text" name="name" /></td> </tr> <tr> <td class="label">Email:</td> <td class="input"> <p class="formError" choice="email-error" var="email-error" /> <input type="text" name="email" /> </td> </tr> <tr> <td class="label">Country</td> <td class="input"> <select name="country"></select> </td> </tr> <tr> <td class="label">Comments:</td> <td class="input"><textarea name="comments" rows="5" cols="40"></textarea></td> </tr> <tr> <td class="label"> </td> <td class="input"><input type="submit" name="process" value="Submit"/></td> </tr> </table> </form>
那么我们可以通过以下代码访问表单
<?php $template = \Dom\Template::load($buff); // Set the pageTitle tag --> <h1 var="pageTitle">Default Text</h1> $template->insertText('pageTitle', 'Dynamic Form Example'); $domForm = $template->getForm('contactForm'); // Init any form elements to a default status $select = $domForm->getFormElement('country'); /* @var $select \Dom\Form\Select */ $select->appendOption('-- Select --', ''); $select->appendOption('New Zealand', 'NZ'); $select->appendOption('England', 'UK'); $select->appendOption('Australia', 'AU'); $select->appendOption('America', 'US'); $select->setValue('AU'); ... // Then you can set the value from the request if you like.... $domForm->getFormElement('name')->setValue($_REQUEST['name']); $domForm->getFormElement('email')->setValue($_REQUEST['email']); $domForm->getFormElement('country')->setValue($_REQUEST['country']); $domForm->getFormElement('comments')->setValue($_REQUEST['comments']);
其他方法
对于CSS和JavaScript,我们添加了一些独特的方法,这些方法允许您调用insertTemplate()、
appendTemplate()、insertDoc()、appendDoc()等方法,并将JavaScript和CSS插入到父标签中。这允许您在渲染过程中将脚本或URL插入到任何位置,只要最终的父模板有一个Head标签。
- appendCss()
$template->appendCss('body > p {background: #00FF00; }');
- appendCssUrl()
$template->appendCssUrl('http://example.com/css/style.css');
- appendJs()
$js = <<<JS
jQuery(function ($) {
$('.act').click(function (e) {
return confirm('Are you sure you want to install this plugin?');
});
});
JS;
$template->appendJs($js);
- appendJsUrl()
$template->appendJsUrl('http://example.com/js/sctipt.css');
如果您想在显示文档之前遍历DOMTemplate并操作所有CSS或JavaScript节点,这个功能非常棒。
DomTemplate的其他功能包括
- getElementById():通过ID属性获取节点。
- setTitleText():如果存在
标签,则设置其文本。 - appendMetaTag():如果存在标签,则将在父模板中添加一个meta标签。
加载器
Loader对象让开发者能够在加载提供的模板之前搜索其他模板。这在您想要用户能够覆盖现有默认模板时非常有用。可以根据您自己的框架需求添加/创建适配器,以搜索其他模板。
首先,您需要设置Loader并添加任何将查找现有模板的适配器。这使用一个后进先出队列。因此,最后添加的适配器是第一个执行的。
<?php // * Setup the Template loader, create adapters to look for templates as needed /* @var \Dom\Loader $dl */ $dl = \Dom\Loader::getInstance(); $dl->addAdapter(new \Dom\Loader\Adapter\DefaultLoader()); $dl->addAdapter(new \Dom\Loader\Adapter\ClassPath($config->getAppPath().'/html/xml'));
然后您可以在以后检索它来加载所有应用程序的模板
<?php $tplFile = \Tk\Config::getInstance()->getTemplatePath().'/index.html'; $template = \Dom\Loader::loadFile($tplFile);
AutoRenderer(已弃用)
为什么? 由于我不是内部脚本逻辑的粉丝,这将为设计师增加一个新的复杂性层次,我已经终止了作为DOMTemplate库支持的这一部分。
它仅作为参考保留在此,如果您想要基于它构建自己的需求,请将其作为起点。
自动渲染器是为了方便自动渲染数据而构建的,类似于其他模板语言的数据。
数据传递给自动渲染器,并使用模板属性来显示在AutoRenderer中存储的选定数据。