[RESOLVED] Don't want to use eval()

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

I am quite stuck here.

I am starting to build my own simple CMS with templating. I would like to do it very much the way drupal does it in the way that on the actual template file I can just have some variables available like $url, $title etc. The issue I have is with the variable $content which would get the content of the page.

Now I can get which file to go get for example if I go to "http:://cms.dem/user/create" (not an actual url) I know I am working with the "user" module and I need the "create.php" file. But this file might (most likely will) have html in it and then eval() does not know what to do with it ...

How will I be able to get the contents of a file with the variables already parsed* into a variable without the use of eval(). Is it even possible?

*for lack of a better word
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

I think I figured it out ... there we go ... all I needed to do is use an output buffer, the code then looks something as follows.
PHP Code: [ Select ]
ob_start();
  include($action_file);
  $content = ob_get_contents();
ob_end_clean();
include($template_file);
  1. ob_start();
  2.   include($action_file);
  3.   $content = ob_get_contents();
  4. ob_end_clean();
  5. include($template_file);
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I'm not sure I understand why you're using include() and storing it in a buffer instead of using something like file_get_contents(). Does using include() replace the variables found in $action_file with the contents of variables already registered in the script ?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6230
  • Loc: South-Africa

Post 3+ Months Ago

Yeah it does ... let's say I have the following:

somefile.php
Code: [ Select ]
<p>
<?php
 $foo = "bar";
 echo $foo;
?>
</p>
  1. <p>
  2. <?php
  3.  $foo = "bar";
  4.  echo $foo;
  5. ?>
  6. </p>


index.php
Code: [ Select ]
<?php
 ob_start();
  $action_file = "somefile.php";
  include($action_file);
  $content = ob_get_contents();
 ob_end_clean();
 $template_file = "template.php";
 include($template_file);
?>
  1. <?php
  2.  ob_start();
  3.   $action_file = "somefile.php";
  4.   include($action_file);
  5.   $content = ob_get_contents();
  6.  ob_end_clean();
  7.  $template_file = "template.php";
  8.  include($template_file);
  9. ?>


template.php
Code: [ Select ]
...
<body>
<?php echo $content; ?>
</body>
...
  1. ...
  2. <body>
  3. <?php echo $content; ?>
  4. </body>
  5. ...


I would see the word "bar" printed on the screen.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Ah ok. I didn't realize you were using <?php?> tokens in $action_file. I thought you had something like this.

Code: [ Select ]
This is a $color ball

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: Bogey and 90 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.