Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 56
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    Running a loop through an array

    Okay, heres my issue.

    In my template, I have the code:

    Code:
    {nav_item:start}
    <li>{nav_item:name}</li>
    {nav_item:end}
    Which is being sent to my code as:

    nav_item("start");
    nav_item("name");
    nav_item("end");

    What I want to do, is make a nav_item function that recognizes the start and end as a loop.

    So in my function I can have an array:

    PHP Code:
    $navlinks = array(
    "Home" => "http://www.mydomain.com",
    "Forums" => "http://www.mydomain.com/forums",
    ); 
    And have my function return as its final product:

    Code:
    <li><a href="http://www.mydomain.com">Home</a></li>
    <li><a href="http://www.mydomain.com/forums">Forums</a></li>
    How do I go about parsing this loop? I know that I wasnt completely clear in describing this, so if you don't understand something, please ask.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    PHP Code:
    $navlinks = array(
    "Home" => "http://www.mydomain.com",
    "Forums" => "http://www.mydomain.com/forums",
    ); 

    foreach(
    $navlinks as $key => $value) {
         
    $html .= '<li><a href="'.$value.'">'.$key.'</a></li>';
    }

    $assign_your_template("nav"$html); 
    Not sure what templating system your using but this is one method through php code. If you using smarty you can loop through arrays like so.

    PHP Code:
    $navlinks = array(
    array(
    "name" => "Home""link" => "http://www.mydomain.com"),
    array(
    "name" => "Forums""link" => "http://www.mydomain.com/forums")
    );

    $smarty->assign("nav"$navlinks); 
    Code:
    {section name=menu loop=$nav}
    {strip}
         <li><a href="{$nav[menu].link}">{$nav[menu].name}</a></li>
    {/strip}
    {/section}

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    I am not using smarty, Its something custom, I need some way to make that loop through there with the template code I already have. I know its going to be more difficult, but thats what I need to happen, now how do I go about doing that?

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you can't really do that, I think you're going to have to do it in a different way.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by GJay View Post
    you can't really do that, I think you're going to have to do it in a different way.
    What you mean is, it is possible. But it's hard.

    The way Smarty works is it takes Smarty codes and transforms them to PHP.

    This allows them to do fancier stuff like that.

    If you're executing code as you parse it, I think you'll find that it will be a lot harder.

    What you could possibly do. ob_start() allows nesting. So, once you see nav_item:start, start another ob_start layer. Then once you find nav_item:end, collect the data from the output buffer. Parse out your template code from the output you collected. Then do a simple foreach and you're done.

    Actually, that's really easy. :P

    EDIT: This is a good idea for my own templates
    Last edited by aedrin; 03-23-2007 at 06:17 PM.

  • #6
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Cant really help without knowing how the template engine acts. Since you want it in the template. You will have to do through php and just add in the menu as a variable to the template. Or Create a separate menu.tpl and just loop through that in php and then put all the output in one variable for display. Just a thought to keep code/display separate.

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Just a thought to keep code/display separate.
    This is the problem that I find often, people want to separate code and HTML 100%. This is not logical. There's no reason for it.

    What you want to separate is business logic. Database queries, calculations. You want to generate the data in 1 file. Then display it in another. Hence Model and View. HTML should never pass through the Controller, and data is not flat, so you have to generate the HTML through code in the template.

  • #8
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    This is the problem that I find often, people want to separate code and HTML 100%. This is not logical. There's no reason for it.

    What you want to separate is business logic. Database queries, calculations. You want to generate the data in 1 file. Then display it in another. Hence Model and View. HTML should never pass through the Controller, and data is not flat, so you have to generate the HTML through code in the template.
    Yea I agree to a point, sometimes its more trouble to figure out a way to do this when you can just put in php and be done in 2mins. That is what I used to do and it gets messy after a while. I then started using smarty and keeping everything separate, sure you have to get creative at times like it seems he has to do, but I think its worth it and much more cleaner. Now obviously there will be things you really can't do and using templates with the nice { } your still using "code" to a point. So I agree with the "idea" keeping things separate, but know you can't do this a 100% of the time. To each his own

  • #9
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    What you could possibly do. ob_start() allows nesting. So, once you see nav_item:start, start another ob_start layer. Then once you find nav_item:end, collect the data from the output buffer. Parse out your template code from the output you collected. Then do a simple foreach and you're done.
    That sounds great in theory, but how would that actually work?

    The way my template thing is working for the navbar, is that between the start and end should be repeated for every instance in the array.

    maybe something in theory, dont know the code for it, but something like:

    Code:
    {nav_item:start:}
    <li>{nav_item:name:}</li>
    {nav_item:end:}
    when nav_item("start"); is called, it does a preg match to find everything between the {nav_item:start:} the {nav_item:end:} Then the function strips out everything before the start and everything after the end lines and loops the array through the rest. But how would you go about writing this actually?

  • #10
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    why not just have a 'navitems()' function in your template that displays the navigation? You're making it awfully complicated.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #11
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    okay, how can I do it to make it less complicated, but keep the basic structure the same?

  • #12
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    *bump*

    The template file tells the function how to lay out the navigation, the function just fills it. That is what Im trying to do, so I need a code or something that will run through the array and populate the template.

  • #13
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Paste up how you handle templates. IE how you define variables and such and then output to the template

  • #14
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    This is my template parsing class:

    PHP Code:
    class tpl_parse
    {    
        public 
    $superclass// Superclass usage in this func
        
    public $tplname// What template to use
        
    public $pagename//What page to load
        
    public $tpl//Stores template info.
        
    public $variable = array(); // Stores information about the variable in use
        
        
    function run()
        {
            
    $this->tpl file_get_contents(ROOT_PATH."themes/".$this->tplname."/templates/pages/".$this->pagename.".tpl"); // Load the tpl file into the variable
            
    while(preg_match("/{(.*)\:(.*)\:\}/"$this->tpl$this->variable)) // Search for variables
            // Call function $this->variable[1] with attribute $this->variable[2]
            
    {
                
    $func_name=$this->variable[1];
                if (
    method_exists($this$func_name))
                {
                    
    $this->$func_name($this->variable[2]);
                }
                else
                {
                    
    $this->superclass->err->errno 3;
                    
    $this->superclass->err->throwerr();
                }
            }
            echo 
    $this->tpl;
        }
        function 
    file_include($tplfile)
        {
            
    $pos strpos($tplfile".."); // Make sure people dont go up directories
            
    if ($pos === false// Does this string exist?
            
    {
                
    $this->tpl str_replace($this->variable[0],file_get_contents(ROOT_PATH."themes/".$this->tplname."/templates/".$tplfile.".tpl"), $this->tpl); // Include another tpl
            
    }
            else
            {
                
    $this->superclass->err->errno 2;
                
    $this->superclass->err->throwerr();
            }        
        }
        function 
    config($configcall)
        {
            
    $this->tpl str_replace($this->variable[0], $this->superclass->config[$configcall], $this->tpl); // Include another tpl
        
    }
        function 
    lang($langvar)
        {
            
    $this->tpl str_replace($this->variable[0], $this->superclass->lang->text[$langvar], $this->tpl); // Include another tpl
        
    }
        function 
    nav_item($var)
        {
            
    $this->tpl str_replace($this->variable[0], ""$this->tpl);
        }

    called like this:

    PHP Code:
    $superclass->tpl->tplname "default";
    $superclass->tpl->pagename "index";
    $superclass->tpl->run(); 
    index.tpl looks like this:

    Code:
    {file_include:header:}
    {file_include:top_search:}
    {file_include:forum_group:}
    {file_include:stats:}
    {file_include:footer:}
    each of which are parsed by the file_include() function in my class.

    anything else you need?

  • #15
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    How are you assigning variables? Only with functions?


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

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