SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending variables to template

    I have a very simple templating engine Here is the part that sends variables from business logic to templates:

    PHP Code:
    // Load variables
    foreach ($this->variables as $key => $value)
    {
        $
    $key $value;

    Which then works like this:

    PHP Code:
    // Send variable to template
    $template->variable 'hello'
    Now my problem, I would like to improve it a bit so that this would be possible:

    PHP Code:
    // Update a single value in array
    $template->array['key'] = 'new value'
    Somehow the muse doesn't work and I cannot figure out how to do it.

    At the moment, I have to update the whole array and then resend it to template but many times that results in lots of redundant code...

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you please post your templating engine? Your samples are a little obscure...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Re-reading your post, I think PHP's SPL maybe what you are looking for. It can make your classes look a little messy at times, but if you need the functionality, it certainly beats re-writing your own.

    Take a look at the following and let me know how you get on...I've knocked up a quick sample for you which provides 3 ways of setting/getting the template data.

    PHP Code:
    <?php
    class template implements ArrayAccess
    {
        private 
    $aVariables = array();
        
        public function 
    offsetExists$offset )
        {
            return isset( 
    $this->aVariables[$offset] );
        }
        
        public function 
    offsetSet$offset$value)
        {
            
    $this->aVariables[$offset] = $value;
        }
        
        public function 
    offsetGet$offset )
        {
            return 
    $this->aVariables[$offset];
        }
        
        public function 
    offsetUnset$offset )
        {
            unset( 
    $this->aVariables[$offset] );
        }
        
        public function 
    __set$sKey$mValue )
        {
            
    $this->aVariables$sKey ] = $mValue;
        }
        
        public function 
    __get$sKey )
        {
            return 
    $this->aVariables$sKey ];
        }
        
        public function 
    getVariables()
        {
            return 
    $this->aVariables;
        }
    }

    //--> Demo One
    echo '<h4>Demo One</h4>';
    $oTemplate = new template();
    $oTemplate['article_title'] = 'PHP SPL';
    $oTemplate['article_date'] = '21/12/2008';
    $oTemplate['article_author'] = 'SilverBulletUK';
    echo 
    $oTemplate->article_title '<br />';
    echo 
    $oTemplate->article_date '<br />';
    echo 
    $oTemplate->article_author '<br />';

    //--> Demo Two
    echo '<h4>Demo Two</h4>';
    $oTemplate = new template();
    $oTemplate->article_title 'PHP SPL';
    $oTemplate->article_date '21/12/2008';
    $oTemplate->article_author 'SilverBulletUK';
    echo 
    $oTemplate['article_title'] . '<br />';
    echo 
    $oTemplate['article_date'] . '<br />';
    echo 
    $oTemplate['article_author'] . '<br />';

    //-->Demo Three
    echo '<h4>Demo Three</h4>';
    $oTemplate = new template();
    $oTemplate['article_title'] = 'PHP SPL';
    $oTemplate['article_date'] = '21/12/2008';
    $oTemplate['article_author'] = 'SilverBulletUK';
    foreach (
    $oTemplate->getVariables() as $sKey => $mValue)
    {
        echo 
    $sKey ' - ' $mValue '<br />';
    }

    //--> Output
    /*
    Demo One
    PHP SPL
    21/12/2008
    SilverBulletUK
    Demo Two
    PHP SPL
    21/12/2008
    SilverBulletUK
    Demo Three
    article_title - PHP SPL
    article_date - 21/12/2008
    article_author - SilverBulletUK
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if this will make it any clearer:

    PHP Code:
    <?php

    /*
     @desc Template object for loading views
     @param object $registry
     @author Richard Knop
    */
    class Template
    {
        private 
    $registry;
        
        private 
    $variables = array();
        
        
    /*
         @desc Constructor - loads registry
         @access public
         @return void
        */
        
    public function __construct($registry)
        {
            
    $this->registry $registry;
        }
        
        
    /*
         @desc Set undefined variables
         @access public
         @param string $index
         @param mixed $value
         @return void
        */
        
    public function __set($key$value)
        {
            
    $this->variables[$key] = $value;
        }
        
        
    /*
         @desc Load template
         @access public
         @param string $template Template name
         @return void
        */
        
    public function show($template)
        {
            try
            {
                
    $template SITE_PATH.'/application/views/'.$template.'.php';
                
                
    // If template doesn't exist
                
    if (!file_exists($template))
                {
                    throw new 
    Exception('Template not found.');
                }
                
                
    // Load variables
                
    foreach ($this->variables as $key => $value)
                {
                    $
    $key $value;
                }
                
                include 
    $template;
            }
            catch (
    Exception $e)
            {
                
    printf("<p>Exception: %s</p>\n"$e->getMessage());
                exit();
            }
        }
    }

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Check out my 2nd post.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see but that's basically the same thing I already have.

    I don't know how to explain it.

    I need to send and associative array to template. Then I will do some calculations and I might need to change just a single item in the array. That's what I need.

    Because now, I have to resend the whole array again.

  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm I think I understand now. Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •