DStyles object provides some methods to make scripting Your project as simply and easy as it is possible. Remember, that every method which goes after get( ... ) method can by linked to another using -> (pause and greater mark) sign.
get() method returns another element, named DStylesElement. It contains data associated with single template element. Every element (DStyles or DStylesElement) has own methods that work only with him.
This method allows You to change folder for Your styles:
<?php // Following lines... $obj = new DStyles; $obj->setHome('myStyles'); // ...are an equivalent to: $obj = new DStyles('myStyles'); ?>
This method allows You to change folder with Your style:
<?php // Following lines... $obj = new DStyles; $obj->setStyle('style1'); // ...are an equivalent to: $obj = new DStyles(DSTYLES_DEFAULT_DIR, 'style1'); ?>
This method allows You to change extension used by Your template files:
<?php // Following lines... $obj = new DStyles; $obj->setExt('.tpl'); // ...are an equivalent to: $obj = new DStyles(DSTYLES_DEFAULT_DIR, DSTYLES_DEFAULT_EXT, '.tpl'); ?>
This method allows You to open up template file. Remember, that original file will never by overridden by generated content. What is more, results are generated "at the end", that means only when You call create() or result() methods.
<?php // Creating DStyles object $obj = new DStyles; // And opening template file $obj->get('myFile'); // You can also save opened file to a variable and use it many times: $element = $obj->get('myFile'); for( ... ){ $element->...; } ?>
This methods are just shortened versions of:
<?php // Creating DStyles object $obj = new DStyles; // Following lines mean the same... $header = $obj->header($data); $footer = $obj->footer($data); // ...as these $header = $obj->get('header')->set($data); $footer = $obj->get('footer')->set($data); ?>
Triggers are something that brings to Your styles a bit of logic. They allow You to show or hide part of Your HTML code depending on particular condition. In DStyles, creating triggers is to simply add HTML comment. Then You have to register that comment, now called trigger. All registered triggers are set to false by default, which means they will not be shown.
You have no limit in using triggers, however remember, that open trigger has to be closed. You can also use negated triggers just adding ! (exclamation mark) before its name in template file. This allows You to reduce amount of triggers since You do not have to register theirs negations in PHP code.
Note, that only registred triggers are removed, no matter if they are set to true or false. Every unregistred triggers will be omitted.
Example mySite file
<p> Paragraph #1 </p> <div> <!-- JAVASCRIPT --> <!-- DIV1 --> <div style="color: green;" > Here is div shown only, when JavaScript is on (just fake example) </div> <!-- JAVASCRIPT --><!-- !JAVASCRIPT --> <!-- DIV2 --> <div style="color: maroon;" > Here is div shown only, when JavaScript is off (just fake example) </div> <!-- !JAVASCRIPT --> </div> <p> Paragraph #2 </p>
Example index.php file
<?php // Creating DStyles object $obj = new DStyles; // Get mySite file $mySite = $obj->get('mySite'); // Now we can register our JAVASCRIPT trigger $myStyle->registerTrigger('JAVASCRIPT'); // Now display result $mySite->result(); ?>
Example above will result in following content:
<p> Paragraph #1 </p> <div> <!-- DIV2 --> <div style="color: maroon;" > Here is div shown only, when JavaScript is off (just fake example) </div> </div> <p> Paragraph #2 </p>
This method is used to change trigger's state. First parameter can be both string, to set only one trigger at once, or array, to define multiple changes.
<?php // Creating DStyles object $obj = new DStyles; // Registering many triggers $el = $obj->get('myFile')->registerTrigger(array( 'TRIGGER_1', 'TRIGGER_2', 'TRIGGER_3', ... )); // Now controll Your triggers $el->trigger('TRIGGER_1', true); // This will affect to only one trigger $el->trigger(array( 'TRIGGER_2' => true, 'TRIGGER_3' => (2 > 3 || 1 == 2) )); // You can also set second parameter when first is an array. Then, all numerix-indexed elements will have state set by second parameter. // Both, TRIGGER_1 and TRIGGER_3, will be set to true due to second parameter $el->trigger(array( 0 => 'TRIGGER_1', // This has index = 0 'TRIGGER_2' => false, 'TRIGGER_3' // And this index = 1 ), true); ?>
This method is used to set content to special constants. Special constants in Your template file will be repleaced by specified value.
Example mySite file
<p>{CONTENT_1}</p> <p>{ANOTHER_CONTENT}</p> <input type="text" value="{MY_VALUE}" />
Example index.php file
<?php // Creating DStyles object $obj = new DStyles; // Get mySite file $mySite = $obj->get('mySite'); // Now You can set value to one special constant... $mySite->set('CONTENT_1', 'My content #1'); // Or to meny special constants $mySite->set(array( 'ANOTHER_CONTENT' => 'My another content goes here.', 'MY_VALUE' => 'Default value...' )); // Now display result $mySite->result(); ?>
Example above will result in following content:
<p>My content #1</p> <p>My another content goes here.</p> <input type="text" value="Default value..." />