Invenzzia »

Pages: [1]   Go Down
  Print  
Author Topic: OPTv2: Section and associative arrays  (Read 587 times)
0 Members and 2 Guests are viewing this topic.
hamczu
User

Offline Offline

Posts: 10


View Profile WWW
« on: September 09, 2010, 00:53:57 »

Let consider the following data structure
Code:
$array = array(
'first' => array('title' => 'whatver1'),
'second' => array('title' => 'whatver2'),
'third' => array('title' => 'whatver3'));
Now I want to iterate over it in opt:section but it does not seem to work.
Code:
<opt:section name="array">
{$array.title}
</opt:section>
I have notice: "Undefined offset: 0"

But when if we change it to non-associative array:

Code:
$array = array(
array('title' => 'whatver1'),
array('title' => 'whatver2'),
array('title' => 'whatver3'));
it works correctly. So my question is:
Is this behavior planned?
I use assoc-arrays very often and i want to iterate over it in section, not only in opt:foreach
Maybe I can modify Opt_Format_Array class to work in that way.
« Last Edit: September 09, 2010, 01:14:31 by hamczu » Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #1 on: September 09, 2010, 07:07:40 »

Yes, it is an intended behaviour.

Writing a new format for the associative arrays is possible. The whole issue lies in the fact that Opt_Format_Array internally uses for loop, and you need foreach. Simply copy the base class to another file, change the name and update it. The documentation explains the meaning of each hook, and you can take a look at Opt_Format_Objective implementation which uses foreach.
Logged

PozDrX, Zyx
---Invenzzia group---
eXtreme
Invenzzia
Administrator
User
*****
Offline Offline

Posts: 129

Jacek Jędrzejewski


View Profile WWW
« Reply #2 on: September 09, 2010, 11:02:24 »

Wouldn't ArrayIterator (or ArrayObject) and Objective dataformat do the job?
Logged

hamczu
User

Offline Offline

Posts: 10


View Profile WWW
« Reply #3 on: September 09, 2010, 14:31:58 »

Thank you for the very fast answers!
Indeed - during my investigation I realized that Format_Array uses for loop.

I have another question:
How can I register null-namespace ?
I mean, that I want to process e.g. all img tags which occur in my template.
Of course I can create new Instruction and put "opt:img" everywhere,
but i want to use "img" tag and process it.
It is possible?
That functionality will be great because designers may not learn
how to use new template language and special xml tags.
They put just "img" in the template code, and Instruction Processor,
checks the path of the file etc..

PS. I have forked OPT 2.0 branch to my Ozo Framework project and i have add some features i needed, e.g.:
- new instructions js, css, a, img
- few changes in attributes - dtd, for example
- sourceDir can be array of paths where templates are located (searched one after other)

I can't use OPT 2.1 branch because of PHP 5.3...
I'd be very glad to use 5.3 in my whole framework,
but unfortunately many of our clients have servers with only PHP 5.2.
Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #4 on: September 09, 2010, 17:43:51 »

Hmm... registering a null namespace would cause to put all the HTML tags to instruction processors, but I'm not perfectly sure about this. I recommend you to stay with explicit namespaces. However, any OPT instruction can scan the compiler tree to find those IMG tags and do something with them. For example, you can modify opt:root instruction and add there some code that would process IMG tags, too.

When it comes to generating URL-s, links to JS/CSS files, I prefer different ways to deal with them. I simply register a function called url() and let the programmer use it wherever he wants.

sourceDir issue -> do you cache the results somehow? Disk operations tend to be a bottleneck, and you should keep their amount as low as possible.

OPT 2.1 -> you can start a new branch of your project, and develop both of them simultaneously for a while. The clients with older PHP would get older version, the others - the newer. Anyway, I think that in case of OPT 2.1 it would be good to wait at least to the beta2 version, as there is still some work that needs to be finished.

Logged

PozDrX, Zyx
---Invenzzia group---
hamczu
User

Offline Offline

Posts: 10


View Profile WWW
« Reply #5 on: September 09, 2010, 19:45:42 »

Okay.. so how to make a instruction that process all "img" tags in my tree?
It could be seperate instructon or in opt:root if there is no other solution

caching - in this moment no, in the future - yes, like the results of php class autoloader
Logged
Zyx
Your programmer
Administrator
User
*****
Offline Offline

Posts: 291



View Profile WWW
« Reply #6 on: September 10, 2010, 08:07:54 »

$imgNodes $node->getElementsByTagName('img'true);

As a result, you get a list of all the img tags within $node and you can process them somehow. If you wish to find more tags, you can optimize the process by writing a custom tree traversal:

$queue = new SplQueue;
$queue->enqueue($node);
while(
$queue->count() > 0)
{
    
$current $queue->dequeue();

    
// do the recognition here.

    
if($current instanceof Opt_Xml_Element)
    {
        foreach(
$current as $subitem)
        {
            
$queue->enqueue($subitem);
        }
    }
}
Logged

PozDrX, Zyx
---Invenzzia group---
Pages: [1]   Go Up
  Print  
 
Jump to:  

Subject Started by Replies Views Last post
abercrombie outlet, seismic design and construction of hard galardef 0 11 Last post May 08, 2012, 03:33:21
by galardef
New website announcement Zyx 0 808 Last post January 24, 2011, 13:45:11
by Zyx
OPTv2: Main tpl parced but opt:include view not parce amichelin 5 1969 Last post May 03, 2009, 16:03:37
by eXtreme
OPTv2: Problem z zapytaniem w pętli.. GyniO 6 583 Last post March 15, 2010, 19:26:53
by GyniO
OPTv2: Load items to a seccion amichelin 2 683 Last post April 28, 2009, 19:31:51
by amichelin