• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
$replaceContent = new jquery('replace') and =& get_instance() confusion!!!

#1
[eluser]thinkigniter[/eluser]
I need some help with a helper I'm writing.

It's called taconite or taco for short.

It's main function is to create an xml file.

It allows you to do JQUERY commands from within PHP.

E.G.
Code:
$replaceContent = new taconite('replace');
$replaceContent->set('#my_div','This is my new content ');

or

Code:
$slideDown = new taconite('slideDown');
$slideDown->set('#my_div');

This will allow you to replace the content of your div tag with the id of my_div with the new content.
E.G.
Code:
<div id="my_div">Something to start with</div>

Now I won't go on about how this happens until the Docs are complete but I do have a question that I can't find an answer for.

It is…
In my wrapper class for the jquery function "replace" I need to write back to the "parent_class".
I do this with…
Code:
"$object =& get_instance(); $object->store['xmlString'] = 'This is my new content'; "

The Question:
Now does this mean that my class now contains a duplicate of the CI_BASE class in my class or is it just able to refer back to it?

#2
[eluser]xwero[/eluser]
If the main function is to create an xml file why do you need the CI object? Can't you just write to the xml file directly?

Code:
$slideDown = new taconite('slideDown');
$slideDown->set('#my_div');
Would generate something like
Code:
<slideDown select="#example4" value="100" />
i assume. So your helper could only add one tag per object.

If you want to store the tags you better have one object where you add all the tags for the xml file to.
Code:
$this->tactonite->open_tag('slideDown');
$this->tactonite->set('#my_div');
$this->tactonite->close_tag('slideDown');
// or with php5 method chaining
$this->tactonite->open_tag('slideDown')->set('#my_div')->close_tag('slideDown');
// ...
$this->tactonite->create_xml('path/to/file/filename');
So a library would be better than a helper.

#3
[eluser]thinkigniter[/eluser]
Thanks for answering xwero.

This is what I have so far...

Code:
class Taconite{
    
    /* Recieve the Jquery function name */
    var $jq_func = '';
    
    /* Create CI variable */
    var $CI = NULL;    
    
    /* Instanciate the element */
    function taconite( $element = '' )
    {
        if( $element != '' )
        {    
            /* Assign the CodeIgniter object to the variable CI */
            if( $this->CI == NULL )
            {
                $this->CI =& get_instance();    
            }
            /* Stores the Jquery function name */
            if( is_string($element) )
            {
                $this->jq_func = $element;
            }            
        }
    }
    /* Set and write the command and args to the xmlstring */
    function set($command = NULL, $args = NULL)
    {
        $arguments = '';

        $count = 1;

        /*  */
        if( is_array($args) and is_string($command) )
        {
            foreach( $args as $v)
            {
                /*  */
                $arguments .= ' arg'.$count++.'="'.$v.'" ';
            }
            
            $this->CI->store['xmlString'][] ='<'.$this->jq_func.' select="'.$command.'"'.$arguments.'/>';
        }
        /*  */
        elseif( ($args == 'fast' or $args == 'slow' ) and is_string($command) )    
        {
            $this->CI->store['xmlString'][] ='<'.$this->jq_func.' select="'.$command.'" arg1="'.$args.'" />';
        }
        /*  */
        elseif( is_string($args) and is_string($command) )    
        {
            $this->CI->store['xmlString'][] ='<'.$this->jq_func.' select="'.$command.'" >'.$args.'</'.$this->jq_func.'>';
        }
    }
}/* &lt;!-- Taconite --&gt; */

Now what i'm worried about is have multiple copies of "taconite" class with "get_instance()" refering back to the parent class where the content of the "xmlString" array is read and displayed as an xml file.

Are all the array's and objects from the CI_BASE object included in my "taconite" class or are they only refering back to them?

#4
[eluser]xwero[/eluser]
There is no reason why the class needs to depend on CI to store the generated tags.

My taconite class
Code:
class Taconite
{
   var $tag_data = array();
   var $generated_tags = array();
   var $opened_tag = '';
  
   function open_tag($name)
   {
      if( ! empty($this->opened_tag))
      {
         return false;
      }
      // name checks here
      $this->opened_tag = $name;
      return true;
   }

   function set($command, $args = NULL)
   {
      $this->tag_data[$this->opened_tag] = array($command,$args);  
   }

   function close_tag($name)
   {
      if($name != $this->opened_tag)
      {
         return false;
      }

      if(! isset($this->tag_data[$this->opened_tag]))
      {
        return false;
      }
      // generate the tags here
      $this->generated_tags[] = $generated_tag;
      unset($this->tag_data[$this->opened_tag]);
      $this->opened_tag = '';
   }

   function create_xml($filepath)
   {
      if(empty($this->generated_tags))
      {
         return false;
      }
      // add tags to xml file
   }
}

#5
[eluser]thinkigniter[/eluser]
Great example.

I love this
Code:
function set($command, $args = NULL)
   {
      $this->tag_data[$this->opened_tag] = array($command,$args);  
   }
So simple.

Thanks for the work but the format of the xml must be like this...

Code:
<taconite>
<css select="p.important" arg1="red" arg2="blue"/>
<slideUp select="div#features ul" arg1="fast"/>
<replaceContent select="div#features ul">
<ul>
<li>ReplaceContent</li>
<li>css</li>
</ul>
</replaceContent>
<css select="div#features li" arg1="blue"/>
<slideDown select="div#features ul" arg1="slow"/>
</taconite>

As you can see multiple "taconite objects" are needed to generate the correct formats all writing back to one source in the parent controller

#6
[eluser]xwero[/eluser]
You shouldn't look at the tags as objects, this is one of the reasons the library you CI-ed has so much negative responses. It's too much abstraction for a tag that can be generated using a function.

The tags are just a bunch of strings with the same format. you can write one function to create all xml style tags you need.
Code:
function create_tag($name,$attributes='',$content='',$self_closing = false)
{
   $output = '<'.$name;

   if(! empty($attributes)
   {
      $output .= ' '.$attributes;
   }

   if($self_closing)
   {
      $output .= ' />';
   }
   else
   {
      $output .= '>'.$content.'</'.$name.'>';
   }

   return $output;
}

And you can use this function to create functions that are easier for the developers to use.

But back to your helper now. For the CI-ed class it makes sense to make different objects because they are independent. But your helpers functionality is to create an xml file which means the tags are related.

An example how the creation of your taconite xml file would look with my library
Code:
$this->taconite->open_tag('css')->set('p.important', array('red','blue'))->close_tag('css');
$this->taconite->open_tag('slideUp')->set('div#features ul', 'fast')->close_tag('slideUp');
// ...
$this->taconite->create_xml('path/taconite');
It would be more convenient if the tag creation is just
Code:
$this->taconite->add_tag('slideUp','div#features ul','fast');

#7
[eluser]thinkigniter[/eluser]
Thanks XWERO,

I understand, I like the way you've done this do you mind if I incorporate some of the idea's you've shown here into my final scripts.

Of course, I won't just copy and paste but rework the ideas.

Thanks again.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.