Skip to content

Commit

Permalink
Merge pull request #34 from WebFiori/dev
Browse files Browse the repository at this point in the history
Added Support for Loading Template From Current Directory
  • Loading branch information
usernane authored Apr 2, 2023
2 parents 9b081eb + 3ca6100 commit 3622328
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 70 deletions.
27 changes: 25 additions & 2 deletions tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class LoadTemplateTest extends TestCase {
* @test
*/
public function test00() {
$this->expectException('Exception');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Empty template path');
$compiler = new TemplateCompiler('');
}
/**
Expand Down Expand Up @@ -112,6 +113,28 @@ public function test07() {
$node = $compiler->getCompiled();
$this->assertEquals("<div>\n No posts.\n</div>", $node->toHTML());
}
/**
* @test
*/
public function test08() {
$compiler = new TemplateCompiler('template.php', [
'message' => 'Good Job!',
'posts' => [
'One',
'Two',
'Three'
]]);
$this->assertEquals("<div>"
. "<ul>"
. "<li>One</li>"
. "<li>Two</li>"
. "<li>Three</li>"
. "</ul>"
. "<div>\n"
. " Good Job!"
. "</div>"
. "</div>", $compiler->getCompiled()->toHTML());
}
/**
* @test
*/
Expand Down Expand Up @@ -179,7 +202,7 @@ public function testHeadTemplate04() {
*/
public function testAddChildFromTemplate00() {
$node = new HTMLNode();
$node->component(self::TEST_TEMPLATES_PATH.'component-00.html', [
$node->include(self::TEST_TEMPLATES_PATH.'component-00.html', [
'base' => 'https://example.com',
'home-label' => 'Home Page',
'about-label' => 'About Us',
Expand Down
3 changes: 3 additions & 0 deletions tests/webfiori/test/ui/sub-component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<?= $message?>
</div>
18 changes: 18 additions & 0 deletions tests/webfiori/test/ui/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div>
<?php
if (count($posts) != 0) {?>
<ul>
<?php
foreach ($posts as $postTitle) {?>
<li><?= $postTitle;?></li>
<?php
}
?>
</ul>
<?php
} else {
echo "No posts.\n";
}
?>
<?php include 'sub-component.php'; ?>
</div>
13 changes: 6 additions & 7 deletions webfiori/ui/CodeSnippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* @version 1.0.3
*/
class CodeSnippet extends HTMLNode {

private $code;

private $codeDisplay;
Expand Down Expand Up @@ -250,12 +249,12 @@ private function addLineHelper() {
$span = new HTMLNode('span');
$span->setClassName('line-number');
$span->setAttribute('style',
'font-weight: bold;'
.'display: block;'
.'font-family: monospace;'
.'border-right: 1px dotted white;'
.'padding-right: 4px;'
.'color: #378e80;');
'font-weight: bold;'
.'display: block;'
.'font-family: monospace;'
.'border-right: 1px dotted white;'
.'padding-right: 4px;'
.'color: #378e80;');
$span->addTextNode($this->currentLineNum);
$this->currentLineNum++;
$this->lineNumsNode->addChild($span);
Expand Down
39 changes: 32 additions & 7 deletions webfiori/ui/HTMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,9 @@ public function comment(string $txt) {
* @throws TemplateNotFoundException If the file that the component is
* loaded from does not exist.
*
*
* @deprecated Use HTMLNode::include()
*/
public function component(string $path, array $slotsValues) {
public function component(string $path, array $slotsValues = []) {
$loaded = self::fromFile($path, $slotsValues);

if (gettype($loaded) == 'array') {
Expand All @@ -719,7 +719,6 @@ public function component(string $path, array $slotsValues) {
*
* @return int The number of child nodes attached to the node.
*
*
*/
public function count() : int {
return $this->childrenCount();
Expand Down Expand Up @@ -825,8 +824,9 @@ public function form(array $attributes = []) : HTMLNode {
*
* @throws TemplateNotFoundException
*/
public static function fromFile(string $absPath, array $slotsOrVars) {
public static function fromFile(string $absPath, array $slotsOrVars = []) {
$compiler = new TemplateCompiler($absPath, $slotsOrVars);

return $compiler->getCompiled();
}

Expand Down Expand Up @@ -1219,6 +1219,33 @@ public function img(array $attributes = []) : HTMLNode {

return $this->addChild($img);
}
/**
* Loads HTML-like or PHP component and make it a child of current node.
*
* This method can be used to load any component that uses HTML syntax
* into an object and make it a child of the instance at which the method is
* called in. If the component file contains more than one node as a root note,
* all nodes will be added as children.
*
* @param string $path The location of the file that
* will have the HTML component.
*
* @param array $values An array that contains slots values or variables
* to be passed to PHP template. A slot in
* the component is a string which is enclosed between two curly braces (such as {{name}}).
* This array must be associative. The indices of the array are slots names
* and values of the indices are slots values. The values of the slots can be
* also sub-array that contains more values. For example, if we
* have a slot with the name {{ user-name }}, then the array can have the
* index 'user-name' with the value of the slot.
*
* @throws TemplateNotFoundException If the file that the component is
* loaded from does not exist.
*
*/
public function include(string $path, array $values = []) {
$this->component($path, $values);
}
/**
* Adds new input (&lt;input&gt;, &lt;select&gt; or &lt;textarea&gt;)
* element as a child to the body of the node.
Expand Down Expand Up @@ -1950,7 +1977,6 @@ public function setNodeName(string $name) : bool {
*
*/
public function setStyle(array $cssStyles, bool $override = false) : HTMLNode {

if (!$override) {
$styleArr = $this->getStyle();
} else {
Expand Down Expand Up @@ -2664,8 +2690,7 @@ private function validateAttrNameHelper(string $name) : bool {
*
*
*/
private function validateFormattingOptions(array $FO): array
{
private function validateFormattingOptions(array $FO): array {
$defaultFormat = self::DEFAULT_CODE_FORMAT;

foreach ($defaultFormat as $key => $value) {
Expand Down
78 changes: 39 additions & 39 deletions webfiori/ui/HeadNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,6 @@ public function addChild($node, $attrsOrChain = [], bool $chainOnParent = true)
}


return $this;
}
/**
* Add multiple CSS resources files.
*
* @param array $files An array that holds paths to CSS files. This also
* can be an associative array. In this case, the indices are paths to files
* and the value of each index is a sub associative array of attributes.
*
* @return HeadNode The method will return the instance at which the method
* is called on.
*/
public function addCSSFiles(array $files) : HeadNode {
foreach ($files as $index => $options) {
if (gettype($index) == 'integer') {
$this->addCSS($options);
} else {
$this->addCSS($index, $options);
}
}

return $this;
}
/**
Expand Down Expand Up @@ -306,24 +285,24 @@ public function addCSS(string $href, array $otherAttrs = []) : HeadNode {
return $this;
}
/**
* Add multiple JS resources files.
* Add multiple CSS resources files.
*
* @param array $files An array that holds paths to JS files. This also
* @param array $files An array that holds paths to CSS files. This also
* can be an associative array. In this case, the indices are paths to files
* and the value of each index is a sub associative array of attributes.
*
* @return HeadNode The method will return the instance at which the method
* is called on.
*/
public function addJSFiles(array $files) : HeadNode {
public function addCSSFiles(array $files) : HeadNode {
foreach ($files as $index => $options) {
if (gettype($index) == 'integer') {
$this->addJs($options);
$this->addCSS($options);
} else {
$this->addJs($index, $options);
$this->addCSS($index, $options);
}
}

return $this;
}
/**
Expand Down Expand Up @@ -396,6 +375,27 @@ public function addJs(string $loc, array $otherAttrs = []) : HeadNode {

return $this;
}
/**
* Add multiple JS resources files.
*
* @param array $files An array that holds paths to JS files. This also
* can be an associative array. In this case, the indices are paths to files
* and the value of each index is a sub associative array of attributes.
*
* @return HeadNode The method will return the instance at which the method
* is called on.
*/
public function addJSFiles(array $files) : HeadNode {
foreach ($files as $index => $options) {
if (gettype($index) == 'integer') {
$this->addJs($options);
} else {
$this->addJs($index, $options);
}
}

return $this;
}
/**
* Adds new 'link' node.
* Note that if the 'rel' attribute value is 'canonical' or 'alternate', no node will be
Expand Down Expand Up @@ -451,18 +451,6 @@ public function addLink(string $rel, string $href, array $otherAttrs = []) : Hea

return $this;
}
/**
* Adds a set of meta tags.
*
* @param array $tags An associative array. The indices of the array
* are the values of the attribute 'name' and the value of the index is
* the value of the attribute 'content'.
*/
public function addMetaTags(array $tags) {
foreach ($tags as $name => $content) {
$this->addMeta($name, $content);
}
}
/**
* Adds new meta tag.
*
Expand Down Expand Up @@ -514,6 +502,18 @@ public function addMeta(string $name, string $content, bool $override = false) :

return $this;
}
/**
* Adds a set of meta tags.
*
* @param array $tags An associative array. The indices of the array
* are the values of the attribute 'name' and the value of the index is
* the value of the attribute 'content'.
*/
public function addMetaTags(array $tags) {
foreach ($tags as $name => $content) {
$this->addMeta($name, $content);
}
}
/**
* Returns a linked list of all alternate nodes that was added to the header.
*
Expand Down
16 changes: 6 additions & 10 deletions webfiori/ui/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ public function addChild($node, $attrsOrChain = [], bool $chainOnParent = true)
*
* @since 1.0.1
*/
public function addOption(array $options = []): Input
{
public function addOption(array $options = []): Input {
if ($this->getNodeName() == 'select' && gettype($options) == 'array' && isset($options['value']) && isset($options['label'])) {
$option = new HTMLNode('option');
$option->setAttribute('value', $options['value']);
Expand Down Expand Up @@ -202,8 +201,7 @@ public function addOption(array $options = []): Input
*
* @since 1.0.1
*/
public function addOptions(array $arrayOfOpt): Input
{
public function addOptions(array $arrayOfOpt): Input {
if (gettype($arrayOfOpt) == 'array') {
foreach ($arrayOfOpt as $value => $lblOrOptions) {
if (gettype($lblOrOptions) == 'array') {
Expand Down Expand Up @@ -249,8 +247,7 @@ public function addOptions(array $arrayOfOpt): Input
* is called on.
* @since 1.0.1
*/
public function addOptionsGroup(array $optionsGroupArr): Input
{
public function addOptionsGroup(array $optionsGroupArr): Input {
if ($this->getNodeName() == 'select' && gettype($optionsGroupArr) == 'array' && isset($optionsGroupArr['label']) && isset($optionsGroupArr['options'])) {
$optGroup = new HTMLNode('optgroup');
$optGroup->setAttribute('label', $optionsGroupArr['label']);
Expand Down Expand Up @@ -382,8 +379,7 @@ public function setMin(int $min) : Input {
*
* @since 1.0
*/
public function setMinLength(int $length): Input
{
public function setMinLength(int $length): Input {
if ($length >= 0) {
$iType = $this->getType();

Expand Down Expand Up @@ -426,8 +422,7 @@ public function setNodeName(string $name) : bool {
* @return Input The method will return the instance at which the method
* is called on.
*/
public function setPlaceholder(string $text = null): Input
{
public function setPlaceholder(string $text = null): Input {
if ($text !== null) {
$iType = $this->getType();

Expand Down Expand Up @@ -491,6 +486,7 @@ private function addOptionsToGroupHelper($optionsGroupArr, $optGroup) {
foreach ($optionsGroupArr['options'] as $value => $labelOrOptions) {
$o = new HTMLNode('option');
$o->setAttribute('value', $value);

if (gettype($labelOrOptions) == 'array' && isset($labelOrOptions['label'])) {
$o->addTextNode($labelOrOptions['label'],false);

Expand Down
Loading

0 comments on commit 3622328

Please sign in to comment.