5 replies
  • WEB DESIGN
  • |
I'm trying to use the same basic article backdrop for each article on my site. For this I use the include function. One problem I'm running into is all the <head> stuff (meta, title). I don't want each article to have two different files since that would create a lot of extra work for me and the reason why I'm doing this in the first place is to save time if I ever redesign my site. Anyway, one way of getting around this would be to use functions. w3 says that you can call a function from anywhere within the document, but that's not true. If I call it before I declare it, it won't know what to do and pretty much "erase" the rest of the document from that point onward when it tries to display it. Here's the code:
****************Article design page*****************
<html>
<head>
<?php
metafun($head);
?>
</head>
<body>
<?php
include "ArticleTemplate.txt";

?>


</body>
</html>


***************article file itself*******************
<?php
$head = "<meta name=description content=hello>";
?>

hi


<?php

function metafun($head){
echo $head;
}
?>
*****************

If this was C it should work, but it's not and it doesn't. Any alternatives out there?
#php
  • Profile picture of the author ziffgone
    When you say "article file itself", what file is this? The ArticleTemplate.txt?

    Anyway, here's my take on it. Try placing all of your functions in a "functions.php" file.

    functions.php:


    function metafun($head){
    echo $head;
    }


    Then maybe go with this...

    ****************Article design page*****************


    <?php
    require_once("functions.php");
    $head = "<meta name=description content=hello>";
    ?>
    <html>
    <head>
    <?php
    metafun($head);
    ?>
    </head>
    <body>
    <?php
    include "ArticleTemplate.txt";
    ?>
    </body>
    </html>

    How's that look to you?

    Regards...
    {{ DiscussionBoard.errors[805040].message }}
    • Profile picture of the author Darth Executor
      Originally Posted by ziffgone View Post

      When you say "article file itself", what file is this? The ArticleTemplate.txt?
      Yes.

      Anyway, here's my take on it. Try placing all of your functions in a "functions.php" file.

      functions.php:


      function metafun(){
      echo ;
      }


      Then maybe go with this...

      ****************Article design page*****************


      <?php
      require_once("functions.php");
      = "<meta name=description content=hello>";
      ?>
      <html>
      <head>
      <?php
      metafun();
      ?>
      </head>
      <body>
      <?php
      include "ArticleTemplate.txt";
      ?>
      </body>
      </html>

      How's that look to you?

      Regards...
      That won't work. I want to pick up the meta tag information from the ArticleTemplate.txt page. Although a third page that holds all the meta tag information isn't a bad idea. I'll wait a bit to see if someone has a solution that does exactly what I asked since that would be the least amount of work, but if not I'll implement this.
      {{ DiscussionBoard.errors[805054].message }}
  • Profile picture of the author ziffgone
    OK,

    Do you need to save the "ArticleTemplate.txt" as a text file? Once you include PHP code in the text file you need to do one of two things. Save it instead as "ArticleTemplate.php". Doesn't matter if it has a ".php" extension because the text will still work just like text but the PHP will now be parsed properly.

    If you want to keep the ".txt" extension, you instead need to add a Mime type to text files on your site to allow PHP to parse them like a ".php" file. This can be done by either adding a ".htaccess" file with the following line to your root directory or add the following line to your existing ".htaccess" file:

    AddType application/x-httpd-php .php .txt

    Either of those two solutions should solve your problem.

    Regards,
    Perry.

    P.S. - All PHP is parsed from both the main file and the included file(s) before anything is outputted to the browser, this is why you can declare a PHP function anywhere in the file(s).
    {{ DiscussionBoard.errors[805156].message }}
    • Profile picture of the author Darth Executor
      Originally Posted by ziffgone View Post

      OK,

      Do you need to save the "ArticleTemplate.txt" as a text file? Once you include PHP code in the text file you need to do one of two things. Save it instead as "ArticleTemplate.php". Doesn't matter if it has a ".php" extension because the text will still work just like text but the PHP will now be parsed properly.
      The PHP is parsed properly regardless of the included file's extension. All include does is cut all the code from the text file and adds it to the original php file. I've tested it before to make sure the extension type of the included file doesn't matter, and it doesn't. Other php code works just fine with it. But, just in case, I changed the extension of the txt file to php and the result is the same. No, I don't need a .txt extension. It's more convenient but if changing its extension made this work, I'd do it. But it doesn't.


      P.S. - All PHP is parsed from both the main file and the included file(s) before anything is outputted to the browser, this is why you can declare a PHP function anywhere in the file(s).
      Declare, yes. But I also thought I could call the function from anywhere in the file. I guess that's not really true.
      {{ DiscussionBoard.errors[805240].message }}
  • Profile picture of the author Darth Executor
    I've come up with a solution on my own:

    In ArticleTemplate.txt, instead of letting the article body free, I include it in a variable. So ArticleTemplate.txt now contains two variables: one with the meta tags, a second with the article body. In the original file, I include ArticleTemplate.txt in the head, but since all it does is include the variables, it adds nothing to the visible page (or the source). At this point I echo the meta tag variable in the head tag and the rest of the article in the body tag. It's a bit annoying to make such huge variables, but other than that it seems to be working.
    {{ DiscussionBoard.errors[805257].message }}

Trending Topics