3

I want to create a layout (or override) that i can run a foreach loop on the div's of certain classes. Is there a method in joomla to help me? I have seen a little about php HTML DOM parser but I thought someone might know a simple way

I am thinking I have to parse the content into an array first then I can loop the layout.

2 Answers 2

2

I'm not sure if a client side solution would work for you, but this sounds like a job for jQuery:

http://api.jquery.com/jquery.each/

<script type="text/javascript">

$('.classname').each(index, function(){

    // preform task on each class instance

})
</script>
5
  • perhaps. I am actually trying to build a layout that makes it easy to put the content in bootstrap modals so that might be the way to go. I want to be able to allow the client to only insert this structure in the article. <div class="list1"> <dl></dl> </div> <div class="list2"> ... or ids Promise I will post my solution
    – poproar
    Jul 16, 2014 at 17:16
  • As for wrapping the content php side stutteringp0et shared this in the forum. link
    – poproar
    Jul 16, 2014 at 17:23
  • so far i am here $dom = new DOMDocument; $dom->loadHTML($this->item->introtext); $main = $dom->find('div[id=main]',0); ?> but my HTML is crapped..
    – poproar
    Jul 16, 2014 at 20:04
  • sorry I'm not familiar with parsing html using PHP. Maybe someone else could help there. Jul 16, 2014 at 20:13
  • I did a preg_match_all. Thanks for you attention though. I posted what I did
    – poproar
    Jul 17, 2014 at 14:16
1

This wraps a bunch of lists in an article for modals

// find what I am looking for
if (preg_match_all("'<h4>(.*?)</h4>\n?<dl id=\"(.*?)\">(.*?)</dl>' si", $this->item->introtext, $matches)) :

$lists = array();

// adjust array so I know what I am working with a better
for($i = 0; $i < count($matches[0]) ; $i++)
{
    // preg match all creates an array for each item wrapped in parens after full match
    $lists[$i] = new stdClass();
    $lists[$i]->name = $matches[1][$i];
    $lists[$i]->id = $matches[2][$i];
    $lists[$i]->data = $matches[3][$i];
} ?>

<div class="row-fluid">
<?php foreach($lists as $list) : ?>

    <a class="btn btn-large btn-block" href="#<?php echo $list->id; ?>" data-toggle="modal"><?php echo $list->name; ?></a>

<?php endforeach; ?>

<?php foreach($lists as $list) : ?>

    <div id="<?php echo $list->id; ?>" class="modal hide fade" tabindex="-1">

        <div class="modal-header"><button class="close" type="button" data-dismiss="modal">×</button>
            <h3><?php echo $this->item->category_title; ?> - <?php echo $list->name; ?></h3>
        </div>

        <div class="modal-body">
            <dl>
                <?php echo  $list->data; ?>
            </dl>
        </div>

        <div class="modal-footer"><button class="btn" data-dismiss="modal">Close</button></div>

    </div>

<?php   endforeach; ?>

<?php   endif; // end here ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.