Download the PHP package livy/cyrus without Composer
On this page you can find all versions of the php package livy/cyrus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cyrus
Cyrus
a simple object-based HTML generator
Usage
Cyrus uses objects and method chaining to construct semantic HTML elements and then output them for you.
The basic process is as follows:
Note: You can also instatiate Cyrus with its internal factory:
The above code will print out the following:
It supports any tag type, even ones you made up:
In general the order you chain methods in doesn't matter: $element->setClass('a-class')->setEl('p')
is function equivalent to $element->setEl('p')->setClass('a-class')
. There are, however, a few exceptions:
- Nesting (see next section) requires
openChild
at the beginning fo a child element andcloseChild
at the end: Any other order will cause Cyrus to fail. - Methods that overwrite content (i.e.
setEl
) will overwrite the actions of previous calls in the chain (unless separated by child barriers). - Calls to
construct
ordisplay
should always come last. Since they don't return the current object, they'll break the chain, and attempting to chain other things after them will cause some errors.
Initial Class
When instantiating Cyrus, you can specify a class for the primary element, by doing the following:
Nesting
You can nest elements inside of one another using the openChild
and closeChild
methods:
Object Nesting
If you pass a Cyrus object to addContent
, that object will be inserted as content and automatically expanded.
Advanced Nesting
You can also nest items after a chain has been terminated by using the nest
method an assigning an ID when calling openChild
. This is especially useful if, say, you want to insert (or not) content based on a conditional without resorting to creating an entirely separate Cyrus instatiation:
You must point to nested elements directly, and define the entire path if they are nested more than one level down. You can do this by delimiting the ids with /
, like so:
It's important to note that when opening up nesting contexts like this, all children must be closed. There are a two convenience methods that can help you with this, closeChildren
and closeAll
. closeChildren
takes an integer as an argument, and will close a number children equal to that integer. closeAll
takes no arguments, and will close all chilren that are open in the current context.
Methods
To learn how methods operate, have a look at the source files (./src
). Each method is well documented.
The follow will cover some special functionality and edge cases.
Short forms
Any method that begins with "set" can be called in a shortened form, i.e. you can call setClass
as just class
.
Most nesting functions have short forms as well:
Advanced Attribute Manipulation
Unset Attribute
If you find you want to unset an attribute, call setAttr
on it with the false
argument:
Valueless Attributes
If you want to set an attribute that doesn't have a value--i.e. checked
--you can do so by calling setAttr
with the true
argument:
setAttr, etc
setAttr
and all of its aliased methods (i.e. setClass
, setURL
, etc) stack up whatever is passed to the same attribute--they don't overwrite anything. The only exception to this is if you pass false
as an argument to setAttr
, as this will completely remove that attribute from the element.
This value stacking means that the following statements are equivalent:
Negate Element with False Content
Using setContent
to set the only content of an element explictly to bool false
will cause that element to not be generated. This can be useful if you want elements to only appear if they have content. In order for the element to be negated, the following must be true:
- The element has only one item of content (i.e. count($this->content) === 1)
- That content item is exactly equal to bool
false
(not a falsey value, but the literal booleanfalse
) - The element in question is not a self-closing element
Examples: