Camen Design Forum

[DOMTemplate->repeat()] SetValue for repeated element

append delete xaktly

Hi,

Just recently I've found your project and it works beautifully. Thank you for that!

The only problem I have is when I'm using repeat() function. Consider this code:

%HTML

<div id="tabs">
    <ul>
        <li><a href="" class="tab-clickable"></a></li>
    </ul>
    <div id="tabs-container">
        <div class="tab clear-before"><div></div></div>
    </div>
</div>

%

Now, adding list elements is easy, I can do that like that:

%

$menu_item = $template->repeat('//ul/li');

for($i = 0; $i < count($tabs); $i++) {
        $tab = $tabs[$i];
        
        if($i === 0) $menu_item->addClass("a", $active_class);
        $menu_item->setValue('a', $tab['title']);
        $menu_item->setValue('a@href', "javascript:$js_func_name($i)");
        $menu_item->next();
}

%

But how do I add content to the divs I'm repeating, when there are is no nested element there? (I'm adding all the <p>'s and other tags in another place.)

It doesn't work when I do it this way:

%

$tab_item = $template->repeat('.tab');
 for($i = 0; $i < count($tabs); $i++) {
        $tab = $tabs[$i];
        
        $tab_item->setValue(".tab", $tab['content'], true);
        $tab_item->next();
}

%

Neither does changing ".tab" to "div". I gather, your code selects an element and then duplicates it, so XPath can only select its children. But how to SetValue for the selected element?

Reply RSS

Replies

append delete #1. Kroc

Use "." to reference the element being repeated!

Consider this code from "lib/functions.php" which builds the language list:

% PHP
$item = $template->repeat ('.nnf_lang');
//build the list for each additional language
foreach ($LANG as $code => $lang) $item->set (array (
	'./@value'	=> $code,
	'.'		=> $lang['name']
))->remove (array (
	'./@selected'	=> !($code == LANG)
))->next ();
%

Here you can see the `.` is used to refer to the repeat of ".nnf_lang", and `./@value` to refer to an attribute of the repeating element.

In your example, this should work:

% PHP
$tab_item = $template->repeat('.tab');
 for($i = 0; $i < count($tabs); $i++) {
        $tab = $tabs[$i];
        
        $tab_item->setValue(".", $tab['content'], true);
        $tab_item->next();
}
%

Hope this helps :)

append delete #2. xaktly

Thank you for your fast answer. That's exactly what I've been looking for! :)

And thank you for this great library. I'm gonna do some coding now. :)

append delete #3. xaktly

BTW, NNF is probably the best forum script I've ever seen. :)

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn