Download the PHP package finalclass/nbml without Composer
On this page you can find all versions of the php package finalclass/nbml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package nbml
NetBricks Markup Language [welcome]
Nbml is a language which serves to define views in object-oriented manner. With its help we can easily transform view in phtml format (HTML with insertions in PHP language), to PHP classes.
System requirements [system-requirements]
The nbml library requires PHP in version minimum 5.3
Instantiation [instantiation]
In order to start working with nbml files, one has to initialise ViewAutoLoader. This class registers php autoloader, so it can handle *.nbml file. It can be done using provided sandbox, or manually. It is also needed to initialise some autoloader to nbml library classes - because nbml uses standard method to load classes this action is left to the programmer. But at first lets' go to the easier variant - to start work using sandbox file [instantiation-sandbox].
Instantiation using sandbox [instantiation-sandbox]
It is the simplest, and at the same time the least flexible method to initialise nbml.
$viewAutoLoader = include 'library/Nbml/sandbox_runtime.php';
Creating an instance manually [instatiation-manual]
We can, of course, initialise the interpreter manually. Thanks to this we can at its fullest have benefit from the configuration options, which are provided by the interpreter. Below you can find an exemplary manual configuration:
file index.php
Example component
Let's put it in MyNamespace\Example folder and name it Example.nbml (the file would be in this location: /MyNamespace/Example/Example.nbml)
Now let's execute somewhere in *.php file following instruction:
Example component
Hello World!
It is all what needs to be encoded. Now what remains, is to run the script. Your eyes will be presented with this WWW website, with following source code:
Generated html
<!DOCTYPE>
<html>
<head>
<title></title>
<meta charset="utf-8"/>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Hello World!
</body>
</html>
Notice, that in folder HelloWorld has been created a new file: HelloWorld.php - it is compiled version of file HelloWorld.nbml. Here is its content:
Generated php
Hello World!
will create a class that inherits from \Nbml\Component
with private variable $message that has type string
.
This way we can define certain aspects of component that is being built.
Nbml also has a mechanism to define custom tags, which change properties of defined variable.
In standard library we have at our disposal following tags: [[Public]][metadata-tags-public],
[[State]][metadata-tags-state], [[OnState]][metadata-tags-on-state],
[[OnDemand]][metadata-tags-on-demand], [[Css]][metadata-tags-css] and [[Js]][metadata-tags-js].
Metadata tags attributes
Metadata tags can have certain attributes. If given Metatag has attributes, it can also have the default attribute defined. For instance attribute [[State]][metadata-tags-state] can
be parametrised by name
attribute, which is the default one. So this notation:
* [OnState(name='selected')]
is equal with this notation:
* [OnState('selected')]
In case when metadata tag accepts more attributes, their names should be separated with comma in such manner:
* [Tag(attr1='value1', attr2='value2', attr3='value3')]
The interpreter also allows to easily create custom Metadata tags. The process is described in this place [Custom Metadata Tags][custom-metadata-tags]
Metadata Tags - Public [metadata-tags-public]
Let's consider following example:
file HelloWorld.nbml
<div class="center bold">
<?=$message?>
</div>
And then its usage:
<a href="/example" label="Example" class="<?=$selectedState ? 'selected' : ''?>">
Example
</a>
and then its usage:
<a href="/example"
title="Example"
class="<?=$selectedClass?>">
Example
This button will possess class selected
only in case when the component Button will be in state selectedState.
This property can be easily connected with [[Public]][metadata-tags-public] to create customisable button in this manner:
<a href="<?=$href?>"
title="<?=$label?>"
class="<?=$selectedClass?>">
<?=$label?>
</a>
In this case we can define by our own, what class should be used for state selectedState
.
Metadata Tags - OnDemand [metadata-tags-on-demand]
This tag causes creating in lieu of given variable the object \Nbml\MetadataTag\OnDemandMetadataTag\OnDemandFactory
.
The foregoing class creates given variable in the very moment it's used
(in the moment methods __toString()
, __invoke()
,
__call()
, __set()
lub __get()
are called).
<div class="example">
<?=$subComponent?>
</div>
In preceding example, in place of $subComponent the OnDemandFactory class is created, and then (only if rand(0, 9) > 4), OnDemand Factory in a very moment of invoking __toString() creates an object of class \Nbml\Component, as it was set in phpDoc.
The above example's purpose is to present the idea of the [OnDemand] tag functionality, however in real applications the tag is useless in this case. [OnDemand] shows its best at "heavy" components, whose initialisation involves heavy system's load.
Metadata Tags - Css [metadata-tags-css]
Metadata tag [[Css]][metadata-tags-css] is used to load CSS files.
The nbml provides basic
version of the component, which actually is an HTML \Nbml\Component\Application
.
This version can be extended with your own needs. Take notice that nmbl does not allow to put css and js files manually (it can be done, but is not recommended). Instead, it is recommended to create main component such way, that it can inherit from \Nbml\Component\Application
. The Application component has variable styleSheets which is of type \Nbml\Collection
. The style files can be added to it for example in this manner:
\Nbml\Component\Application::getInstance()->styleSheets()->add('/css/styles.css')
Exactly this operation is done by tag [[Css]][metadata-tags-css]. Inasmuch Application::getInstance()
returns always the last created instance (with new
), in the moment of creating we have access exactly to it. In the system there shouldn't be created another instance of Application. We send to client just one HTML site!
Sure, there can be exceptions (but it's hard to me to figure out any).
In such case it should be kept in mind, that Application::getInstance()
always returns the last created instance.
Attributes
Tag [[Css]][metadata-tags-css] has one attribute: file.
It is default attribute, so construction [Css(file="/css/styles.css")]
is equal with [Css("/css/styles.css")]
Relative paths
Tag [[Css]][metadata-tags-css] allows inputting relative paths. When, for example, we create file providing relative path to CSS file:
public/Example/MyComponent/MyComponent.nbml
Component content...
the relative path shall be expanded this way:
<link href="/Example/MyComponent/myComponent.css" .../>
When we will define path as an absolute path: [Css('/myComponent.css')]
it remains unchanged: <link href="/myComponent.css" .../>
.
In case of working with remote files, keep in mind that HTTP or HTTPS must be inserted when defining a location of CSS file - the system has to know, that it's dealing with remote file.
For instance a path: [Css('example.com/file.css')]
used in aforementioned component will be extracted to: /Example/MyComponent/example.com/file.css.
The proper way to do this is to put http on the beginning, in such manner:
[Css('http://example.com/file.css')]
The same rule applies to tag [[Js]][metadata-tags-js]
Assigning tag to variable
The tag is not binded with the variable it is assigned to, however it should be assigned to some particular variable.
Below is wrong construcion:
Wrong file!!!
Component content...
Properly this component should be built like this:
Component content...
It means that tag [[Css]][metadata-tags-css] should be assigned to some particular variable. The best way is to assign such metadata tags to variable $this. It is the recommended method. Similarly works tag [[Js]][metadata-tags-js].
Metadata Tags - Js [metadata-tags-js]
Functionality of [[Js]][metadata-tags-js] tag is identical with functionality of [[Css]][metadata-tags-css] tag with the exception, that it is responsible for adding JavaScript files. It causes adding the following line in component's constructor:
\Nbml\Component\Application::getInstance()->scripts()->add('file.js');
Custom Metadata tags [custom-metadata-tags]
The nbml engine makes possible to define which metadata tags should be concerned when interpreting components. You make this choice when initialising compiler. Go to section [Instantation][instantiation] to learn more about this process.
Class \Nbml\Compiler
has a method ->addTagProcessor($className)
which allows to add custom tags. For instance initialisation of
metadata tags provided with standard library looks as follows:
$viewCompiler = new Compiler();
$viewCompiler
->addTagProcessor('\Nbml\MetadataTag\PublicMetadataTag')
->addTagProcessor('\Nbml\MetadataTag\StateMetadataTag')
->addTagProcessor('\Nbml\MetadataTag\OnDemandMetadataTag')
->addTagProcessor('\Nbml\MetadataTag\OnStateMetadataTag')
->addTagProcessor('\Nbml\MetadataTag\CssMetadataTag')
->addTagProcessor('\Nbml\MetadataTag\JsMetadataTag');
In order to create custom tag, you have to create a class that implements interface
\Nbml\MetadataTag
:
namespace Nbml;
use \Nbml\Reflector\Variable;
use \Nbml\Reflector\MetadataTagDefinition;
use \Nbml\Reflector;
interface MetadataTag
{
static function getMetadataTagName();
function __construct(Variable $variable, MetadataTagDefinition $definition, Reflector $classReflection);
function hasGetter();
function getGetterMethodDefinition();
function hasSetter();
function getSetterMethodDefinition();
function hasInitializationCode();
function getInitializationCode();
function hasBeforeRenderRetrievalCode();
function getBeforeRenderRetrieveCode();
function getRequiredProperties();
function hasDefaultProperty();
function getDefaultPropertyName();
}
Here comes to aid class \Nbml\MetadataTag\AbstractMetadataTag
which implements
most of the methods of \Nbml\MetadataTag
interface. It is recommended to extend this class
in purpose to create new tags' processors.
Exemplary metadata tag
Let's assume that we want to build our own metadata tag [Hello]
, which would add to variable which is assigned to
the string: "Hello ". Here is this tag's content:
Content of such metadata tags' processor shall be as follows:
<?=$miwa?>
Invoke of this component:
<?php
echo new HelloComponent(); //Hello Miwa
When writing components we have an access to variables $this->variable
and $this->definition
.
$this->variable
is of \Nbml\Reflector\Variable
type and has in itself full description
of variable, which given metadata tag is assigned to.
On the other hand, variable $this->definition
is a definiton of given metadata tag (in this case a definition of meta tag Hello).
It is of type: \Nbml\Reflector\MetadataTagDefinition. In this object we'll find
all data concerning attributes (and its values) of given metadata tag.
Sometimes when building certain metadata tag a necessity can arise to manipulate the entire
being-built component's class. Here comes to aid the property $this->classReflection
which is of type \Nbml\Reflector
.
Consider also this line in metadata tag [Hello]
that is being built:
return '$this->options[\'' . $nameUnderscored . '\'] = '
. '\'Hello \' . ' . $default . ';';
You may wonder where one should know from how to write such line? Well it depends
on you. It depends on you what your components would inherit from.
By default it is class \Nbml\Component
, its knowledge is crucial here.
Let's have a look at its content:
namespace Nbml;
class Component
{
protected $text = '';
protected $options = array();
public function __construct($options = array())
{
$this->options = array_merge($this->options, $options);
}
public function __toString()
{
try {
$this->__invoke();
} catch(\Exception $e) {
trigger_error((string)$e);
}
return $this->text;
}
public function __invoke()
{
return '';
}
}
As it may be seen it is a very short, light class. Ready to being expanded.
Writing the components, the interpreter overrides function __invoke()
.
The role of the function __invoke()
is to fill up variable $this->text
.
Whtat remains to me, is to left you with your imagination:)