taeluf/phtml
Composer 安装命令:
composer require taeluf/phtml
包简介
README 文档
README
A DOMDocument extension that's easier to use, allows PHP code, and provides a few extra apis for working with the document.
Status: Stable, tests passing
I don't expect any existing functionality to break. I'm not aware of any major bugs, but if you find one, open an issue & I'll try to fix it. I have no idea what happens if you use it on XML.
Install
composer require taeluf/phtml 0.1.*OR{"require": {"taeluf/phtml": "0.1.*"}}- Dev version is
v0.1.x-dev.
- Dev version is
Document
- Construct with
$doc = new \Taeluf\PHTML($htmlAndPHPSource), instead of usingloadHtml() - Allows PHP anywhere in the document
- Use
$doc->output(false);to remove PHP code
- Use
echo $docwill show your document.$doc->output(true)is called internally (truerestores your PHP code from placeholders)
- Call
$doc->xpath('//tagname[@attributename="value"]', $refNode=null)to query with xpath- Returns an array of nodes, not a node list
$refNodemay be null to search from root of document or it may be a node within the document to search under
- It IS a
DOMDocumentso all the functions are available as documented on the php site. $doc->childNodes[0]is the root node that's added by pHtml.- The 1st node declared in your html is at
$doc->childNodes[0]->childNodes[0];
- The 1st node declared in your html is at
Node
- Call
$node->attributes()to get all HTML attributes in an array $node->docreturns the DOMDocument instance$node->innerHTML = "whatever"and$var = $node->innerHTMLboth work$inputNode->formreturns the parent form node, if it exists$node->attributereturns the named attribute$node->attribute = "funny"sets the named attribute to the valueunset($node->attribute)removes the attributeecho $nodeechos the outer html- This will not restore PHP, though. Placeholders will still be present
- call
$doc->fillWithPHP("".$node);to get the node WITH php
$formNode->addHiddenInput($name,$value)will... do that$node->boolAttribute('attributeName');- currently just calls$node->hasAttriute($attrName), but I had plans to do something else. Those plans were not standars-compliant, though.
Compiler
The compiler is NOT used by this library. I'm using it in another lib. I use this to modify PHP in an HTML document in a fine-tuned manner. I don't know if this will remain a part of PHTML or not. It uses the PHP parser, and provides some niceties around the PHP without using a DOMDocument
- Converts PHP + HTML code to HTML code with placeholders
- Output to a file
- Prepend code before or append code after a placeholder or before/after the full block of code
Extra Stuff
- Call
$html = $doc->phpPlaceholder('<?php echo "A block of PHP code>?>');- returns an alpha-numeric string if it's just PHP code, or modified HTML with the placeholder(s) present
- the placeholder is stored in
PHTMLDocso you can add your placeheld code to the document & it will be restored when you output the document. - Later call
$doc->fillWithPHP($html)to restore the PHP. - You can pass a larger block of HTML code as well, then use the
fillWithPHPto restore it to it's former glory. - This is used internally
Weird Stuff / Warnings
<!DOCTYPE>,<html>, and<head>tags are converted to<tlfphtml-doctype>,<tlfphtml-html>, and<tlfphtml-head>before the DOMDocument is created. They are converted back to the proper DOCTYPE!/html/head tag during the__toString()
xpath stuff
- Get all children of node:
$phtml->xpath('descendant::tagname[@attrName="attrval"]', $contextNode)<- thedescendant::part need stay the same. The rest of the query is up to you. - to
xpathfor doctype, html, or head, use//tlfphtml-doctype,//tlfphtml-html, and//tlfphtml-headrespectively.
TODO
- Install instructions
- Notes for contributors
- Documentation website & Better documentation
Implementation Details
- PHP is compiled out and replaced with alpha-numeric placeholders before being added to PHP's built-in DOMDocument. Then AFTER the document is output, the PHP placeholders are replaced with the original PHP code
- Wraps all code in
<root></root>, but does not output that. I think this was necessary for making the__toString()work correctly...
Bugs
- breaks if an open PHP tag (
<?php) is not later closed (?>)
taeluf/phtml 适用场景与选型建议
taeluf/phtml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 taeluf/phtml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 taeluf/phtml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 83
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 5
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-01