SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with EVAL() help please

    Hello everyone,

    Right i am having a little trouble using the eval statement for what i want bit cant think of a work around at the moment.

    What i have is my site templates stored in a database. Now obviously this contains HTML markup. I have a function which basically looks in the database and recovers the content for the required template. But i am using the Eval to process it before it is getting delivered to the user, this is causing a problem becasue eval likes strict PHP code ... not HTML. Here is the function code just to demonstrate :

    PHP Code:
    function fetchTemplate($templateName) {

        
    $query "SELECT content FROM templates WHERE name='".$templateName."' LIMIT 1";
        
    $tRes mysql_query($query);
        
    $template mysql_fetch_array($tRes);
        
        
    $content "echo ".$template[content]."";
        return 
    $content;
        
    }

    function 
    pageHeader($pageTitle) {
        
        eval(
    fetchTemplate("page_header"));
        
    }

    function 
    pageFooter() {
        
        eval(
    fetchTemplate("page_footer"));
        

    Is there another way of doing this rather then using eval ??

    Regards
    Chris

  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)
    Function/Methods should only really return data unless explicitly designed not to, I think what you're looking for is something like...

    PHP Code:
    <?php
    function fetchTemplate($templateName)
    {
        
    $result mysql_query(
            
    sprintf(
                
    "SELECT content FROM templates WHERE name = '%s' LIMIT 1",
                
    mysql_real_escape_string($templateName)
            )
        );
        
    $record mysql_fetch_assoc($result);
        return 
    $record['content'];
    }

    function 
    displayHeader()
    {
        echo 
    fetchTemplate('page_header');
    }

    function 
    displayFooter()
    {
        echo 
    fetchTemplate('page_footer');
    }
    ?>


    Sorry, I missed the fact you have PHP code in your templates, the trick is to end parsing before passing the rest of the template string to eval().

    Note the '?>'

    PHP Code:
    <?php
    function fetchTemplate($templateName$evaluate false)
    {
        
    $result mysql_query(
            
    sprintf(
                
    "SELECT content FROM templates WHERE name = '%s' LIMIT 1",
                
    mysql_real_escape_string($templateName)
            )
        );
        
    $record mysql_fetch_assoc($result);
        return 
    $evaluate ? eval(sprintf('?>%s'$record['content'])) : $record['content'] ;
    }

    function 
    displayHeader()
    {
        echo 
    fetchTemplate('page_header'true);
    }

    function 
    displayFooter()
    {
        echo 
    fetchTemplate('page_footer'true);
    }
    ?>
    Last edited by AnthonySterling; Oct 6, 2009 at 04:47. Reason: Read post properly
    @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
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Function/Methods should only really return data unless explicitly designed not to, I think what you're looking for is something like...

    PHP Code:
    <?php
    function fetchTemplate($templateName)
    {
        
    $result mysql_query(
            
    sprintf(
                
    "SELECT content FROM templates WHERE name = '%s' LIMIT 1",
                
    mysql_real_escape_string($templateName)
            )
        );
        
    $record mysql_fetch_assoc($result);
        return 
    $record['content'];
    }

    function 
    displayHeader()
    {
        echo 
    fetchTemplate('page_header');
    }

    function 
    displayFooter()
    {
        echo 
    fetchTemplate('page_footer');
    }
    ?>
    Ah ok, so my way i was kinda muddeling things up a bit ?

    why change to using the sprintf function though ? i have just looked it up on the php manual and i understand what it does but i just wondered if there was any advantage to this ?

  4. #4
    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)
    Preference really, I prefer it to concatenation. It allow allows me to cast type and add occasionally add basic logic, I think it's an addiction now.

    Nearly all my posts use it, not intentionally either - is there a (s)printf rehab facility?
    @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.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Preference really, I prefer it to concatenation. It allow allows me to cast type and add occasionally add basic logic, I think it's an addiction now.

    Nearly all my posts use it, not intentionally either - is there a (s)printf rehab facility?
    HaHa! well i see the point *hopes it does not become a new addiction of mine* otherwise i might be joining you if you ever find such a place.

    Thank you for the advise though ! its very much appreciated.

    Regards
    Chris

  6. #6
    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)
    Anytime Chris.
    @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.

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just noticed one thing, i also have php mixed in with the html thats stored in the database, now this is not working, insted its just putting the php on the outputted page ?

    any ideas ?

  8. #8
    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)
    Are you using the second revision Chris?

    Can you post a sample of a template that isn't working also...
    @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.

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right ill have to post up a few pages here, here is the function as it stands now :

    PHP Code:
    function fetchTemplate($templateName) {

        
    $result mysql_query(sprintf("SELECT content FROM templates WHERE name='%s' LIMIT 1",mysql_real_escape_string($templateName)));
        
        
    $record mysql_fetch_assoc($result);    
        return 
    $record['content'];
        
    }

    function 
    pageHeader($pageTitle) {
        
        echo 
    fetchTemplate("page_header");
        
    }

    function 
    pageFooter() {
        
        echo 
    fetchTemplate("page_footer");
        

    Here is the header thats stored in the database :

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br><html><b<head>
    <title>$pageTitle</title></head><body>
    Notice the $pageTitle bit of php, this was working with the other version of the function code i had wrote. However now if you look at the top bar it just says "$pageTitle" insted of the title i have on the page constructor ... which is as follows :

    PHP Code:
    // Include the template functions
    include_once "./includes/template.php";

    // Start building our page
    $title "page title";
    pageHeader($title);

    // Some content here
    echo "page content here";

    //And close the page 
    pageFooter(); 

  10. #10
    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)
    Ah, a few issues here.

    1, You need to use the revised code I posted for embedded php in the templates.
    2, As the variables ($title ...) are defined outside of the function, they won't have scope when we eval the code.
    3, In your template, you still need to add echo's n such.
    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br><html><b<head>
    <title><?php echo $pageTitle; ?></title></head><body>
    Where do you want to go from here? Are you adverse to rewriting some elements?
    @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.

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe i should have explained earlier a bit better what i was wanting to acheive from all of this.

    Whats going to happen when its all coded up is similar to the vbulletin template system, where in the admin panel you can go in and edit, i was hoping this would remove the php aspect from the design aspect. So what i was going to do is use php as my template engine as i do not like smarty and everything like that as its just too .... bulky for my needs.

    I was going to start puttin tags in the html like {title}, {username} etc etc ... then have php filter these out and replace them with the relevent details ... i hope that makes sence

  12. #12
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and appologies ... i missed your revised code *smacks head*

    oh and no i aint adverse as to rewriting some elements, iv being out of the game for around 3 years and my coding is very rusty, but i thought there is nothing like throwing myself in at the deep end to re-learn what i have forgot.

  13. #13
    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)
    OK, here's a pretty simple Template object I knocked up - it should do what you ask.

    Usage
    PHP Code:
    <?php
    #create
    $template = new Template('Hello <?php echo $user; ?>!');

    #bind variable(s)
    $template->bind(array('user' => 'SilverBulletUK'));

    try
    {
        echo 
    $template->render();
    }
    catch(
    Exception $exception)
    {
        echo 
    $exception->getMessage();
    }
    ?>
    object
    PHP Code:
    class Template
    {
        protected
            
    $template    '',
            
    $data        = array();

        public function 
    __construct($template)
        {
            
    $this->template $template;
        }

        public function 
    bind($data = array())
        {
            
    $this->data array_merge(
                
    $data,
                
    $this->data
            
    );
        }

        public function 
    render()
        {
            
    ob_start();
            
    extract($this->data);
            if(
    true !== @eval(sprintf('return true; ?>%s'$this->template)))
            {
                throw new 
    Exception('Bad template.');
            }
            eval(
    sprintf('?>%s'$this->template));
            
    $__compiled__ ob_get_clean();
            
    ob_end_clean();
            return 
    $__compiled__;
        }

    Given your early function examples, you would do something like the following...
    PHP Code:
    function fetchTemplate($name)
    {
        require_once(
    'class.template.php');
        
    $result mysql_query(
            
    sprintf(
                
    "SELECT template FROM content WHERE name = '%s'",
                
    mysql_real_escape_string($name)
            )
        );
        
    $record mysql_fetch_assoc($result);
        return new 
    Template($record['template']);
    }

    function 
    displayHeader($data)
    {
        
    $template fetchTemplate('header');
        
    $template->bind($data);
        try
        {
            echo 
    $template->render();
        }
        catch(
    Exception $exception)
        {
            echo 
    'header template corrupted';
        }
    }

    displayHeader(array('title' => 'My CMS Page Title')); 
    @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.

  14. #14
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha ... now i am tottaly lost, so the last code block above is what would replace my template.php file ... this is what contained my earlier function. Now it calls class.template.php ... which file is this ? ... is this the "object" you listed above the other code block ?

    Sorry its just very strange to understand whats going on thats all :S
    I don't have an adictive personality! ,I just can't say no is all

  15. #15
    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)
    Quote Originally Posted by bainsy View Post
    haha ... now i am tottaly lost, so the last code block above is what would replace my template.php file ... this is what contained my earlier function. Now it calls class.template.php ... which file is this ? ... is this the "object" you listed above the other code block ?

    Sorry its just very strange to understand whats going on thats all :S
    Spot on.

    PHP Code:
    // Include the template functions, which will also include 'class.template.php' automatically.
    include_once "./includes/template_functions.php";



    // Start building our page

    displayHeader(array('title' => 'Page Title'));



    // Some content here

    echo "page content here";



    //And close the page 

    displayFooter(); 
    @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.

  16. #16
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol so i did know what was going on, i just wondered why you included the first block about "usage" but gather u was just showing how it would work.

    Right i think i know whats going on although i am going to have to study the code now and try and understand it ... i think the php manual is going to be my friend tonight as there is a fair few things in there i have never come accross in the past.

    So all i can say is thank you very much for the help so far. but i do have one more tiny question which i hope will not be too complicated, but if i wanted to add language packs into this project ( its not a cms its a new type of forum idea i have ) then how hard is this to impliment ?
    I don't have an adictive personality! ,I just can't say no is all

  17. #17
    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)
    Not a problem, not entirely sure about the lingo side of it, there's a lot to take in that's for sure (i18n, L10n... ).

    I hear GetText() is a good place to start, failing that, the question has been asked a fair few times here.

    Try a quick search.
    @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.

  18. #18
    SitePoint Zealot
    Join Date
    Feb 2006
    Location
    East Yorkshire, UK
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok then will do, thanks again ... ill try your code shortly as just about to set off for home now. will keep you posted ... thanks

    Chris
    I don't have an adictive personality! ,I just can't say no is all


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
  •