Skip to content

Commit

Permalink
Added the ability to specify pass through attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
tthomas48 committed Jan 2, 2010
1 parent 08c595c commit 113286c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 6 deletions.
1 change: 1 addition & 0 deletions .buildpath
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path="test"/>
<buildpathentry kind="src" path="HTML"/>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
</buildpath>
2 changes: 1 addition & 1 deletion .project
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>pjp</name>
<name>nest</name>
<comment></comment>
<projects>
</projects>
Expand Down
3 changes: 2 additions & 1 deletion HTML/Template/Nest/Compiler.php
Expand Up @@ -124,9 +124,9 @@ protected function processChildren($node)
$tag = null;
if (strpos($taglib, "urn:nsttl:") !== false) {
$tag = $this->_loadTag($node);
$output .= $tag->getAttributeDeclarations();
}


if ($tag != null) {
$output .= $tag->start();
} elseif (strlen($node->localName) && !$node->hasChildNodes()) {
Expand Down Expand Up @@ -163,6 +163,7 @@ protected function processChildren($node)

if ($tag != null) {
$output .= $tag->end();
$output .= $tag->getAttributeUnsets();
} elseif (strlen($node->localName)) {
$output .= "</" . $node->nodeName . ">";
}
Expand Down
32 changes: 32 additions & 0 deletions HTML/Template/Nest/Tag.php
Expand Up @@ -52,6 +52,7 @@ class HTML_Template_Nest_Tag
protected $node;
protected $compiler;
protected $attributes;
protected $declaredAttributes = array();

/**
* Constructor
Expand All @@ -68,6 +69,37 @@ public function __construct($compiler, $node, $attributes)
$this->node = $node;
$this->attributes = $attributes;
}

public function getAttributeDeclarations() {
if(count($this->declaredAttributes) == 0) {
return "";
}
$output = "<?php ";
foreach($this->declaredAttributes as $key) {
$value = "";
if(array_key_exists($key, $this->attributes)) {
$value = $this->attributes[$key];
}
$this->registerVariable($key);
$output .= "\$$key = \"" . addslashes($value) . "\";\n";
}
$output .= "?>";
return $output;
}

public function getAttributeUnsets() {
if(count($this->declaredAttributes) == 0) {
return "";
}

$output = "<?php ";
foreach($this->declaredAttributes as $key) {
$this->unregisterVariable($key);
$output .= "unset(\$$key);\n";
}
$output .= "?>";
return $output;
}

/**
* Registers a local variable. This variable will be used literally
Expand Down
15 changes: 15 additions & 0 deletions doc/pages/createtagfile.html
Expand Up @@ -106,6 +106,21 @@ <h1>Creating a tag file</h1>
</pre>
</fieldset>

<h2>Exposing Attributes</h2>
Attributes can be exposed with the $declaredAttributes array. Any keys
in that array will be declared as local variables for the current tag and
can be accessed like any other variable.

<pre>
class SimpleTaglib_SimpleTagFile extends HTML_Template_Nest_TagFile
{
protected $declaredAttributes = array("param1", "param2");
...
}
</pre>

You can then access ${param1} and ${param2} inside the tag.

<p>That's it for tag files. They'll work in pretty much every simple situation.</p>
</body>
</html>
5 changes: 2 additions & 3 deletions package.xml
Expand Up @@ -27,7 +27,7 @@ Nest is compatible with PHP 5.
</lead>
<date>2010-01-01</date>
<version>
<release>0.5.7</release>
<release>0.5.8</release>
<api>0.5.1</api>
</version>
<stability>
Expand All @@ -36,8 +36,7 @@ Nest is compatible with PHP 5.
</stability>
<license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
<notes>
XML namespaces that are not used as tag libraries are now passed through
into the document.
Added the ability to specify pass through attributes.
</notes>
<contents>
<dir name="/">
Expand Down
19 changes: 19 additions & 0 deletions test/TestTaglib.php
Expand Up @@ -45,6 +45,7 @@ class TestTaglib extends HTML_Template_Nest_Taglib
protected $tags = array(
"testTagFile" => "TestTaglib_TestTagFile",
"wrappableTagFile" => "TestTaglib_WrappableTagFile",
"wrappableWithAttributes" => "TestTaglib_WrappableWithAttributesTagFile",
"badXml" => "TestTaglib_BadXmlTagFile",
"nested" => "TestTaglib_NestedTagFile",
"roundedContainer" => "TestTaglib_RoundedContainer",
Expand Down Expand Up @@ -185,3 +186,21 @@ public function getTagFilename() {
}

}

class TestTaglib_WrappableWithAttributesTagFile
extends HTML_Template_Nest_TagFile
{
protected $declaredAttributes = array(
"attribute1", "attribute2", "attribute3"
);
/**
* gets the tag's filename
*
* @see HTML_Template_Nest_Tagfile::getTagFilename()
* @return string filename
*/
public function getTagFilename()
{
return dirname(__FILE__) . "/templates/wrappable_tagfile.tmpl";
}
}
4 changes: 4 additions & 0 deletions test/viewoutput/testtaglib.html
Expand Up @@ -66,5 +66,9 @@
</div>
</body>

<div id="wrapper">
Show me.
Show 99 </div>

</h:body>
</html>
15 changes: 14 additions & 1 deletion test/views/testtaglib.nst
@@ -1,4 +1,5 @@
<html xmlns:test="urn:nsttl:TestTaglib" xmlns:h="http://www.w3.org/TR/html4/">
<html xmlns:test="urn:nsttl:TestTaglib" xmlns:h="http://www.w3.org/TR/html4/"
xmlns:c="urn:nsttl:HTML_Template_Nest_Taglib_Standard">
<h:head>
</h:head>
<h:body>
Expand Down Expand Up @@ -26,5 +27,17 @@
This is the body text.
</test:body>

<test:wrappableWithAttributes attribute1="true" attribute2="false" attribute3="99">
<c:if test="attribute1 == 'true'">
Show me.
</c:if>
<c:if test="attribute2 == 'true'">
Don't show me
</c:if>
<c:if test="attribute3 > 50">
Show ${attribute3}
</c:if>
</test:wrappableWithAttributes>

</h:body>
</html>

0 comments on commit 113286c

Please sign in to comment.