A WebYep-Loop is used to create blocks of content which can be repeated, rearranged, hidden and deleted by the user.
Unfortunately the Loop doesn't have a global
attribute like for example the ShortText element has. A possible use case for a global loop would be a news ticker which is shown on several pages. When the user changes its contents (add, modify and/or delete entries) those changes shall reflect on every instance of this ticker.
In order to achieve this, we'll use a few tricks. WebYep finds its contents by looking for the document ID (a unique number which identifies a page). All we've got to do is make WebYep think the 'global loop' is located in another document. The following code will do this:
<?php
$oCurrentDocument = $goApp->oDocument;
$goApp->oDocument = new WYDocument(new WYURL(basename(__FILE__)));
$goApp->oDocument->setDocumentInstance(0);
foreach (WYLoopElement::aLoopIDs("globalLoop") as $webyep_oCurrentLoop->iLoopID) {
$webyep_oCurrentLoop->loopStart(true);
?>
<!-- Loop content goes here (replace this line with your content) -->
<?php
$webyep_oCurrentLoop->loopEnd();
}
$goApp->oDocument = $oCurrentDocument;
?>
Save this in a file called globalLoop.php
. Now we've got our container for the loop. You can use this as a template for your own experiments.
Note that the loop will take care of its controls in edit mode. Sometimes this is not desired (e.g. when you put a <table>
around the loop and repeat the <tr>
inside the loop). In this case you can start the loop like this (last line in the first block):
$webyep_oCurrentLoop->loopStart(false);
and place the controls manually with this piece of code:
<?php $webyep_oCurrentLoop->showEditButtons(); ?>
So far we've only got the container for the loop items so lets add something useful to display. To keep things simple, I'll just use a WebYep LongText element but you can use any valid HTML with any number of WebYep elements of course.
Add this instead of the HTML comment in globalLoop.php
.
Since we want to create a simple news ticker, a WebYep LongText element wrapped in a <div>
will be sufficient:
<div> <?php webyep_longText("Text", false, "", true); // WebYepV1 ?> </div>
Note that all WebYep elements you put inside the loop must not be global.
We can now add our global loop anywhere we want. Use the following piece of code in your regular web page to include it:
<?php include('globalLoop.php'); ?>
This assumes that globalLoop.php
is located in the same directory as the page including it. If this is not the case you must provide a path relative to the including page.
Technically our global loop is a separate document, not related to any other page. This means it has an own document ID, even if it is not a complete HTML document. The trick is to switch the current document while the loop is processed so we actually have 2 documents in 1.
We've put together a small example site with 2 different global loops, one using <div>
s and the other a <table>
. You can download it here.
Please note that it needs to be run on a web server with a working WebYep installation!